0

Hi...there,,can anyone debugging my code..
this program intend to find the average and count how many numbers is positif and negatif from 10 numbers entered by user.
My problem is when i run the program the result of average is correct but somehow on the result of negatif and positif numbers showed wrongly..
Can anyone tell me where's the error??
Thanks so much

#include <iostream>

void getNum(float& num,float& sum);
void avgNum (float sum,float& avg);
void countNum (float num,int& negatif,int& positif);
void printNum(float avg, int negatif, int positif);

using namespace std;
int main()
{

float n,s,a;
int neg,pos;
getNum(n,s);
avgNum(s,a);
countNum(n,neg,pos);
printNum(a,neg,pos);    
    
system("pause");  
return 0;    
}

void getNum(float& num,float& sum)
{
cout<<"Please enter 10 numbers:"<<endl;
sum=0;
for(int i=0; i<10; i++)
{
        cout<<"Enter "<<i+1<<" number:";
        cin>>num;
        sum=sum+num;
}
return;   
}

void avgNum(float sum,float& avg)
{
avg=sum/10;

return;     
}

void countNum(float num,int& negatif,int& positif)
{
     
if(num<0)
negatif++;
else
positif++;

return;
}


void printNum(float avg, int negatif, int positif)
{
cout<<"The average of the 10 numbers is:"<<avg<<endl;
cout<<"We have"<<negatif<<"negatif numbers"<<endl;
cout<<"We have"<<positif<<"positif numbers"<<endl;
   
return;   
}

Edited by Nick Evan: removed extra [tex]-tags

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by ireimaima
0

Give neg and pos a meaningful value, probably zero, before trying to use them, just like you did for sum. Pass neg and pos to getNum() by reference. Call countNum() from within loop of getNum(), probably between line 31 and 32, otherwise you will only be able to process the last input for num obtained in getNum() in countNum().

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.