This is a lab that i had last week but, i still can't figure it out. Problem: Allow the user to enter positive and negative integers and end the sequence with input of 0. Output the averages of positive, negative, and total integers. If no positive or negative integers are entered then display no positive or negative integers where entered.
ex. Enter a sequence of integers, hit 0 key to stop: 4 4 4 0
The positive average is: 4
There were no negative integers entered
The total average is: 2
My problem is that I can't figure out how to display that there were no positive or negative integers entered. I'm pretty sure that it has something to do with an if statement. Any help would be greatly appreciated. Here's as far I have gotten with my code:
Code:
int main()
{
int n = 0; // user input
int pSum = 0, nSum = 0, pcount=0, ncount=0;
double pAverage = 0, nAverage = 0, totalAverage = 0; // positive and negative averages
cout << "Enter a sequence of integers, hit 0 key to stop: " ;
cin >> n; // user input
while ( n != 0 )
{
if( n > 0 ) // if input is positive
pAverage = double( pSum += n) / ++pcount; // operation for average
if( n < 0 ) // if input is negative
nAverage = double( nSum += n) / ++ncount;
cin >> n;
}
totalAverage = double(pAverage + nAverage) / 2;
if ( n == 0 ) // end sequence
{
cout << "The positive average is: " << pAverage << endl;
cout << "The negative average is: " << nAverage << endl;
cout << "The total average is: " << totalAverage << endl;
}
else
{
cout << "Invalid integers entered" << endl;// invlaid entry
}
return 0;
}