C++ programming Program 1: Write a program that uses a structure to store the fo
ID: 3792897 • Letter: C
Question
C++ programming
Program 1:
Write a program that uses a structure to store the following weather data for a particular month:
Total Rainfall
High Temperature
Low Temperature
Average Temperature
The program should have an array of 12 structures to hold weather data for an entire year. When the program runs, it should ask the user to enter data for each month (the avg temperature should be calculated.) Once the data are entered for all the months, the program should calculate and display the average monthly rainfall, the total rainfall for the year, the highest and lowest temperatures for the year (and the months they occurred in), and the average of all the monthly average temperatures.
Input validation: only accept temperatures within the range between -100 and +140 degrees Fahrenheit.
Program 2:
Modify your program so it defines an enumerated data type with enumerators for the months (JANUARY, FEBRUARY, etc.). The program should use the enumerated type to step through the elements of the array.
User interface is important!
You should post pseudocode
Your code should have appropriate comments
Your code should be well-formatted, with camel case variables
Variables should be self-descriptive
Your code should have functions (besides main!)
Your functions should only do one thing
Explanation / Answer
// C++ code
#include <iostream>
using namespace std;
char monthWeatherArray[12][10] = { "January", "February", "March", "April", "May","June", "July", "August", "September", "October","November", "December" };
struct Weather
{
double totalRainfall;
double highTemperature;
double lowTemperature;
double averageTemperature;
};
double weatherArrayAverage(Weather weatherArray [], char c[])
{
double average = 0;
if(c == "temp")
{
for(int i = 0; i < 12; i++)
{
average = average + weatherArray[i].averageTemperature;
}
return (average / 12);
}
if(c == "rain")
{
for(int i = 0; i < 12; i++)
{
average = average + weatherArray[i].totalRainfall;
}
return (average / 12);
}
}
void getUserData(Weather weatherArray[])
{
for(int i = 0; i < 12; i++)
{
cout << "From " << monthWeatherArray[i] << ": ";
cout << "Total rainfall: ";
cin >> weatherArray[i].totalRainfall;
while(weatherArray[i].totalRainfall < 0)
{
cout << " Invaid Input ";
cout << "Total rainfall: ";
cin >> weatherArray[i].totalRainfall;
}
cout << "Highest temperature: ";
cin >> weatherArray[i].highTemperature;
while((weatherArray[i].highTemperature < -100) or (weatherArray[i].highTemperature > 140))
{
cout << " Invalid Input ";
cout << "Highest temperature: ";
cin >> weatherArray[i].highTemperature;
}
cout << "Lowest temperature: ";
cin >> weatherArray[i].lowTemperature;
while((weatherArray[i].lowTemperature < -100) or (weatherArray[i].lowTemperature > 140) or (weatherArray[i].lowTemperature > weatherArray[i].highTemperature))
{
cout << " Invaid Input ";
cout << "Lowest temperature: ";
cin >> weatherArray[i].lowTemperature;
}
weatherArray[i].averageTemperature = (weatherArray[i].highTemperature + weatherArray[i].lowTemperature)/2;
cout << endl;
}
}
int main()
{
Weather weatherArray[12];
getUserData(weatherArray);
int maximumposition = 0;
int minimumposition = 0;
double minimum = weatherArray[0].lowTemperature;
double maximum = weatherArray[0].highTemperature;
for(int i = 1; i < 12; i++)
{
if(minimum > weatherArray[i].lowTemperature)
{
minimum = weatherArray[i].lowTemperature;
minimumposition = i;
}
if(maximum < weatherArray[i].highTemperature)
{
maximum = weatherArray[i].highTemperature;
maximumposition = i;
}
}
cout << "Average rainfall: " << weatherArrayAverage(weatherArray, "rain") << endl;
cout << "Average temperature: " << weatherArrayAverage(weatherArray, "temp") << endl;
cout << "Highest temperature: " << weatherArray[maximumposition].highTemperature << " (" << monthWeatherArray[maximumposition] << ")" << endl;
cout << "Lowest temperature: " << weatherArray[minimumposition].lowTemperature << " (" << monthWeatherArray[minimumposition] << ")" << endl;
return 0;
}
/*
output:
From January:
Total rainfall: 30
Highest temperature: 42
Lowest temperature: 10
From February:
Total rainfall: 34
Highest temperature: 32
Lowest temperature: 10
From March:
Total rainfall: 67
Highest temperature: 44
Lowest temperature: 32
From April:
Total rainfall: 56
Highest temperature: 32
Lowest temperature: 11
From May:
Total rainfall: 67
Highest temperature: 98
Lowest temperature: 65
From June:
Total rainfall: 54
Highest temperature: 33
Lowest temperature: 21
From July:
Total rainfall: 45
Highest temperature: 44
Lowest temperature: 32
From August:
Total rainfall: 76
Highest temperature: 43
Lowest temperature: 22
From September:
Total rainfall: 87
Highest temperature: 66
Lowest temperature: 54
From October:
Total rainfall: 76
Highest temperature: 44
Lowest temperature: 33
From November:
Total rainfall: 34
Highest temperature: 22
Lowest temperature: 11
From December:
Total rainfall: 44
Highest temperature: 33
Lowest temperature: 21
Average rainfall: 55.8333
Average temperature: 35.625
Highest temperature: 98 (May)
Lowest temperature: 10 (January)
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.