Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ While Loops ( weather.cpp ) Goals: • to use a sentinel controlled loop • to

ID: 3779259 • Letter: C

Question

C++

While Loops (weather.cpp)

Goals:

• to use a sentinel controlled loop

• to use functions

• to read to end of file

• to read from a text file

Weather

Overview
For this assignment, you will write a program to process weather station data.

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

// C++ code
#include <iostream>
#include <string.h>
#include <fstream>
#include <math.h> /* asin */
#include <limits.h>

using namespace std;


int main()
{
   double low, high, rain;
   double minTemp = INT_MAX, maxTemp = INT_MIN, avgMinTemp = 0, avgmaxTemp = 0;
   double totalRain = 0;
   int rainyDays = 0;
   int totalDays = 0;

   ifstream myfile ("input.txt");
   if (myfile.is_open())
   {
   while ( true )
   {

       myfile >> low >> high >> rain;

       if(low < minTemp)
           minTemp = low;
       if(high > maxTemp)
           maxTemp = high;
       if(rain > 0)
       {
           totalRain = totalRain + rain;
           rainyDays++;
       }

       avgMinTemp = avgMinTemp + low;
       avgmaxTemp = avgmaxTemp + high;
       totalDays++;

   if(myfile.eof() == true)
       break;
   }
   myfile.close();
   }
   else cout << "Unable to open file";
  
   avgMinTemp = avgMinTemp/totalDays;
   avgmaxTemp = avgmaxTemp/totalDays;

   cout << "Days reported = " << totalDays << endl;
   cout << "Min Temp = " << minTemp <<endl;
   cout << "Max Temp = " << maxTemp <<endl;
   cout << "Avg Low = " << avgMinTemp << endl;
   cout << "Avg High = " << avgmaxTemp << endl;
   cout << "Total rain = " << totalRain << endl;
   cout << "Number of rainy days = " << rainyDays << endl;

   return 0;
}


/*
output:

Days reported = 3
Min Temp = 25
Max Temp = 65
Avg Low = 28.3
Avg High = 60
Total rain = 0.3
Number of rainy days = 1


*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote