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

Your task is to design a small program that meets these specifications: 1. Using

ID: 3854706 • Letter: Y

Question

Your task is to design a small program that meets these specifications: 1. Using a loop, the program prompts the user to enter an integer value (it can be negative, positive, or zero). The program keeps a running sum of just the positive values (negative values are ignored). If the user enters a zero value, then the program exits the loop: otherwise, it stays in the loop to prompt the user for more input. 2. After exiting the loop, the program prints out the average of the positive numbers. Note that if the user entered no positive numbers at all then the program should print a message saying the average cannot be computed. Here is what a sample run might look like: Value? 5 Value? 10 Value? -4 Value? 2 Value? -3 Value? theta Positive average = 5.6667 Task Figure out a suitable method to solve the problem and then write a complete Program Design Report. State your assumptions and discuss your approach. For the logic low, use pseudocode.

Explanation / Answer

Algoritm to complete above program

1) We need to declare three variables of type double and 1 variable of type int, the First variable is to store the input value is of type double; the second variable to count the number of input values is of type int; the third variable to calculate and store the sum of input values is of type double; and the fourth variable is to calculate and store the average, is again of type double.

double num = -1; // to store the input numbers, the num has been initialized to -1 becoz first time the loop condition

// is not satisfied and the counter moves inside the body of the loop else the counter will not go inside the body of

// the loop if we initialize it to a positive number or 0.


int count = -1; // to count the input numbers
double sum = 0; // to store sum
double avg; // to store average

2) Next we will create a loop(say while loop) which will continue its looping until input number is zero. Whenever the input number is 0 the loop will terminate and the program ends printing the average.

while(num!=0)

3) Now, coming to the body of the loop. We will display a message to ask the user input his value. And the value is entered and stored in the variable num.

while(num!=0)

{

printf("Value ? ");   // message
scanf("%f", &num); // input number stored in variable num

4) Next we will put an if statement to check whether the input value was greater than 0 or not. If yes then sum the number with variable sum and increment the value of variable count to 1.

while(num!=0)

{

printf("Value? ");

scanf("%f", &num);

if(num >= 0)

{

sum += num; //calculating sum

count++; //counting number of input numbers

}

}

and the above process continues untill the user inputs 0 to exit.

5) After the user inputs 0, the counter exits from the loop and next we put an if statement outside of the loop which checks that whether the value of count is not equal to zero i.e., if it's greater than 0 then it means the user had inputted positive value in the loop that's the reason the count value has incremented.

while(num!=0)

{

// while Loop body

} // loop ends here

if(count != 0) // if condition to check whether count is not 0

6) Next when the condition satisfies that count value is greater than 0, then the counter goes inside the body of the if block to calculate the average and print the same.

if(count != 0)

{

avg = (float) sum/count; //calculating average

printf(" Positive Average = %.2f", avg); //printing average, upto 2 decimal places

}

7) Else if the count is zero then the program prints an error message that average can not be computed.

else

printf(" The average cannot be computed"); //else print this message

FULL C CODE

#include<stdio.h>

#include<conio.h>

void main()

{

//variable declaration

double num = -1;

int count = -1;

double sum = 0;

double avg;

//looping until num is not equal to 0

while(num!=0)

{

printf("Value? ");

scanf("%lf", &num);

if(num >= 0)

{

count++; //counting number of input numbers

sum += num; //calculating sum

}

}

if(count != 0)

{

avg = sum/count; //calculating average

printf(" Positive Average = %.2lf", avg); //printing average upto 2 decimal places

}

else

printf(" The average cannot be computed"); //else print this message

getch();

}

#include<stdio.h>

#include<conio.h>

void main()

{

//variable declaration

double num = -1;

int count = -1;

double sum = 0;

double avg;

//looping until num is not equal to 0

while(num!=0)

{

printf("Value? ");

scanf("%lf", &num);

if(num >= 0)

{

count++; //counting number of input numbers

sum += num; //calculating sum

}

}

if(count != 0)

{

avg = sum/count; //calculating average

printf(" Positive Average = %.2lf", avg); //printing average upto 2 decimal places

}

else

printf(" The average cannot be computed"); //else print this message

getch();

}