152k views
4 votes
Write a program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubles. The program should calculate and display (in this order): the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts. Months should be expressed as English names for months in the Gregorian calendar, i.e.: January, February, March, April, May, June, July, August, September, October, November, December.

User Leslyn
by
3.8k points

1 Answer

7 votes

Answer:

Using c++

Step-by-step explanation:

#include <iostream>

#include <string>

using namespace std;

int main()

{

//this is data declaration

double aveRain, PeakRain, MiniRain;

double monthlyRains[12];

double TotalRains = 0;

int HighMonth, LowMonth;

//This next line is used to declare the moths of the year

String Months[12]= { "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

cout << "Enter the rainfall for Months" << endl;

//This will loop through the months to get the rainfalls

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

{

cout << Months[i] << ": ";

cin >> monthlyRains[i];

TotalRains = TotalRains + monthlyRains[i]; // This is to get the total rainfall

}

// To get the average rainfall

aveRain = TotalRains/12;

//To get the month with the highest rainfall

//This set the first month to highest then loops through the rest.

PeakRain = monthlyRains[0];

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

if (monthlyRains[i]>PeakRain ){

PeakRain = monthlyRains[i];

HighMonth = i;

}

else{

HighMonth = 0;

}

}

MiniRain = monthlyRains[0]; //This mark first month as the lowest

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

if (monthlyRains[i]<MiniRain ){

LowRain =monthlyRains[i] ;

LowMonth = i; // This marks the month with the lowest rain

}else{

LowMonth = 0; //if no lower month is found, the first month is marked as lowest

}

}

//To display the answers

cout << "The total rainfall for the year was " << TotalRains << endl;

cout << "The average rainfall for the year was " << aveRain << endl;

cout <<"The month with the highest rainfall was" <<Months[HighMonth]<< endl;

cout <<"The month with the lowest rainfall was" <<Months[LowMonth]<< endl;

return 0;

}

User Tuinstoel
by
4.2k points