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 ).
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 = newint[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.
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 .