Question: Modify the code to allow the user to enter any number of positive integers and calculate the average. In other words, the user could enter
Modify the code to allow the user to enter any number of positive integers and calculate the average. In other words, the user could enter any number of positive integers. (Hint: You can prompt the user for how many they want to enter. Or; you could use a sentinel value to trigger when the user has completed entering values). Prepare a new test table with at least 3 distinct test cases listing input and expected output for the code you created. Support your experimentation with screen captures of executing the new code.
How can I make the code bellow allow any number of integers for the input?
// C code
// This program will calculate the sum of 10 positive integers.
// Developer: Faculty CMIS102
// Date: Jan 31, XXXX
#include
int main ()
{
/* variable definition: */
int count, value, sum; double avg;
/* Initialize */
count = 0;
sum = 0; avg = 0.0;
// Loop through to input values
while (count < 10)
{
printf("Enter a positive Integer ");
scanf("%d", &value); if (value >= 0) { sum = sum + value; count = count + 1; } else { printf("Value must be positive "); }
}
// Calculate avg. Need to type cast since two integers will yield an integer
avg = (double) sum/count;
printf("average is %lf " , avg );
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
