C++ Write a program using structures to store the following weather information:
ID: 3691514 • Letter: C
Question
C++
Write a program using structures to store the following weather information:
Total Rainfall
High Temperature
Low Temperature
Average Temperature
Use an input file called input.txt to load the data to your array of 12 structures to hold the weather data (the average temperature should be calculated). Once the data for all the months is entered, the program should calculate and display the average monthly rainfall, total rainfall for the year, highest and lowest temps for the year (and the months they happen in) and the average of all the months average temperatures.
Input temps should be between -100 and +140 and and total rainfall must not be less than 0
The information in the input file is the following:
January 0 -10 -20 -12
February 0 20 -30 -10
March 5 45 20 32
April 25 65 45 55
May 12 75 32 66
June 18 85 65 72
July 6 98 88 90
August 18 102 82 91
September 16 85 65 73
October 12 72 45 59
November 5 62 30 45
December 0 46 -15 33
This is an example of the output:
Annual Rain Analysis
Total Rain: ###
Average Rain: ###
Average of average tempature: ###
The highest temp is: ### in August
The Lowest temp is: ### in February
Explanation / Answer
rogram solutions:
include <iostream>
using namespace std;
int main()
{
const int NUM_MONTHS = 12; //number of values
int values[NUM_MONTHS]; //each value
int month; //loop counter
int lowest, highest, rainfall, count;
double average;
double total = 0;
//Array with the names of the months
string months[NUM_MONTHS] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
//Input rainfall for each month
for(int month = 1; month <= NUM_MONTHS; month++)
{
cout << "Enter the total rainfall for " << months[month-1] << ": ";
cin >> rainfall;
//Input validation - enter a value over or equal to 0
if ( rainfall < 0 )
{
cout << "The number you have entered is invalid." << endl;
cout << "Please reenter: ";
cin >> rainfall;
}
//Calculate total rainfall for the year
total += rainfall;
}
//Calculate the average monthly rainfall
average = total/12;
//Find the months with the highest rainfall and lowest rainfall
highest = values[0];
lowest = values[0];
for(int month = 0; month < NUM_MONTHS; month++)
{
if(values[month] > highest)
highest = values[month];
if(values[month] < lowest)
lowest = values[month];
}
//Display total rainfall for year, monthly average, highest and lowest months
cout << " The total rainfall for the year is: " << total << " inches"
<< " The average monthly rainfall is: " << average << " inches";
cout << " The month with the highest rainfall is: " << highest
<< " The month with the lowest rainfall is: " << lowest << endl;
system("pause");
return 0;
}
method2:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.