Write a program that calculates the average of a stream of positive numbers. The user can enter as many positive numbers as they want, and they will indicate that they are finished by entering a negative number. For this program, treat zero as a positive number, i.e., zero counts as a number that goes into the average. Of course, the negative number should not be part of the average. You must use a function to read in the numbers, keep track of the running sum and count, compute the average, and return the average to the main() function. Note that you must use a loop to do this, as you don't know how many numbers the user will enter ahead of time.

So this is what I have so far but its not working out at all. I need some help!

#include <iostream>

using namespace std;

double do_calculation(double number);

int main()

{

int number=0,sum_numbers=0, total_number=0;

double average;

do

{

cout<<"Enter the number: ";

cin>>number;

sum_numbers=number+sum_numbers;

total_number++;

average= do_calculation(number);

cout<<"The sum of the postive number is" << sum_numbers<< "and the average is equal to " << average<<endl;

}while(number >= 0);

double do_calculation(double number);

{

return (sum_numbers/total_number);

}

}

So this is what I have so far but its not working out at all. I need some help!

#include <iostream>

using namespace std;

double do_calculation(double number);

int main()

{

int number=0,sum_numbers=0, total_number=0;

double average;

do

{

cout<<"Enter the number: ";

cin>>number;

sum_numbers=number+sum_numbers;

total_number++;

average= do_calculation(number);

cout<<"The sum of the postive number is" << sum_numbers<< "and the average is equal to " << average<<endl;

}while(number >= 0);

double do_calculation(double number);

{

return (sum_numbers/total_number);

}

}

here is what I have now.

#include <iostream>

using namespace std;

double do_calculation(double number);

int main()

{

int number=0,sum_numbers=0, total_number=0;

double average;

do

{

cout<<"Enter the number: ";

cin>>number;

if(number>=0)

{sum_numbers=number+sum_numbers;}

else

{sum_numbers=sum_numbers;}

if(number>=0)

{total_number++;}

else

{total_number;}

cout<<"The sum of the postive number =" << sum_numbers<< " total number = " << total_number<<endl;

}while(number >= 0);

}

#include <iostream>

using namespace std;

double do_calculation(double number);

int main()

{

int number=0,sum_numbers=0, total_number=0;

double average;

do

{

cout<<"Enter the number: ";

cin>>number;

if(number>=0)

{sum_numbers=number+sum_numbers;}

else

{sum_numbers=sum_numbers;}

if(number>=0)

{total_number++;}

else

{total_number;}

cout<<"The sum of the postive number =" << sum_numbers<< " total number = " << total_number<<endl;

}while(number >= 0);

}

Topic archived. No new replies allowed.