C++ problem

ploutarchos

Distinguished
Sep 15, 2008
10
0
18,560
ok here is my problem....


Write a program that allows the user to enter values and
prints out the average of all the positive values and the average of all the negative
values entered. Use 0 as the sentinel value.


my code is that::

[cpp]
#include<iostream>
using namespace std;
int main()
{
int number, pos = 0, neg = 0,sum2=0;
int countNEG=0, sum1=0, countPOS=0;
double averagePOS;
double averageNEG;

cout << "nnNumber: ";
cin >> number;
while (number != 0)
{
if (number > 0)
pos++;
else
neg++;
sum1 += pos++;
countPOS++;
sum2 += neg++;
countNEG++;
cout << "Number: ";
cin >> number;
}

if ( (countPOS == 0) || (countNEG ==0))
cout<<"You didn't enter any integer!";
else
averagePOS = ((double)sum1) / countPOS;
averageNEG = ((double)sum2) / countNEG;

cout<<" ";
cout<<"You entered "<<countPOS<< " positive integers."<<endl;
cout<<"You entered "<<countNEG<< " negative integers."<<endl;
cout<<"The average Of positive numbers is "<<averagePOS<<endl;
cout<<"The average Of negative numbers is "<<averageNEG<<endl;

cin>>pos;
return 0;
}
[/cpp]



help me...... it's wear because i set the positive numbers end the negatives
but always take all numbers.......


also i have this...... problem

Write a program to find the sum of the first 100 terms of the
series:

(1/1* 2) + (1/2* 3) + (1/3* 4) +....+ (1/n * (n + 1))

my code it's not full but i write anything i know,,
i thing is all wrong but anyway i will post it.....



[cpp]#include<iostream>
using namespace std;
int main()
{
int number = 1, total = 0,power=1;
while (power <= 100)
{
total = total + number * power;
power+=power+1;
number++;
}
cout << "Total: " <<total;
cin>>total;
return 0;
}


//Mt results here it's 621
[/cpp]
 

ploutarchos

Distinguished
Sep 15, 2008
10
0
18,560
yeaup it's easy if you practice ........ so

the solution for the first one is here............ when i found the second i will post, if any one knows something
please help..... thank you!!!!!!

[cpp]


#include<iostream>
using namespace std;
int main()
{

int number, pos = 0, neg = 0,sum2=0,sum1=0;
float averagePOS,averageNEG;

cout << "Number: ";
cin >> number;
while (number != 0)
{ //While loop
if (number > 0)
{ // IF
sum1=sum1+number;
pos++;
} // END IF
if (number<0)
{ // IF
sum2=sum2+number;
neg++;
} //END IF
cout << "Number: ";
cin >> number;

} //END While loop

averagePOS = static_cast<float>(sum1)/pos;
averageNEG = static_cast<float>(sum2)/neg;
cout<<"You entered "<<pos<< " positive integers."<<endl;
cout<<"You entered "<<neg<< " negative integers."<<endl;
cout<<"------------------------------------------"<<endl ;
cout<<"The average Of positive numbers is "<<averagePOS<<endl;
cout<<"The average Of negative numbers is "<<averageNEG<<endl;

cin>>pos;
return 0;
} ///END cpp.
[/cpp]