Answer to Question #310576 in C++ for nickname

Question #310576

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


1
Expert's answer
2022-03-13T03:33:18-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment