Question

In: Computer Science

C++ Code While Loops. Ask user for file and open file. Do priming read and make...

C++ Code While Loops.

Ask user for file and open file.

Do priming read and make a while loop that: 1. Reads in numbers. 2. Counts how many there is. 3. Also for every 10 numbers in the file, print out the average of those 10 numbers. Ex: (If 20 numbers in the file. "With 10 numbers the average is .... and With 20 numbers the average is" and EX for a file with 5 numbers "There are 5 numbers in this file with an average of ..."

After the loop calculate and print the average of all nums in the file.

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

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

using namespace std;

int main(){
  
   string filename;
   cout<<"Enter filename: ";
   getline(cin,filename,'\n');
  
   ifstream infile(filename.c_str());
   if(infile.bad() || !infile.is_open()){
       cout<<"Error: Could not open/read file: "
           <<filename<<endl;
           return 1;
   }
  
   int count = 0;
   double totalAll = 0;
   double num;
  
   while(infile>>num){
       count+=1;
       totalAll+=num;
       if(count%10==0){
           cout<<setprecision(2)<<fixed<<showpoint;
           cout<<"With "<<count<<" numbers the average is "<<totalAll/count<<endl;
          
       }
   }
  
   cout<<"There are "<<count;
   cout<<setprecision(2)<<fixed<<showpoint;
   cout<<" numbers in the file with an average of "<<totalAll/count<<endl;
   infile.close();

   return 0;
}


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


Related Solutions

C++ Code while loops. 1. Ask for file name and open file. 2. Do a priming...
C++ Code while loops. 1. Ask for file name and open file. 2. Do a priming read and make a while loop that A) Reads in numbers B) Sums them up. C) Counts how many there are. D) Prints "With 10 numbers, the average is blank.." every 10 numbers. So with every 10 numbers read in print the average. 3. After the loop calculate average of all numbers and print it. So for example once the file is open the...
Write a program that uses a while loop with a priming read to ask the user...
Write a program that uses a while loop with a priming read to ask the user to input a set positive integers. As long as the user enters a number greater than -1, the program should accumulate the total, keep track of the number of numbers being entered and then calculate the average of the set of numbers after the user enters a -1. This is a sentinel controlled-loop. Here is what a sample run should look like: Enter the...
Write a series of codes using WHILE loops C++ 1. Ask the user for a number...
Write a series of codes using WHILE loops C++ 1. Ask the user for a number and adds even numbers for 1 to the user entered number. 2. Write another piece of code that asks the user for 2 numbers and adds up the numbers between and including the numbers. 3. Write another piece of code that asks user for a file name and then add up all the numbers for the file.
c++ In this program ask the user what name they prefer for their file, and make...
c++ In this program ask the user what name they prefer for their file, and make a file, the file name should za file. Get a number from the user and save it into the create file. should be more than 20 number in any order. print out all 20 numbers in a sorted array by using for loop, last print out its total and average. At last create a function in which you will call it from main and...
Query the user for the name of a file. Open the file, read it, and count...
Query the user for the name of a file. Open the file, read it, and count and report the number of vowels found in the file. Using C++.
Code in c++, do not use loops, make it as simple as it can be. Thanks!...
Code in c++, do not use loops, make it as simple as it can be. Thanks! Background Well it has finally happened; AMC’s “The Walking Dead” has become a reality. The zombie apocalypse took place. It has been a couple of years since we have started to “rebuild” our society, and now is the time for you to be able to shine. We have come across technology that will allow for us to get back to life as it once...
File IO Java question • Ask the user to specify the file name to open for...
File IO Java question • Ask the user to specify the file name to open for reading • Get the number of data M (M<= N) he wants to read from file • Read M numbers from the file and store them in an array • Compute the average and display the numbers and average.
C++ please 1. Write a do while loop that continually loops until the user enters a...
C++ please 1. Write a do while loop that continually loops until the user enters a single digit between 0 and 9. There should only be one prompt for the user to enter a number inside the loop. 2. Write a simple 4 function calculator using a switch statement. The program should switch on the operator: +, -, *, /. The user should enter two numbers (any type is fine) and then enter an operator and the program should perform...
Ask the user for a filename. In binary format, read all the integers in that file....
Ask the user for a filename. In binary format, read all the integers in that file. Show the number of integers you read in, along with the maximum and minimum integer found. Language is Java. /////////////input///////// n1.dat ////////// output////////// Enter a filename\n Found 50 integers.\n Max: 9944\n Min: 74\n
Please code in c# (C-Sharp) Write a program that will ask the user for their name....
Please code in c# (C-Sharp) Write a program that will ask the user for their name. If the user does not input anything, display a warning before continuing. The program will then ask the user whether they want to have an addition, subtraction, multiplication, or division problem. Once the user indicates their choice, the program will display 2 randomly generated numbers from 1 to 9 in a math problem matching the user’s choice. Example: user selects addition, the equation presented...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT