How to separate positive and negative count from the following code: #include<io
ID: 653440 • Letter: H
Question
How to separate positive and negative count from the following code:
#include<iostream>
using namespace std;
int main()
{
int count=0;
double num;
double positive=0;
double negative=0;
cout<<"Please enter 10 whole numbers:"<<endl;
do
{
cin >> num;
if (num<=0)
negative = negative+num;
else
positive= positive+num;
count++;
}
while (count<10);
cout<<"The sum of all postive numbers is:"<<" "<< positive<<endl;
cout<<"The sum of all negative numbers is:"<<" "<<negative<<endl;
cout<<"The sum of all numbers is:"<<" "<<positive+negative<<endl;
cout<<"The average of all positive numbers is:"<<" "<<positive/count<<endl;
cout<<"The average of all negative numbers is:"<<" "<<negative/count<<endl;
cout<<"The average of all numbers is:"<<" "<<(positive+negative)/count<<endl;
return 0;
}
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
int positiveCount=0;
int negativeCount=0;
int count = 0;
double num;
double positive=0;
double negative=0;
cout<<"Please enter 10 whole numbers:"<<endl;
do
{
cin >> num;
if (num<=0)
{
negative = negative+num;
negativeCount++;
}
else{
positive= positive+num;
positiveCount++;
}
count++;
}
while (count<10);
cout<<"The sum of all postive numbers is:"<<" "<< positive<<endl;
cout<<"The sum of all negative numbers is:"<<" "<<negative<<endl;
cout<<"The sum of all numbers is:"<<" "<<positive+negative<<endl;
cout<<"The average of all positive numbers is:"<<" "<<positive/positiveCount<<endl;
cout<<"The average of all negative numbers is:"<<" "<<negative/negativeCount<<endl;
cout<<"The average of all numbers is:"<<" "<<(positive+negative)/count<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.