Please Help me with my c++ problem

So i have a program that i need to write for school but I just can't seem to figure it out. The program needs to print out ( output ) the biggest decreasing subarray ( the subarray with most elements) of an array with "n" elements. So far I've managed to make the program so it recognises the decreasing subarrays and counts them, but i can't figure out how to print out the biggest one ( the subarray with the most elements ).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  #include <iostream>

using namespace std;

int main ()
{

    int i,n,j,k,p1,p2,bnn,z;
    bnn=0;
    i=1;
    j=0;
    k=0;
    cin >> n;
    int a[n];
    for (i=1;i<=n;i++) cin >> a[i];
    for (i=1;i<=n;i++)
    {
        if ((a[i] > a[i+1]) && (k==0))
            {
                p1=i;
                k=1;
            }
        else if ((a[i] < a[i+1]) && (a[i]<a[i-1]))
            {
                p2=i;
                j=1;
                bnn++;
            }
            z=p2-p1;
    }
    cout << "Elements of the subarrays."
    for (i=p1;i<=p2;i++) cout << a[i] << endl;
    cout << "Number of decreasing subarrays: " << bnn << endl;
    cout << "Number of elements of all decreasing subarrays " <<z;

}
First of all the size of an array must be a constant expression. Indexes of elements of an array start from 0 not from 1. So this code

1
2
3
4
    cin >> n;
    int a[n];
    for (i=1;i<=n;i++) cin >> a[i];
    for (i=1;i<=n;i++)


is invalid.
sure

1
2
3
    int a[n];
    for (i=0;i<n;i++) cin >> a[i];
    for (i=0;i<n;i++)

is that better ? now pls help?
1
2
3
      cin >> n;
      int a[n];
 

This means that the array will have n elements.

 
  for (i=0;i<=n;i++) cin >> a[i];

now we are inputing a[0]; a[1]; a[2] ... untill n. I don't understand what the problem is?
As I said the size of an array shall be a constant expression that evaluates during compilation.

For example

const int N = 10;
int a[N];

If you do not know beforehand how many elements of the array you will need then you should either dynamically allocate the array or use std::vector.
1
2
cin >> n;
int a[n];


This is unfortunately invalid because n needs to be a constant value (evaluated at compile-time, not runtime). Here are three work arounds:

1) define the size of a as a very large number which will not be exceeded by n:
1
2
int a[200];
cin >> n; // n must be <= 200 


2) Dynamic memory allocation:
1
2
3
4
cin >> n;
int* a = new int[n];
//...
delete[] a;


3) STL containers:
1
2
3
4
5
6
7
8
cin >> n;
std::vector<int> a;
for (int i = 0; i < n; ++i) 
{
    int temp;
    cin >> temp;
    a.push_back(temp);
}


Regarding the 1 to N, versus 0 to N-1, c++ uses 0 to N-1. So when we iterate through the array, we do this:
for (int i = 0; i < n; ++i). The technical reason why with do this is that when we do this: int a[10];, a becomes a pointer to the first element of a set of 10 integers. when we do this: a[i], the [] operator is telling us to point to the first element in that array + an offset of i.
Last edited on
Can you make an algorithm that will compare the number of elements in decreasing subarrays,please? Sorry for being a dummy but I am new to programming: D .
Last edited on
Topic archived. No new replies allowed.