Program Specification Write a program that uses an array of structures to store
ID: 3694013 • Letter: P
Question
Program Specification Write a program that uses an array of structures to store the following weather data for a particular month: Total Rainfall High Temperature Low Temperature Average Temperature (Calculated by adding the high and low and dividing by 2) 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 average temperature should be calculated.) Once the data is 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 average of all the monthly average temperatures. Functions You should have a minimum of 5 functions. Main should contain very little code. Do not use global variables. Input Validation Only accept temperatures within the range between –100 and +140 degrees Fahrenheit.
Explanation / Answer
#include <iostream>
using namespace std;
const short months = 12;
char MONTHS[months][10] = { "January", "February", "March", "April", "May","June", "July", "August", "September", "October","November", "December" } ;
struct Weather
{
double rainfall;
double hightemp;
double lowtemp;
double avgtemp;
};
void setAverage(Weather [], int);
double arrayAverage(Weather [], char[]);
void max_min_temp(Weather[], short&, short&);
void getData(Weather []);
void lastBits(Weather[]);
int main()
{
Weather array[months];
getData(array);
lastBits(array);
return 0;
}
void setAverage(Weather array[], int i)
{
array[i].avgtemp = (array[i].hightemp + array[i].lowtemp) / 2;
}
double arrayAverage(Weather array [], char choice[])
{
double average = 0;
if(choice == "temp")
{
for(int i = 0; i < months; i++)
average += array[i].avgtemp;
return (average / months);
}
if(choice == "rain")
{
for(int i = 0; i < months; i++)
average += array[i].rainfall;
return (average / months);
}
}
void max_min_temp(Weather array [], short & max_pos, short & min_pos)
{
double min = array[0].lowtemp, max = array[0].hightemp;
min_pos = 0;
max_pos = 0;
for(int i = 1; i < months; i++)
{
if(min > array[i].lowtemp)
{
min = array[i].lowtemp;
min_pos = i;
}
if(max < array[i].hightemp)
{
max = array[i].hightemp;
max_pos = i;
}
}
}
void getData(Weather array[])
{
cout << "Let's do some weather statistics. " << endl << endl;
cout << "We'll do the total rainfall plus "<< "highest and lowest temperatures" << endl<< "according to months ranging from -100 to 140 degrees "
<< "Fahrenheit." << endl << endl;
for(int i = 0; i < months; i++)
{
cout << "From " << MONTHS[i] << ": " << endl << endl;
cout << "Total rainfall: ";
cin >> array[i].rainfall;
while(array[i].rainfall < 0)
{
cout << endl << "Please enter something that isn't negative. "<< endl << endl;
cout << "Total rainfall: ";
cin >> array[i].rainfall;
}
cout << "Highest temperature: ";
cin >> array[i].hightemp;
while((array[i].hightemp < -100) or (array[i].hightemp > 140))
{
cout << endl << "Input a temperature "<< "ranging from -100 to 140 degrees Fahrenheit. "<< endl << endl;
cout << "Highest temperature: ";
cin >> array[i].hightemp;
}
cout << "Lowest temperature: ";
cin >> array[i].lowtemp;
while((array[i].lowtemp < -100) or (array[i].lowtemp > 140)or (array[i].lowtemp > array[i].hightemp))
{
cout << endl << "Either you need to input a temperature "<< "ranging from -100 to 140 degrees " << endl<< "Fahrenheit or your lowest is bigger than the highest. "<< endl << endl;
cout << "Lowest temperature: ";
cin >> array[i].lowtemp;
}
setAverage(array, i);
cout << endl;
}
}
void lastBits(Weather array[])
{
short max_pos, min_pos;
max_min_temp(array, max_pos, min_pos);
cout << "Now to show the last bits of the year. " << endl << endl;
cout << "Average rainfall: " << arrayAverage(array, "rain") << endl<< "Highest temperature: " << array[max_pos].hightemp<< " (on " << MONTHS[max_pos] << ")" << endl<< "Lowest temperature: " << array[min_pos].lowtemp<< " (on " << MONTHS[min_pos] << ")" << endl<< "Average temperature: " << arrayAverage(array, "temp") << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.