Quote:
Originally posted by Kurious
I posted a question last night, but I was unable to include my code. The question is again how to determine the highest and lowest value in an array.
//This program will allow the user to input rainfall amounts for twelve months,
//It will then calculate the total amount, the average amount, and tell the months
//with the highest and lowest amounts.
#include <iostream.h>
void main(void)
{
float rain[12];
cout<<"Enter the rainfall (in inches) for each month "<<endl;
for (int count=1; count<=12; count++)
{
cout<<"Month "<<count<<":";
cin>>rain[count-1];
}
cout.precision(2);
cout.setf(ios::fixed|ios::showpoint);
float total = rain[0]+rain[1]+rain[2]+rain[3]+rain[4]+rain[5]+rain[6]+rain[7]+rain[8]+rain[9]+rain[10]+rain[11];
cout<<"The total rainfall was: "<<total<<endl;
float average = total/12;
cout<<"The average rainfall was: "<< average <<" inches. "<<endl;
}
Any other pointers concerning my code would be greatly appreciated, because this is due Monday, and I'm stuck!!
thanks, George
Well there are some things you could do to make it a bit cleaner. When you are looping through getting the rainfall for each month, why not have a variable called total ( initially set to 0 ) that you add each month to. That would save you the line where you add them all up. What if you had to do that for 100 elements? It would take a while to say array[0]+, array[1]+, ... ,+ array[100]. As for finding the highest and lowest value thats pretty simple. You will need another loop. Assign the lowest value as the first index. Then each loop check in the current index is lower then the "low variable" you have, if it is replace the value with the current array. Use the same method to find the highest value. Try to implement it, if you get stuck let me know.