Answer to Question #293575 in C++ for Ianidy

Question #293575

Make a C ++ program that calculates the sum, mean of a series of numbers.



Example:



-Numbers: 10, 12, 10, 14



-The sum is 46,



-the mean is 11.5




1
Expert's answer
2022-02-03T03:59:39-0500
#include <iostream>
#include <sstream>
using namespace std;


int main() {
    int x, sum=0, n=0;
    char ch;

    string str;
    cout << "Numbers: ";
    getline(cin, str);

    istringstream iss(str);
    while (iss) {
        if (iss >> x) {
            sum += x;
            n++;
        }
        iss >> ch;
    }

    cout << "The sum is " << sum << endl;
    double mean = static_cast<double>(sum) / n;
    cout << "The mean is " << mean << endl;

}

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

LATEST TUTORIALS
New on Blog