Question

In: Computer Science

using C++ 24. Using Files—Total and Average Rainfall Write a program that reads in from a...

using C++

24. Using Files—Total and Average Rainfall
Write a program that reads in from a file a starting month name, an ending month name,
and then the monthly rainfall for each month during that period. As it does this, it should
sum the rainfall amounts and then report the total rainfall and average rainfall for the
period. For example, the output might look like this:
During the months of March–June the total rainfall was 7.32 inches and the average
monthly rainfall was 1.83 inches.
Data for the program can be found in the Rainfall.txt file.
Hint: After reading in the month names, you will need to read in rain amounts until
the EOF is reached, and count how many pieces of rain data you read in.

You can use note pad to create a Rainfall.txt with the following data:

January
May
1.35 2.15 3.03 4.41 5.41

Please note that you must read the first line of the file as starting month and second line as ending month. Please do not hard code the starting and ending month into your program.

test case

During the months of January-May the total
rainfall was 16.35 inches and the average monthly
rainfall was 3.27 inches.

Solutions

Expert Solution

Thanks for the question, here is the simple C++ program to read from the file and displays the total and average rainfalls

When you run the program, please update the below line for the file name path in your system

char * filename ="Rainfall.txt";

else it wont be able to read the file and display the output.

=====================================================================

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

using namespace std;

int main(){
  
   char * filename ="F:\\Rainfall.txt";
  
   ifstream infile(filename);
   double total_rainfall=0;
   int months=0;
   string startMonth;
   string endMonth;
   if(infile.is_open()){
      
  
       double rainfall;
       infile>>startMonth>>endMonth;
       while(infile>>rainfall){
           total_rainfall+=rainfall;
           months+=1;
       }
       infile.close();
      
   }else{
       cout<<"Unable to read data from file: "<<filename<<endl;
       exit(EXIT_FAILURE);
   }
  
   cout<<"During the months if "<<startMonth<<"-"
   <<endMonth<<" the total \nrainfall was "
   <<fixed<<showpoint<<setprecision(2)
   <<total_rainfall<<" inches and the average monthly \nrainfall was "
   <<total_rainfall/months<<" inches."<<endl;
}


Related Solutions

In c++ please. Write a program that reads the contents of two text files and compares...
In c++ please. Write a program that reads the contents of two text files and compares them in the following ways: It should display a list of all the unique words contained in both files. It should display a list of the words that appears in both files. It should display a list of the words that appears in the first file, but not the second. It should display a list of the words that appears in the second file,...
-In C Programming- Write a program to display the total rainfall for a year. In addition,...
-In C Programming- Write a program to display the total rainfall for a year. In addition, display the average monthly rainfall, and the months with the lowest and highest rainfall. Create an array to hold the rainfall values. Create a 2nd parallel array (as a constant) to hold the abbreviated names of the months. I created my arrays to be 1 element bigger than needed, and then disregarded element [0] (so that my months went from [1] = "Jan" to...
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
Write a C++ program that lets the user enter the total rainfall for each of 12...
Write a C++ program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubles. The program should calculate and display (in this order): the total rainfall for the year,     the average monthly rainfall,     and the months with the highest and lowest amounts. Months should be expressed as English names for months in the Gregorian calendar, i.e.: January, February, March, April, May, June, July, August, September, October, November,...
write these programs in c++ using getch() function Write a program that reads 20 integers from...
write these programs in c++ using getch() function Write a program that reads 20 integers from a user using a while loop and determines and prints whether each of those integers is an even number. Write a program that uses a while statement to read integers from a user and prints the sum, average, and largest of these numbers. The input should terminate if the user enters -1.
Using C++ Write a program that reads a text from the keyboard until the user presses...
Using C++ Write a program that reads a text from the keyboard until the user presses the “Enter” key. Your program must count the number of uppercase alphabets (A through Z), lowercase alphabets (a through z), digits (0 through 9) and any other characters. The other character count value should NOT include the “Enter” key. Display the count on the screen. You must use the in-built C++ character I/O functions in this program.
The Sum and The Average In C++, Write a program that reads in 10 integer numbers....
The Sum and The Average In C++, Write a program that reads in 10 integer numbers. Your program should do the following things: Use a Do statement Determine the number positive or negative Count the numbers of positive numbers, and negative Outputs the sum of: all the numbers greater than zero all the numbers less than zero (which will be a negative number or zero) all the numbers Calculate the average of all the numbers. The user enters the ten...
Write a C++ program that reads in a table of numbers and finds out the average...
Write a C++ program that reads in a table of numbers and finds out the average of each row and column. This program should first take two numbers as number of rows and columns as input from the user
How do I create this program? Using C++ language! Write a program that reads data from...
How do I create this program? Using C++ language! Write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global varibles are the mean, standard deviation, and the number of data entered. All other varibles must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function....ALL...
Write a C++ program that uses nested loops to collect data and calculate the average rainfall...
Write a C++ program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT