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
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream ifs("numbers.txt");
int total = 0;
int cnt = 0;
int x;
while (!ifs.eof()) {
ifs >> x;
total += x;
cnt++;
}
double avg = static_cast<double>(total) / cnt;
cout << "Total = " << total << endl;
cout << "Average = " << avg << endl;
cout << "Total * 2 = " << total*2 << endl;
return 0;
}
Comments
Leave a comment