In: Computer Science
The Sum and The Average
In C++,
Write a program that reads in 10 integer numbers. Your program should do the following things:
Use a Do statement
Determine the number positive or negative
Count the numbers of positive numbers, and negative
Outputs the sum of:
all the numbers greater than zero
all the numbers less than zero (which will be a negative number or zero)
all the numbers
Calculate the average of all the numbers.
The user enters the ten numbers just once each and the user can
enter them in any order. Your program should not ask the user to
enter the positive numbers and the negative numbers separately
#include <iostream>
using namespace std;
int main() {
int a[10], i = 0;
int positive = 0, negative = 0, posSum = 0, negSum = 0;
cout << "Enter 10 numbers:\n";
do{
cin >> a[i];
if(a[i] > 0)
{
positive++;
posSum += a[i];
}
else
{
negative++;
negSum += a[i];
}
i++;
}while(i < 10);
cout << "Sum of numbers > 0 = " << posSum
<< endl;
cout << "Sum of numbers <= 0 = " << negSum <<
endl;
cout << "Sum of all numbers = " << posSum + negSum
<< endl;
cout << "Average = " << (posSum + negSum)/10.0;
}
// Hit the thumbs up if you are fine with the answer.
Happy Learning!