In: Computer Science
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
#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...