Question

Your next programming assignment at the Research Center is to write a program that reads data...

Your next programming assignment at the Research Center is to write a program that reads data from a file and produces a report of rainfall for that period of time. The program should read the starting month name from the data file, then read an ending month name, followed by reading in monthly rainfall amounts for each of the months from the starting month through the ending month. As it reads the monthly totals, it should sum (accumulate) the rainfall amounts and then eport the total rainfall and average rainfall for the period. For example, if the starting month name is January and the ending month is March, and the rainfall amounts are 2.55, 3.45 and 4.0, then th eoutput might look like this: During the months of January-March, the total rainfall was 10.00 inches and the average monthly rainfall was 3.33 inches. Data for the program is located in the file attached below (rainfall.txt). You may modify this data file to include more months, but make sure that you have a rainfall amount for each month (if you use January and December for the month names, you MUST have 12 rainfall amounts in your data file. Hint: After reading in the two month names, you will need to read in rain amounts until the EOF is reached and, as you read in each amount, add 1 to the count of how many pieces of rain data you read in. This will help with the average calculation. DO NOT assume there will always be 5 values in the file.

rainfall.txt

ouput:

April
August
1.16 2.02 1.34 1.51 .30

C++ programming language

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;

int main()
{
   ifstream in;
   string startMonth, endMonth;
   double sum=0, avg, rainFall;
   int count=0;

   in.open("rainfall.txt");

   if (!in)
   {
       cout << "ERROR: Can't opent the file. Exiting....." << endl;
       return 0;
   }
   cout << fixed << setprecision(2);
   in >> startMonth;
   in >> endMonth;
   while (!in.eof())
   {
       in >> rainFall;
       sum += rainFall;
       count++;
   }
   avg = sum / count;
   cout << "During the months of " << startMonth << "-" << endMonth << " the total rainfall was " << sum << " inches and the average monthly rainfall was " << avg << " inches" << endl;;
   system("pause");
   return 1;
}

outputs

rainfall.txt

output

rainfall.txt

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Your next programming assignment at the Research Center is to write a program that reads data...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • [C++] Using Files—Total and Average Rainfall Write a program that reads in from a file a...

    [C++] 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...

  • qbasic is the programming language Chapter 8 Do Programming Exercise 8-3 om page 414. Create a...

    qbasic is the programming language Chapter 8 Do Programming Exercise 8-3 om page 414. Create a string array to hold the months of the year. Call it anything you want to just make sure it's a string array is at the end of the name, e.g. months). Create a DATA statement that holds the month names: DATA "jan","Feb", "Mar" etc. Next you'll need a FOR loop to READ the DATA into the array, Call a SUB to get the rainfall...

  • use vc++ to complete the programming project.... Number Array Version 1 (all interactive). Write a program...

    use vc++ to complete the programming project.... Number Array Version 1 (all interactive). Write a program that reads in the average monthly rainfall for a city for each month of the year and then reads in the actual monthly rainfall for each of the previous 12 months. The program then prints out a nicely formatted table showing the rainfall for each of the previous 12 months as well as how much above or below average the rainfall was for each...

  • how to make this program for c++ visual studio 2015. Also, can I show your working...

    how to make this program for c++ visual studio 2015. Also, can I show your working and program. Also, Can you have notice for pseudo code? For the first problem, please implement Problem 4 on page 142 (p 143, 7E) of the text. A scan of the problem is provided below. This problem asks you to calculate the average rainfall for three months. The program should ask the user to enter the name of each month, such as June or...

  • Write a program that scores the total rainfall for each of 12 months into an array...

    Write a program that scores the total rainfall for each of 12 months into an array double. A sample array definition is shown below double thisYear[] = {1.6, 2.1, 1.7, 3.5, 2.6, 3.7, 3.9, 2.6, 2.9, 4.3, 2.4, 3.7}; The program should have the four functions to calculate the following 1. The total rainfall for the year 2. The average monthly rainfall 3. The month with most rain 4. The month with least rain Output: What to deliver? Your .cpp...

  • I was wondering if I could get some help on the last program I need to...

    I was wondering if I could get some help on the last program I need to complete for an engineering C++ class. It is a very simple program however there is one part of the problem stumping me. Here is the problem statement . Write a C++ program that reads in from a file two month names, followed by the rainfall amounts for each month in that span of months. These rain amounts are totaled, and then an average for...

  • starting out with c++ early objects 7th ed. chapter 8 programming challenges #7

    Rainfall StatisticsWrite a modular program that analyzes a year's worth of rainfall data. In addition to main, the program should have a getData function that accepts the total rainfallfor each of 12 months from the user, and stores it in a double array. It should also have four value-returning functions that compute and return to main thetotalRainfall, averageRainfall, driestRainfall, and wettestMonth. These last two functions return the number of the month with the lowest and highest rainfall amounts,not the amount...

  • Write a Python program that allows the user to enter the total rainfall for each of...

    Write a Python program that allows the user to enter the total rainfall for each of 12 months into a list. 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. Data: January: 8.9 inches February: 11.1 inches March: 13.4 inches April: 6.9 inches May: 8.7 inches June: 9.1 inches July: 15.9 inches August: 4.4 inches September: 3.1 inches October: 5.6 inches November: 7.8...

  • Rainfall Statistics Write a program that lets the user enter the total rainfall for each of...

    Rainfall Statistics Write a 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, December....

  • PYTHON PROGRAMMING - CODE THE PROGRAM BASED ON THE INSTRUCTIONS AND ALGORITHM PROVIDED home / study...

    PYTHON PROGRAMMING - CODE THE PROGRAM BASED ON THE INSTRUCTIONS AND ALGORITHM PROVIDED home / study / engineering / computer science / questions and answers / python programming - code the program based on ... Your question has been answered! Rate it below. Let us know if you got a helpful answer. Question: PYTHON PROGRAMMING - CODE THE PROGRAM BASED ON THE... Bookmark PYTHON PROGRAMMING - CODE THE PROGRAM BASED ON THE INSTRUCTIONS AND ALGORITHM PROVIDED Rainfall Algorithm Declare and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT