# Homework Cont'

• 02-01-2003
Kurious
Homework Cont'
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
• 02-01-2003
itld
howdy,
did you say
Code:

void main(void)
DUCK.... IN COMING

M.R.
• 02-01-2003
MrWizard
Re: Homework Cont'
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.

P.S. - Use int main.
Code:

int main( void )
{
return 0;
}

• 02-01-2003
nextus
Code:

float total = rain[0]+rain[1]+rain[2]+rain[3]+rain[4]+rain[5]+ra
in[6]+rain[7]+rain[8]+rain[9]+rain[10]+rain[11];
cout<<"The total rainfall was: "<<total<<endl;

why so much work..just do

Code:

for(int i = 0; i < 12; i++)
{
total += rain[i];
}

if to find the most try this

Code:

float most = rain[0]; // sets the most to the first

for(int j = 1; j < 12; j++)
{
if(most < rain[i])
{
most = rain[i];
}
}

just try the opposite for less..oh yea have fun and void main(void)...yikes!!!!!
• 02-01-2003
Mister C
well if you sort the array from largest to smallest:

x[0] would hold the largest value and x[n -1] would hold the smallest. loop would be helpful as well.