Question

In: Computer Science

This program will read a bunch of numbers from an external file (numbers.txt) and calculate their...

This program will read a bunch of numbers from an external file (numbers.txt) and calculate their total and average. In addition, the total will be multiplied by an integer that is supplied by the user.

** IMPORTANT: The numbers are included in your starter code as a separate file (numbers.txt). Don't touch that file! All of your code should be placed in code.cpp, as usual. **

Input:

After all numbers are read, what number would you like to multiply the total by? [user types: 2]

Output:

Total = 1717
Average = 50.5
Total * 2 = 3434

Notes and Hints:

1) Average is the total divided by the quantity of numbers that you read from the external file. How do we normally keep track of how many times something has happened?

2) Note that the number in Total * 2 matches what the user entered

numbers.txt

1
4
7
10
13
16
19
22
25
28
31
34
37
40
43
46
49
52
55
58
61
64
67
70
73
76
79
82
85
88
91
94
97
100

Solutions

Expert Solution

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

int main(){
   int total = 0, numbers;
   int counter = 0;
   double avg;
   int multiply;
   ifstream readfile;
   readfile.open("numbers.txt");
   cin>>multiply;
   while(readfile>>numbers){
       total = total + numbers;
       counter++;
   }
   avg = (double)total/counter;
   cout<<"Total = "<<total<<endl;
   cout<<"Average = "<<avg<<endl;
   cout<<"Total * "<<multiply<<" = "<<total*multiply<<endl;
  
   return 0;
}

Screenshot of the program

Output

Output 2

numbers.txt

NOTE: If you want to change something , please let me know through comments; I will surely revert back to you.

Please give a up vote .....
Thank you...


Related Solutions

Write a program that processes numbers, corresponding to student records read in from a file, and...
Write a program that processes numbers, corresponding to student records read in from a file, and writes the required results to an output file (see main ( )). Your program should define the following functions: double read_double (FILE *infile) — Reads one double precision number from the input file. Note: You may assume that the file only contains real numbers. int read_integer (FILE *infile) - Reads one integer number from the input file. double calculate_sum (double number1, double number2, double...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat (posted on Canvas)and store it into an array. The number of values in the file is less than 300 and all the values are whole numbers. The actual number of values stored in the file should be determined. Your program should then prompt the user to enter another whole number between 2 and 20 (you may assume the user enters a valid value) and...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided above. The program should sum all the numbers, find the lowest number, find the highest number, and computer the average. Upon completion of the processing, the program should write a new text file named stats.txt with the information found in the following format where xxx represents a number calculated above. The sum of the numbers is: xxx The lowest number is: xxx The highest...
How do I do this: Write a program that can read a text file of numbers...
How do I do this: Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT