Query the user for rainfall numbers for a city for the 12 months of this year as well as last year. Display this output in table form along with the total rainfall and the average rainfall for each year. The programming basically accomplishes the task, but the code is ugly.
Code:
#include <stdio.h>
#include <string.h>
#define max_number 12
#define months 12
int main(void)
{double current[max_number][12],
current_rain[12],
last[max_number][12],
last_rain[12],
last_sum,
current_sum,
last_average,
current_average;
int i;
for(i=0;i<12;i++)
{printf("Enter this year's rainfall for the current month: ");
scanf("%lf", ¤t_rain[i]);
printf("Enter last year's rainfall for the current month: ");
scanf("%lf", &last_rain[i]);
printf("Now assume the next month is the current month.\n");
}
current_sum=current_rain[0]+current_rain[1]+current_rain[2]+current_rain[3]+current_rain[4]+current_rain[5]+current_rain[6]+current_rain[7]+current_rain[8]+current_rain[9]+current_rain[10]+current_rain[11];
last_sum=last_rain[0]+last_rain[1]+last_rain[2]+last_rain[3]+last_rain[4]+last_rain[5]+last_rain[6]+last_rain[7]+last_rain[8]+last_rain[9]+last_rain[10]+last_rain[11];
current_average=current_sum/months;
last_average=last_sum/months;
printf(" Table of Monthly Rainfall\n");
printf(" January February March April May June July\n");
printf("This year: %lf %lf %lf %lf %lf %lf %lf\n",
current_rain[0], current_rain[1], current_rain[2], current_rain[3],
current_rain[4], current_rain[5], current_rain[6]);
printf("Last year: %lf %lf %lf %lf %lf %lf %lf",
last_rain[0], last_rain[1], last_rain[2], last_rain[3], last_rain[4],
last_rain[5], last_rain[6]);
printf(" August September October November December\n");
printf("This year: %lf %lf %lf %lf %lf\n",
current_rain[7], current_rain[8], current_rain[9], current_rain[10],
current_rain[11]);
printf("Last year: %lf %lf %lf %lf %lf\n",
last_rain[7], last_rain[8], last_rain[9], last_rain[10], last_rain[11]);
printf("Total rainfall this year: %lf\n", current_sum);
printf("Total rainfall last year: %lf\n", last_sum);
printf("Average rainfall this year: %lf\n", current_average);
printf("Average rainfall last year: %lf\n", last_average);
return 0;
}
Any suggestions? Also, when the program displays the rainfall data, it runs the number off about 5 decimal places. For instance, a value of 3.6 would be displayed as 3.60000. This is offsetting the values so that they aren't being displayed under the appropriate month. How do I reduce the number of decimal places displayed?