Reputation: 861
I am new to multi-dimensional arrays, the question I am struggling with right now is the following: I have a "maxn" number of days, and the highest and lowest temperature recorded for these days and I have to do certain things with this data. One of these things is to determine the days when the temperature has been both below and above 0 (i.e.: the lowest recorded temp was below and the highest was above). The program has to output the number of days when such a thing occurred as well as the index of these days.
However, no matter which loop I use and how, I mess it up and either I get something absolutely irrelevant or an infinite loop. Here is what I have so far:
void abovebelow (int n, float days[maxn][2]{
int counter=0;
float a[maxn];
for (int i=0; i<n; i++){
for (int j=0; j<n; j++){
if ((days[i][0]<0 && days[i][1]>0) || (days[i][1]<0 && days[i][0]>0)){
counter++;
i=a[j];
cout<<counter<<" "<<a[j]<<" ";
}
}
}
Upvotes: 2
Views: 233
Reputation: 44444
You could do it with simple for-loop. Why use two nested loops for that?
And plus, the if-condition could be modified to:
for (int i=0; i<n; i++) {
if (days[i][0]*days[i][1]<0) {
counter++;
cout<<"Day "<<i+1<<endl;
}
}
cout<<"Counter: "<<counter<<endl;
PS:
1.I hope its a typo in the code you have pasted. There's a syntax error in the function declaration.
2.In standard C++, there's no VLA (you are unknowingly using your compiler specific extra). Unless maxn
is a const
(thanks, James), You would need to:
float *a = new fload[n];
//do all the stuffs here
delete[] a;
Upvotes: 1