C++ Write a program using structures to store the following weather information:
ID: 3690072 • 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
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
struct weather{
double totalRainfall;
double high;
double low;
double average;
};
int main(){
ifstream inFile("input.txt");
weather arr[12];
int i = 0;
double avgMonthly = 0;
double total;
double highest = -100;
double lowest = 140;
while(inFile >> arr[i].totalRainfall){
inFile >> arr[i].high;
inFile >> arr[i].low;
inFile >> arr[i].average;
total += arr[i].totalRainfall;
if(highest < arr[i].high) highest = arr[i].high;
if(lowest > arr[i].low) lowest = arr[i].low;
i++;
}
avgMonthly /= i;
cout << "Average Monthly rainfall: " << avgMonthly << " ";
cout << "Total rainfall: " << total << " ";
cout << "Highest rainfall: " << highest << " ";
cout << "Lowest rainfall: " << lowest << " ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.