Overview For this assignment, you will work alone to write a program to process
ID: 3778919 • Letter: O
Question
Overview For this assignment, you will work alone to write a program to process weather station data. This is a substantial program that uses loops and conditions. It will take time to develop. Develop it in small pieces a little bit at a time.
Overall Specifications For this lab, you will create a C++ program that summarizes weather information supposedly obtained from your home weather station. The program will process several lines of information where each line of three real values represents one day. A day's data will be a low temperature, a high temperature, and a rain total. The file will be terminated by end-of-file, but there will be no partial lines. Your program will output the number of days in the reporting period, the maximum high and minimum low temperatures, the mean (average) high and low temperatures, total rain during the period, and number of days with rain.
Input Specification This program will input from a text file. The input consists of three values per line: low temperature (int), high temperature (int), and rain (double). Each line corresponds to one reporting day. The input will be terminated by end-of-file. Temperatures are in Fahrenheit. Rain is in inches. Rain will be non-negative. Consider the possibility of very high lows and very low highs within. An initial very high value for a minimum will be replaced by the first actual input value. You will input data from a file. To do this, create a file named wet.txt with the appropriate data in your project folder. You don't need a special character at the end. Just type the data as if you were typing it to your program and save the file. When I test your program, I will be using a data file named wet.txt.
Output Specification The output will be a summary of the weather information. You should NOT echo the input data nor prompt for input. The output will be: • the number of reporting days (integer); if zero days, say so and don't report other info • the maximum high and minimum low temperatures (integer) • the mean (average) high and low temperatures (real; one decimal place) • the total rain in the period (real; one decimal place) • the number of days with rain (integer)
Approaching the Problem Develop your solution incrementally. I suggest the following initial iterations:
• set up a loop to read the data and count the number of days
• total the rain
• count days with rain
• compute means
• determine maximum high and minimum low
Sample Input and Output
30 60 0.3
25 55 0
30 65 0
Days reported = 3
Min Temp = 25
Max Temp = 65
Avg Low = 28.3
Avg High = 60.0
Total rain = 0.3
Number of rainy days = 1
Explanation / Answer
SOURCE CODE:
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
#include<cstring>
#include<sstream>
using namespace std;
int main()
{
int days=0,rainy=0,minTemp=0,maxTemp=0;
double avgLow=0.0,avgHigh=0.0,totalRain=0.0,t4=0.0;
int t1=0,t2,t3;
string temp[3],line="";
ifstream myfile("wet.txt");
if (myfile.is_open())
{
while(getline (myfile,line) )
{
days++;
for(int i=0;i<line.length();i++)
{
temp[t1]=temp[t1]+line.at(i);
if(line.at(i)==' ')
t1++;
}
t2 = atoi(temp[0].c_str());
t3 = atoi(temp[1].c_str());
t4 = atof(temp[2].c_str());
if(maxTemp<t3)
maxTemp = t3;
if(days == 1)
minTemp = t2;
if(minTemp>t2)
minTemp = t2;
if(t4>0.0)
rainy++;
totalRain = totalRain + t4;
avgLow = avgLow + t2;
avgHigh = avgHigh + t3;
line="";
t1=0;
t2=0;
t3=0;
t4=0.0;
temp[0]="";
temp[1]="";
temp[2]="";
}
avgLow = (double) avgLow / days;
avgHigh= (double) avgHigh/ days;
cout<<" Days reported = "<<days;
if(days>0)
{
cout<<" Min Temp = "<<minTemp;
cout<<" Max Temp = "<<maxTemp;
printf(" Avg Low = %.1f",avgLow);
printf(" Avg High = %.1f",avgHigh);
printf(" Total rain = %.1f",totalRain);
cout<<" Number of rainy days = "<<rainy;
}
myfile.close();
}
return 1;
}
SAMPLE OUTPUT:
wet.txt
30 60 0.3
25 55 0
30 65 0
OUTPUT
Days reported = 3
Min Temp = 25
Max Temp = 65
Avg Low = 28.3
Avg High = 60.0
Total rain = 0.3
Number of rainy days = 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.