Question

In: Computer Science

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 code should print "With 10 numbers the average is ..." With 20 numbers the average is... " and so on.

Solutions

Expert Solution

  • Below is the detailed implementation of the above problem in C++ with code and output shown.
  • For better understading please read the comments mentioned in the code.
  • Note: the file name you enter in the input should present in the same directory as that of your C++(.cpp) file so as to read by the program.
  • CODE:

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

//driver function
int main(){
//variable to input file name
string name;
//prompt user to enter the file name
cout<<"Enter the name of the file to be opened: ";
cin>>name;
//create ifstream object
ifstream file;
//open file
file.open(name);
//num used to read in numbers from file
double num;
//sum stores total sum of all numbers
double sum=0;
//total stores count of numbers in file
int total=0;
//while EOF(end of file)
while(file>>num){
//increment count
total+=1;
//add to sum
sum+=num;

//if total number are multiple of 10 then print average
if(total%10==0){
double avg=sum/(double)total;
cout<<"With "<<total<<" numbers the average is "<<avg<<"."<<endl;
}
}
//calculate the average of all the numbers
double avg=sum/(double)total;
//print the total sum , count and average of all the numbers.
cout<<"There are total "<<total<<" numbers in the file and sum of all numbers is "<<sum<<" and their average is "<<avg<<"."<<endl;
return 0;
}

  • INPUT/OUTPUT:

Enter the name of the file to be opened: input.txt
With 10 numbers the average is 6.25.
With 20 numbers the average is 11.54.
There are total 22 numbers in the file and sum of all numbers is 246.1 and their average is 11.1864.

  • Below are the screenshot attached for the code and input/output for better clarity and understanding.

CODE

INPUT/OUTPUT

So if you still have any doubt regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.


Related Solutions

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...
C++ . It should all be in one code and using WHILE loops 2. Write a...
C++ . It should all be in one code and using WHILE loops 2. Write a piece of code that asks the user for a number and adds up the even numbers from 1 to that entered number. 3. Write a piece of code that asks the user for 2 numbers and adds up the numbers between and including the numbers. 4. Write another piece of code that asks the user for a random file name and adds all of...
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.
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.
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...
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...
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In...
Code in Python. You can only use while loops NOT for loops. Program 1: cost_living In 2020 the average cost of living/month (excluding housing) for a family of 4 in Pittsburgh was $3850 per month. Write a program to print the first year in which the cost of living/month is over $4450 given that it will rise at a rate of 2.1% per year. (Note:  this program requires no input). Program 2: discount A discount store is having a sale where...
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...
Part 1 Ask for a file name. The file should exist in the same folder as...
Part 1 Ask for a file name. The file should exist in the same folder as the program. If using input() to receive the file name do not give it a path, just give it a name like “number1.txt” or something similar (without the quotes when you enter the name). Then ask for test scores (0-100) until the user enters a specific value, in this case, -1. Write each value to the file as it has been entered. Make sure...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT