Write a program using structures to store the following weather information: Tot
ID: 3682767 • Letter: W
Question
Write a program using structures to store the following weather information: Total Rainfall High Temperature Low Temperature Average Temperature The program should have an 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
Explanation / Answer
#include <iostream>
using namespace std;
const short months = 12; //the constant size used for all of the arrays
char MONTHS[months][10] = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December" } ; //months of the year
//the data type used for the rest of the program
struct Weather
{
double rainfall; //stores the rainfall
double hightemp; //stores the highest temperature
double lowtemp; //stores the lowest temperature
double avgtemp; //stores the average temperature
};
//Function prototypes
void setAverage(Weather [], int);
double arrayAverage(Weather [], char[]);
void max_min_temp(Weather[], short&, short&);
void getData(Weather []);
void lastBits(Weather[]);
int main()
{
//declares the array of weather for each month
Weather array[months];
getData(array); //stores and shows the data to the user
//shows the average rainfall, average temperature,
//highest temperature, and lowest temperature of the year
lastBits(array);
return 0;
}
//Function that calculates the avgtemp from the structure
void setAverage(Weather array[], int i)
{
array[i].avgtemp = (array[i].hightemp + array[i].lowtemp) / 2;
}
//Function that calculates the average of the Weather array depending
//on the choice of string
double arrayAverage(Weather array [], char choice[])
{
double average = 0; //stores the average
//the choice for average temperature
if(choice == "temp")
{
//standard loop that sums all the elements in the array
for(int i = 0; i < months; i++)
average += array[i].avgtemp;
//divides by the months to get the average
return (average / months);
}
//the choice for average rainfall
if(choice == "rain")
{
//standard loop that sums all the elements in the array
for(int i = 0; i < months; i++)
average += array[i].rainfall;
//divides by the months to get the average
return (average / months);
}
}
//Function that looks for the lowest and highest temperature in the
//Weather array, the reference parameters are used in the lastBits function
void max_min_temp(Weather array [], short & max_pos, short & min_pos)
{
//assumes the minimum and maximum are the first elements
double min = array[0].lowtemp, max = array[0].hightemp;
min_pos = 0;
max_pos = 0;
//loop that determines the maximum and minimum temperatures in the array
for(int i = 1; i < months; i++)
{
//changes the min to a smaller value and updates the position
if(min > array[i].lowtemp)
{
min = array[i].lowtemp;
min_pos = i;
}
//changes the max to a higher value and updates the position
if(max < array[i].hightemp)
{
max = array[i].hightemp;
max_pos = i;
}
}
}
//Starting function of the program
//Asks the user to input the highest and lowest temperatures
//for the respectives months
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;
//loop that fills the array with the user data
for(int i = 0; i < months; i++)
{
//the respective month
cout << "From " << MONTHS[i] << ": " << endl << endl;
cout << "Total rainfall: ";
cin >> array[i].rainfall;
//validation in case the user puts a negative value
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;
//validation in case the user puts a temperature higher than 140
//or less than -100 degrees Fahrenheit
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;
//validation in case the user puts a temperature higher than 140
//or less than -100 degrees Fahrenheit or inputs a lowest temperature
//that is higher than the highest temperature
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;
}
//calcultes the avtemp from the structure Weather
setAverage(array, i);
cout << endl;
}
}
//Function that shows the average rainfall, average temperature,
//highest temperature, and lowest temperature of the year
void lastBits(Weather array[])
{
short max_pos, min_pos; //the position for their respective roles
max_min_temp(array, max_pos, min_pos); //gets both positions
cout << "Now to show the last bits of the year. " << endl << endl;
//Shows the last part of the data(the yearly data)
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;
}
#include <iostream>
using namespace std;
const short months = 12; //the constant size used for all of the arrays
char MONTHS[months][10] = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December" } ; //months of the year
//the data type used for the rest of the program
struct Weather
{
double rainfall; //stores the rainfall
double hightemp; //stores the highest temperature
double lowtemp; //stores the lowest temperature
double avgtemp; //stores the average temperature
};
//Function prototypes
void setAverage(Weather [], int);
double arrayAverage(Weather [], char[]);
void max_min_temp(Weather[], short&, short&);
void getData(Weather []);
void lastBits(Weather[]);
int main()
{
//declares the array of weather for each month
Weather array[months];
getData(array); //stores and shows the data to the user
//shows the average rainfall, average temperature,
//highest temperature, and lowest temperature of the year
lastBits(array);
return 0;
}
//Function that calculates the avgtemp from the structure
void setAverage(Weather array[], int i)
{
array[i].avgtemp = (array[i].hightemp + array[i].lowtemp) / 2;
}
//Function that calculates the average of the Weather array depending
//on the choice of string
double arrayAverage(Weather array [], char choice[])
{
double average = 0; //stores the average
//the choice for average temperature
if(choice == "temp")
{
//standard loop that sums all the elements in the array
for(int i = 0; i < months; i++)
average += array[i].avgtemp;
//divides by the months to get the average
return (average / months);
}
//the choice for average rainfall
if(choice == "rain")
{
//standard loop that sums all the elements in the array
for(int i = 0; i < months; i++)
average += array[i].rainfall;
//divides by the months to get the average
return (average / months);
}
}
//Function that looks for the lowest and highest temperature in the
//Weather array, the reference parameters are used in the lastBits function
void max_min_temp(Weather array [], short & max_pos, short & min_pos)
{
//assumes the minimum and maximum are the first elements
double min = array[0].lowtemp, max = array[0].hightemp;
min_pos = 0;
max_pos = 0;
//loop that determines the maximum and minimum temperatures in the array
for(int i = 1; i < months; i++)
{
//changes the min to a smaller value and updates the position
if(min > array[i].lowtemp)
{
min = array[i].lowtemp;
min_pos = i;
}
//changes the max to a higher value and updates the position
if(max < array[i].hightemp)
{
max = array[i].hightemp;
max_pos = i;
}
}
}
//Starting function of the program
//Asks the user to input the highest and lowest temperatures
//for the respectives months
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;
//loop that fills the array with the user data
for(int i = 0; i < months; i++)
{
//the respective month
cout << "From " << MONTHS[i] << ": " << endl << endl;
cout << "Total rainfall: ";
cin >> array[i].rainfall;
//validation in case the user puts a negative value
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;
//validation in case the user puts a temperature higher than 140
//or less than -100 degrees Fahrenheit
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;
//validation in case the user puts a temperature higher than 140
//or less than -100 degrees Fahrenheit or inputs a lowest temperature
//that is higher than the highest temperature
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;
}
//calcultes the avtemp from the structure Weather
setAverage(array, i);
cout << endl;
}
}
//Function that shows the average rainfall, average temperature,
//highest temperature, and lowest temperature of the year
void lastBits(Weather array[])
{
short max_pos, min_pos; //the position for their respective roles
max_min_temp(array, max_pos, min_pos); //gets both positions
cout << "Now to show the last bits of the year. " << endl << endl;
//Shows the last part of the data(the yearly data)
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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.