Asked by Anonymous

I am having trouble identifying the flaws in my code. I need to created a program that calculates the rainfall for a given year in months. I needed it to show the total amount of rain for the year, the average monthly rainfall, and the months with the highest and lowest amounts. So far I got everything to calculate and display correctly all except the highest and lowest months. I have included the full code. Thanks.


#include<iostream>
using namespace std;

void main()
{
double rainfall[12], totalrainfall=0, avgrainfall =0, maximum =0, minimum=0;
int i,j,k,m,mimo, mamo;
char monthnames[12] = {'Jan','Feb','Mar','Apr','May','Jun','Ju…

for (i=0;i<12;i++)
{
cout<<"Enter rainfall for month "<<i+1<<":";
cin>>rainfall[i]; //capture all rainfall values from user
}


for(j=0;j<12;j++)
{
totalrainfall = totalrainfall + rainfall[j]; //calculate total rainfall
}

cout<<"Total rainfall for the year:"<<totalrainfall<<endl;


avgrainfall = totalrainfall/12; //calculate average rainfall
cout<<"Average monthly rainfall: "<<avgrainfall<<endl;


for(k=0;k<12;k++)
{
maximum = rainfall[0];
if (rainfall[k] > maximum)
{
maximum = rainfall[k]; //find maximum rainfall value
mamo = k; //month with max rainfall
}
}

for(i=k;i<=k;i++)
{cout<<"Maximum rainfall month: "<<monthnames[mamo]<<endl;}

for(m=0;m<12;m++)
{
minimum = rainfall[0];
if (rainfall[m] < minimum)
{
minimum = rainfall[m];
mimo = m;
}
}
for(i=m;i<=m;i++)
{cout<<"Minimum rainfall month: "<<monthnames[mimo]<<endl;}

}

Answers

Answered by MathMate
Don't know which version of C++ you use.
In general, string≠char, but
char * points to a string with a '\0' termination character.

You could use the string class, for monthnames, or use an array of pointers, such as:
char *monthnames[]={"Jan","Feb",...};
depending on whether you are allowed to use pointers.

I have not compiled your program to see if there are other problems. If you find other problems, post.
Answered by Abdulkerim Hussen
teacher
There are no AI answers yet. The ability to request AI answers is coming soon!

Related Questions