Question

In: Computer Science

You've been hired by Water Wonders to write a C++ console application that analyzes lake level...

You've been hired by Water Wonders to write a C++ console application that analyzes lake level data. MichiganHuronLakeLevels.txt. Place the input file in a folder where your development tool can locate it (on Visual Studio, in folder \). The input file may be placed in any folder but a path must be specified to locate it.

MichiganHuronLakeLevels.txt Down below:

Lake Michigan and Lake Huron - Average lake levels - 1860-2015

Year    Average level (meters)

1860    177.3351667

1861    177.3318333

1862    177.316

1863    177.1796667

1864    176.9955833

1865    176.90525

1866    176.80575

1867    176.9365833

1868    176.7891667

1869    176.8250833

1870    177.1

1871    177.0769167

1872    176.7318333

1873    176.9188333

1874    177.0413333

1875    176.9683333

1876    177.2855833

1877    177.1971667

1878    177.1183333

1879    176.85325

1880    176.90425

1881    177.0205

1882    177.1250833

1883    177.2096667

1884    177.2734167

1885    177.3208333

1886    177.3893333

1887    177.1890833

1888    176.9931667

1889    176.8393333

1890    176.788

1891    176.6149167

1892    176.564

1893    176.6204167

1894    176.6811667

1895    176.4201667

1896    176.3256667

1897    176.5094167

1898    176.5564167

1898    176.5595

1900    176.5626667

1901    176.64175

1902    176.5305833

1903    176.5748333

1904    176.7460909

1905    176.7561667

1906    176.7635

1907    176.7844167

1908    176.7670909

1909    176.5988333

1910    176.5025

1911    176.3356667

1912    176.4768333

1913    176.67025

1914    176.5297273

1915    176.3535833

1916    176.5715833

1917    176.7980833

1918    176.8866667

1919    176.745

1920    176.625

1921    176.4883333

1922    176.445

1923    176.2641667

1924    176.1866667

1925    175.9191667

1926    175.885

1927    176.1483333

1928    176.4433333

1929    176.8958333

1930    176.6508333

1931    176.1183333

1932    175.9408333

1933    175.8675

1934    175.7666667

1935    175.8908333

1936    175.9391667

1937    175.9225

1938   176.1408333

1939    176.2691667

1940    176.1416667

1941    176.1216667

1942    176.3341667

1943    176.6266667

1944    176.5966667

1945    176.57

1946    176.6016667

1947    176.5666667

1948    176.5308333

1949    176.2108333

1950    176.2608333

1951    176.7358333

1952    177.085

1953    176.9333333

1954    176.8291667

1955    176.7225

1956    176.44

1957    176.2633333

1958    176.0675

1959    176.0058333

1960    176.4775

1961    176.3766667

1962    176.2225

1963    175.9225

1964    175.6825

1965    175.9158333

1966    176.1608333

1967    176.3008333

1968    176.4466667

1969    176.6958333

1970    176.6783333

1971    176.805

1972    176.8883333

1973    177.1233333

1974    177.0933333

1975    176.9733333

1976    176.8991667

1977    176.505

1978    176.5908333

1979    176.7941667

1980    176.8033333

1981    176.6983333

1982    176.5983333

1983    176.8333333

1984    176.895

1985    177.1266667

1986    177.2925

1987    176.97

1988    176.5641667

1989    176.4008333

1990    176.35

1991    176.4691667

1992    176.4791667

1993    176.6958333

1994    176.6783333

1995    176.5275

1996    176.6541667

1997    176.9841667

1998    176.7166667

1999    176.2358333

2000    175.9783333

2001    175.9508333

2002    176.1183333

2003    175.8916667

2004    176.1108333

2005    176.09

2006    176.0158333

2007    175.9433333

2008    176.005

2009    176.2583333

2010    176.1108333

2011    176.0366

2012    175.9158

2013    175.9

2014    176.3016667

2015    176.59

Within the app, attempt to open the input file and output file MichiganHuronLakeLevelsHighAndLow.txt. If the input file didn't open, print an error message. If the output file didn't open, print an error message. Read the input file by scanning past the two header rows. Each detail row in the input file contains two fields (year, lake level). Read one token at a time from the input file. See sample Canvas app Text file input – one token per read. Determine the maximum, minimum, and average lake levels. One technique to accomplish this is to use a max variable that starts very small and a min variable that starts very large. After all lines of the input file have been read, use formatted output manipulators (setw, left/right) to print the following rows:

          ● Column headers.

          ● Max values.

          ● Min values.

          ● Average value.

And columns:

          ● A left-justified label.

          ● A right-justified year.

          ● A right-justified level.

Then write the same information to output file MichiganHuronLakeLevelsHighAndLow.txt. Insure that your code is commented! Provide a complete header comment and body comments. Define constants for the input and output file names and column widths. Format any real numbers to four decimal places. The output should look like this:

Welcome to Wonder Waters

------------------------

Reading lines from file 'MichiganHuronLakeLevels.txt' ...

Writing lines to file 'MichiganHuronLakeLevelsHighAndLow.txt' ...

                      Year    Level (meters)

Max level:               …                 …

Min level:               …                 …

Average level:                             …

158 line(s) read from file 'MichiganHuronLakeLevels.txt'.

4 line(s) written to file 'MichiganHuronLakeLevelsHighAndLow.txt'.

End of Wonder Waters

Solutions

Expert Solution

Here is the answer for your question in C++ Programming Language.

Kindly upvote if you find the answer helpful.

NOTE : As you have the input text file with you and its content is big, I am providing the .cpp file alone here in the answer.

##########################################################################

CODE :

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>

using namespace std;

int main(){
   //Required constant variables
   const string inFileName = "MichiganHuronLakeLevels.txt";
   const string outFileName = "MichiganHuronLakeLevelsHighAndLow.txt";
   const int colWidth = 25;
  
   //Required variables
   string header1,header2;
   int year,minYear,maxYear,count = 0;
   double level,average = 0,minLevel = 200.00,maxLevel = 0.00;
  
   //Create input file object and open input file
   ifstream inFile;
   inFile.open(inFileName.c_str());
  
   //Create output file object and open output file
   ofstream outFile;
   outFile.open(outFileName.c_str());
      
   if(!inFile){
       //If input file cannot be opened display error
       cout << "ERROR : Input File cannot be opened." << endl;
       return 1;
   }else if(!outFile){
       //If output file cannot be opened display error
       cout << "ERROR : Output File cannot be opened." << endl;
       return 1;      
   }else{
       //Writing to console
       cout << "Welcome to Wonder Waters" << endl;      
       cout << "------------------------" << endl;
       cout << "Reading lines from file '" << inFileName << "'..." << endl;
       cout << "Writing lines to file '" << outFileName << "'..." << endl;  
      
       //writing to output file
       outFile << "Welcome to Wonder Waters" << endl;      
       outFile << "------------------------" << endl;
       outFile << "Reading lines from file '" << inFileName << "'..." << endl;
       outFile << "Writing lines to file '" << outFileName << "'..." << endl;  
      
       //Read the first two headers
       getline(inFile,header1);
       count++;  
       getline(inFile,header2);
       count++;  
       //Loop till the end of the file is reached
       while(inFile >> year >> level){          
           //Calculate min value
           if(level < minLevel){
               minLevel = level;
               minYear = year;
           }
           //Calculate max value
           if(level > maxLevel){
               maxLevel = level;
               maxYear = year;
           }
           //Calculate average
           average += level;
           //Increment count by 1
           count++;  
       }
       inFile.close();
       average = average/(count-2);
       //writing to console.
       cout << endl << left << setw(colWidth) << "" << right << setw(0) << "Year" << right << setw(colWidth) << "Level" << endl;
       cout << left << setw(colWidth) << "Max Value" << maxYear << right << setw(colWidth) << fixed << setprecision(4) << maxLevel << endl;
       cout << left << setw(colWidth) << "Min Value" << minYear << right << setw(colWidth) << setprecision(4) << minLevel << endl;
       cout << left << setw(colWidth) << "Average Value" << right << setw(colWidth+4) << setprecision(4) << average << endl << endl;
      
       cout << count << "line(s) read from file '" << inFileName << "'." << endl;
       cout << "4 line(s) written to file '"<< outFileName << "'." << endl << "End of Wonder Waters" << endl;
       //Writing to output file
       outFile << endl << left << setw(colWidth) << "" << right << setw(0) << "Year" << right << setw(colWidth) << "Level" << endl;
       outFile << left << setw(colWidth) << "Max Value" << maxYear << right << setw(colWidth) << fixed << setprecision(4) << maxLevel << endl;
       outFile << left << setw(colWidth) << "Min Value" << minYear << right << setw(colWidth) << setprecision(4) << minLevel << endl;
       outFile << left << setw(colWidth) << "Average Value" << right << setw(colWidth+4) << setprecision(4) << average << endl << endl;
       outFile << count << "line(s) read from file '" << inFileName << "'." << endl;
       outFile << "4 line(s) written to file '"<< outFileName << "'." << endl << "End of Wonder Waters" << endl;
      
   }
   return 0;
}

#####################################################################

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

#################################################################

OUTPUT :

Console output :

MichiganHuronLakeLevelsHighAndLow.txt file

Any doubts regarding this can be explained with pleasure :)


Related Solutions

You've been hired by Water Wonders to write a C++ console application that analyzes lake level...
You've been hired by Water Wonders to write a C++ console application that analyzes lake level data. Place the input file in a folder where your development tool can locate it (on Visual Studio, in folder <project-name>\<project-name>). The input file may be placed in any folder but a path must be specified to locate it. The input file has 158 lines and looks like this: Lake Michigan and Lake Huron - Average lake levels - 1860-2015 Year    Average level (meters)...
You've been hired by Avuncular Addresses to write a C++ console application that analyzes and checks...
You've been hired by Avuncular Addresses to write a C++ console application that analyzes and checks a postal address. Prompt for and get from the user an address. Use function getline so that the address can contain spaces. Loop through the address and count the following types of characters:         ● Digits (0-9)         ● Alphabetic (A-Z, a-z)         ● Other Use function length to control the loop. Use functions isdigit and isalpha to determine the character types. Use formatted...
You've been hired by Yogurt Yummies to write a C++ console application that calculates and displays...
You've been hired by Yogurt Yummies to write a C++ console application that calculates and displays the cost of a customer’s yogurt purchase. Use a validation loop to prompt for and get from the user the number of yogurts purchased in the range 1-9. Then use a validation loop to prompt for and get from the user the coupon discount in the range 0-20%. Calculate the following:         ● Subtotal using a cost of $3.50 per yogurt.         ● Subtotal...
Lab 10-2:You've been hired by Yogurt Yummies to write a C++ console application that calculates and...
Lab 10-2:You've been hired by Yogurt Yummies to write a C++ console application that calculates and displays the cost of a customer’s yogurt purchase. Use a validation loop to prompt for and get from the user the number of yogurts purchased in the range 1-9. Then use a validation loop to prompt for and get from the user the coupon discount in the range 0-20%. Calculate the following:           ● Subtotal using a cost of $3.50 per yogurt. ● Subtotal...
write a c++ code for following application You have been hired by XYZ Car Rental to...
write a c++ code for following application You have been hired by XYZ Car Rental to develop a software system for their business. Your program will have two unordered lists, one of Cars and one of Reservations. The Car class will have the following data: string plateNumber (this is the key) string make string model enum vehicleType (VehicleType Enumeration of options: sedan, suv, exotic) double pricePerDay bool isAvailable isAvailable should be set to true on initialization, and a public setter...
write a c# console application app that reads all the services in the task manager and...
write a c# console application app that reads all the services in the task manager and automatically saves what was read in a Notepad txt file.  Please make sure that this program runs, also add some comments
Loops Write a simple C/C++ console application that will calculate the equivalent series or parallel resistance....
Loops Write a simple C/C++ console application that will calculate the equivalent series or parallel resistance. Upon execution the program will request ether 1 Series or 2 Parallel then the number of resisters. The program will then request each resistor value. Upon entering the last resistor the program will print out the equivalent resistance. The program will ask if another run is requested otherwise it will exit.
Write a C++ console application that allows your user to enter the total rainfall for each...
Write a C++ console application that allows your user to enter the total rainfall for each of 12 months into an array of doubles. The program should validate user input by guarding against rainfall values that are less than zero. After all 12 entries have been made, the program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts.
C# I need working code please Write a console application that accepts the following JSON as...
C# I need working code please Write a console application that accepts the following JSON as input: {"menu": { "header": "SVG Viewer", "items": [ {"id": "Open"}, {"id": "OpenNew", "label": "Open New"}, null, {"id": "ZoomIn", "label": "Zoom In"}, {"id": "ZoomOut", "label": "Zoom Out"}, {"id": "OriginalView", "label": "Original View"}, null, {"id": "Quality"}, {"id": "Pause"}, {"id": "Mute"}, null, {"id": "Find", "label": "Find..."}, {"id": "FindAgain", "label": "Find Again"}, {"id": "Copy"}, {"id": "CopyAgain", "label": "Copy Again"}, {"id": "CopySVG", "label": "Copy SVG"}, {"id": "ViewSVG", "label": "View...
Write a C++ console application to simulate a guessing game. Generate a random integer between one...
Write a C++ console application to simulate a guessing game. Generate a random integer between one and 100 inclusive. Ask the user to guess the number. If the user’s number is lower than the random number, let the user know. If the number is higher, indicate that to the user. Prompt the user to enter another number. The game will continue until the user can find out what the random number is. Once the number is guessed correctly, display a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT