C++ Program Write a program using structures to store the following weather info
ID: 3687333 • Letter: C
Question
C++ Program
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
Explanation / Answer
Here is the code for you:
#include <iostream>
#include <fstream>
using namespace std;
struct Weather
{
double totalRainfall;
double highTemperature;
double lowTemperature;
double avgTemperature;
};
double AverageMonthlyRainfall(struct Weather d[])
{
double sum = 0;
for(int i = 0; i < 12; i++)
sum += d[i].totalRainfall;
return sum / 12;
}
double AverageMonthlyTemperature(struct Weather d[])
{
double sum = 0;
for(int i = 0; i < 12; i++)
sum += d[i].avgTemperature;
return sum / 12;
}
int highTempInAYear(struct Weather d[])
{
int index = 0;
for(int i = 1; i < 12; i++)
if(d[i].highTemperature > d[index].highTemperature)
index = i;
return index;
}
int lowTempInAYear(struct Weather d[])
{
int index = 0;
for(int i = 1; i < 12; i++)
if(d[i].lowTemperature < d[index].lowTemperature)
index = i;
return index;
}
int main()
{
struct Weather data[12]; //An array of 12 structures;
ifstream inStream; //Declares an input stream.
inStream.open("input.txt"); //Opens the file input.txt with input stream.
int count = 0; //Initializes the number of records to 0.
while(!inStream.eof()) //As long as there is data in the file.
{
inStream>>data[count].totalRainfall; //Reads the rainfall.
inStream>>data[count].highTemperature; //Reads the high temperature.
inStream>>data[count].lowTemperature; //Reads the low temperature.
data[count].avgTemperature = (data[count].highTemperature + data[count].lowTemperature) / 2; //Calculates the avg temperature.
count++; //Increases the record count.
}
cout<<"The average monthly rainfall is: "<<AverageMonthlyRainfall(data)<<endl; //Calculates and displays the average monthly rainfall.
int highIndex = highTempInAYear(data);
cout<<"The highest monthly temperature in a year is: "<<data[highIndex].highTemperature<<" and it happened in the month "<<highIndex+1<<"."<<endl; //Calculates and prints the highest temperature in a year.
int lowIndex = lowTempInAYear(data);
cout<<"The lowest monthly temperature in a year is: "<<data[lowIndex].lowTemperature<<" and it happened in the month "<<lowIndex+1<<"."<<endl; //Calculates and prints the lowest temperature in a year.
cout<<"The average temperature of the year is: "<<AverageMonthlyTemperature(data)<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.