+ 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)
3 Antworten
+ 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;
+ 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!
0
this question sounds like school homework being asked here