In: Computer Science
-In C Programming-
Write a program to display the total rainfall for a year. In addition, display the average monthly rainfall, and the months with the lowest and highest rainfall.
Create an array to hold the rainfall values. Create a 2nd parallel array (as a constant) to hold the abbreviated names of the months. I created my arrays to be 1 element bigger than needed, and then disregarded element [0] (so that my months went from [1] = "Jan" to [12] = "Dec").
Populate the array with the rainfall values entered by the user. Then, display the rainfall values. Display the 1st 6 months, followed by the last 6 months Then display the statistics (the total, average, lowest, and highest rainfall).
Calculate the total rainfall. After you get the total, you should be able to calculate the average (use the size (minus 1 if the array was 1 element bigger than needed) of the array). Then find the index of lowest rainfall and the index of highest rainfall. Finally, display the statistics, using the indexes that were found to get the lowest and highest rainfall value and the name of the month from the arrays.
Format the output to 1 decimal place.
There is no validation.
Program:
#include<stdio.h>
int main()
{
int i,lowest=0,highest=0;
double values[12];
double min,max;
double total=0.0,average;
const char *month[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
printf("\nEnter Rainfall of each of 12 months: \n");
for(i=0;i<=11;i++)
{
scanf("%lf",&values[i]);
}
max=values[0];
min=values[0];
printf("\nMonth and its rainfall value: \n\n");
for(i=0;i<=11;i++)
{
printf("%s : %.2lf\n",month[i],values[i]);
if(max<values[i])
{
max=values[i];
highest=i;
}
if(min>values[i])
{
min=values[i];
lowest=i;
}
total=total+values[i];
}
average=total/12;
printf("\nTotal rainfall for the year : %.1lf\n",total);
printf("\n\nThe average monthly rainfall : %.1lf\n",average);
printf("\n\nThe month %s with the highest rain ie
%.1lf\n",month[highest],max);
printf("\n\nThe month %s with the lowest rain ie
%.1lf\n",month[lowest],min);
return 0;
}
Output: