Join 136,933 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,862 people online right now. Registration is fast and FREE... Join Now!
hi, i have been trying to figure out this problem for about 3 days now, with no luck. the issue is that i need to find the max value and min value in an array called "rainfall" and display the values along side the month.
gotoxy(10,20); cout << "Average rainfall for 12 months is " << avg << "mm" << endl; gotoxy(10,21);
/*this is where i need to display the values and according months
cout << "The month with the highest rainfall is " << month[i] << " at " << rainfall[i] << "mm." << endl; gotoxy(10,22); cout << "The month with the lowest rainfall is " << month[i] << " at " << rainfall[i] << "mm." << endl;
What's the problem? Are you getting a compiler error? unexpected output?
well at this point it builds and compiles fine, i need to figure out what the max and min values of "rainfall[]" and display it along with the month it corresponds to.
Function of search of the maximal number in a array:
CODE
float max_arr(float *a,int n) { float m; m=*a; for(short i=1;i<n;i+=1) if(m<*(a+i)) m=*(a+i); return m; }
Function of search of the minimal number in a array
CODE
float min_arr(float *a,int n) { float m; m=*a; for(short i=1;i<n;i+=1) if(m>*(a+i)) m=*(a+i); return m; }
Parameters of function are the pointer on a array and quantity of numbers in a array. Function returns accordingly the maximal (minimal) value in array. You only need to insert it into your code!
This post has been edited by Stepler: 14 Mar, 2008 - 08:14 AM
Function of search of the maximal number in a array:
CODE
float max_arr(float *a,int n) { float m; m=*a; for(short i=1;i<n;i+=1) if(m<*(a+i)) m=*(a+i); return m; }
Function of search of the minimal number in a array
CODE
float min_arr(float *a,int n) { float m; m=*a; for(short i=1;i<n;i+=1) if(m>*(a+i)) m=*(a+i); return m; }
Parameters of function are the pointer on a array and quantity of numbers in a array. Function returns accordingly the maximal (minimal) value in array. You only need to insert it into your code!
thanks,
but its not working right, i get lots of errors
l:\assignments & notes\rainfall arrays\rainfall arrays\rainfall arrays.cpp(73) : error C2601: 'max_arr' : local function definitions are illegal l:\assignments & notes\rainfall arrays\rainfall arrays\rainfall arrays.cpp(38): this line contains a '{' which has not yet been matched l:\assignments & notes\rainfall arrays\rainfall arrays\rainfall arrays.cpp(84) : error C2601: 'min_arr' : local function definitions are illegal l:\assignments & notes\rainfall arrays\rainfall arrays\rainfall arrays.cpp(38): this line contains a '{' which has not yet been matched l:\assignments & notes\rainfall arrays\rainfall arrays\rainfall arrays.cpp(98) : error C2065: 'm' : undeclared identifier
gotoxy(10,20); cout << "Average rainfall for 12 months is " << avg << "mm" << endl; gotoxy(10,21); cout<<"max="<<max_arr(rainfall,12)<<endl; gotoxy(10,24); cout<<"min="<<min_arr(rainfall,12)<<endl; /*this is where i need to display the values and according months
cout << "The month with the highest rainfall is " << month[i] << " at " << rainfall[i] << "mm." << endl; gotoxy(10,22); cout << "The month with the lowest rainfall is " << month[i] << " at " << rainfall[i] << "mm." << endl;
*/
_getch(); } float max_arr(float *a,int n) { float m; m=*a; for(short i=1;i<n;i+=1) if(m<*(a+i)) m=*(a+i); return m; } float min_arr(float *a,int n) { float m; m=*a; for(short i=1;i<n;i+=1) if(m>*(a+i)) m=*(a+i); return m; }
k so it works now, thanks stepler. but i still need to display the month for the highest and lowest months...im gunna work on it tonite and ill post back if i get anywhere
gotoxy(10,20); cout << "Average rainfall for 12 months is " << avg << "mm" << endl; gotoxy(10,21); //cout<<"max="<<max_arr(rainfall,12)<<endl; cout<<"Min rainfall in "<<month[min_arr(rainfall,12)]<<endl; gotoxy(10,24); cout<<"Max rainfall in "<<month[max_arr(rainfall,12)]<<endl; //cout<<"min="<<min_arr(rainfall,12)<<endl; /*this is where i need to display the values and according months
cout << "The month with the highest rainfall is " << month[i] << " at " << rainfall[i] << "mm." << endl; gotoxy(10,22); cout << "The month with the lowest rainfall is " << month[i] << " at " << rainfall[i] << "mm." << endl;
*/
_getch(); } int max_arr(float *a,int n) { float m; int nn; m=*a; for(short i=1;i<n;i+=1) if(m<*(a+i)) {m=*(a+i);nn=i;} return nn; } int min_arr(float *a,int n) { float m; int nn; m=*a; for(short i=1;i<n;i+=1) if(m>*(a+i)) {m=*(a+i);nn=i;} return nn; }