140k views
2 votes
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").

1 Answer

1 vote

Answer:

#include <stdio.h>

int main()

{

//variable declaration

int low, high;

float lowRain, highRain, total, avg;

//array declaration

float rainfall[13];

char monthName[13][10] = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

//get user input

for(int i=1; i<=12; i++)

{

printf("Enter the rainfall (in inches) for %s: ", monthName[i]);

scanf("%f", &rainfall[i]);

}

//display the monthly rainfall

printf("\\The rainfall that was entered was:\\");

for(int i = 1; i<=6; i++)

printf("%s ", monthName[i]);

printf("\\");

for(int i = 1; i<=6; i++)

printf("%.1f ", rainfall[i]);

printf("\\");

for(int i = 7; i<=12; i++)

printf("%s ", monthName[i]);

printf("\\");

for(int i = 7; i<=12; i++)

printf("%.1f ", rainfall[i]);

//variable initialization

low = 1;

high = 1;

lowRain = rainfall[1];

highRain = rainfall[1];

total = 0;

//calculate the lowest, highest and averaage rainfall

for(int i=1; i<=12; i++)

{

if(lowRain>rainfall[i])

{

lowRain = rainfall[i];

low = i;

}

if(highRain<rainfall[i])

{

highRain = rainfall[i];

high = i;

}

total = total + rainfall[i];

}

avg = total / 12;

//display the result

printf("\\\\The total rain that fell was %.1f inches", total);

printf("\\The average monthly rainfall was %.1f inches.", avg);

printf("\\The lowest monthly rainfall was %.1f inches in %s.", rainfall[low], monthName[low]);

printf("\\The highest monthly rainfall was %.1f inches in %s.", rainfall[high], monthName[high]);

return 0;

}

User Quagaar
by
4.0k points