Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a program that calculates the average of a stream of positive numbers.

ID: 3544366 • Letter: 1

Question

1.    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 readin 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.

The main() function should print the average (which was returned from your function), and then ask the user if they want to repeat the entire process. The user should be allowed to repeat the process as many times as they wish. This means you will have a loop in your main() function that instructs the user what to do, calls your average function, reports the result.

1. Use for loop.

2. The expression will be evaluated in if statement not in for loop construct.

3. The average variable should be global as well as local same name.

4. The variable total should have scope of main.

5. The count of numbers should be a local variable of function.

6. Then each number accepted by user should be local variable of for loop.

7. The answer could be in decimal also.

Please help me

Explanation / Answer

include <iostream>
using namespace std;

double do_calculation(int sum, int count);

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;
total_number++;
}
}while(number >= 0);

average= do_calculation(sum_numbers, total_numbers);

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



double do_calculation(int sum, int count);
{
return ((double)sum) / (double)count;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote