+ 1

Plz solve this

Write a program that an unspecified number of positive integer input, and the mean is calculated and published them ( Input -1 is ended)

26th Mar 2017, 9:18 AM
fateme fadaee
3 odpowiedzi
+ 7
int i = 0, a[10], size, sum, mean; do { cin >> a[i]; i++; } while (a[i - 1] != 1); size = i - 1; sum = 0; for (int j = 0; j < size; j++) sum += j; mean = sum / size;
26th Mar 2017, 11:11 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 3
The example from Krishna will only allow for a maximum of 10 numbers to be entered. I would recommend using a vector, or maybe the new keyword. For example: vector<int> intVect; // #include <vector> while (true) { int n; cin >> n; if (n == -1) { break; } else { intVect.push_back(n); } } int sum = 0; for (int i = 0; i < intVect.size(); i++) { sum += intVect[i]; } cout << "Sum: " << sum << endl; cout << "Mean: " << sum/intVect.size() << endl; I hope this helps. Happy coding!
10th Apr 2017, 4:12 AM
Zeke Williams
Zeke Williams - avatar
0
this question sounds like school homework being asked here
9th Jan 2018, 11:07 AM
crypto hacker
crypto hacker - avatar