C++: Make an array out of the elements of a two dimensional array that meet certain requirements -
i new multi-dimensional arrays, question struggling right following: have "maxn" number of days, , highest , lowest temperature recorded these days , have things data. 1 of these things determine days when temperature has been both below , above 0 (i.e.: lowest recorded temp below , highest above). program has output number of days when such thing occurred index of these days.
however, no matter loop use , how, mess , either absolutely irrelevant or infinite loop. here have far:
void abovebelow (int n, float days[maxn][2]{ int counter=0; float a[maxn]; (int i=0; i<n; i++){ (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]<<" "; } } }
you simple for-loop. why use 2 nested loops that?
and plus, if-condition 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 typo in code have pasted. there's syntax error in function declaration.
2.in standard c++, there's no vla (you unknowingly using compiler specific extra). unless maxn const (thanks, james), need to:
float *a = new fload[n]; //do stuffs here delete[] a;
Comments
Post a Comment