Changing the code to average 20 Input Test Case Expected Output
1\svalue=10\svalue=3\svalue=1\svalue=5\svalue=1\svalue=2\svalue=7\svalue=12\svalue=3\svalue=21\svalue=16\svalue=11\svalue=9\svalue=8\svalue=19\svalue=20\svalue=13\svalue=17\svalue=27\svalue=40
The average is 12.25.
2 value=120 value=18 value=100 value=95 value=101 value=203 value=200 value=310 value=70 value=200 value=100 value=74 value=100 value=0 value=200 value=2 value=200 value=10 value=75
3 value=80\svalue=100\svalue=12\svalue=100\svalue=-90\svalue=90\svalue = 100\svalue=200\svalue=30\svalue=202\svalue=94\svalue=230\svalue=100\svalue=76\svalue=78\svalue=100\svalue=-80\svalue=80\svalue=20\svalue=92\svalue=200\svalue=234\svalue=202\svalue=243
When a positive value is entered, the average is 133.15.
What if you enter a value that isn't an integer? (For instance, a float or a string).
Support your experimentation with screen captures of executing the code. Entering a string or floating-point number, the still requires inputting an integer.
4. Modify the code to allow the user to enter an unspecified number of positive integers and calculate the average. In other words, the user could enter 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). You may need to conduct some research on your own to solve this problem. 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.
#include
int main ()
{
/* variable definition: */
int count, value, sum;
double avg;
/* Initialize */
printf("Enter the Count of the numbers\n");
scanf("%d",&count);
int i=0;
sum = 0;
// Loop through to input values
while (i < count)
{
printf("Enter a positive Integer\n");
scanf("%d", &value);
if (value >= 0)
{
sum = sum + value;
i = i + 1;
}
else {
printf("Value must be positiven");
}
}
// Calculate avg. Need to type cast since two integers will yield an integer
avg = (double) sum/count;
printf("Average is %lf\n" , avg );
return 0;
}
Test
Count
Integers
Output
1
4
5
6
8
9
7.000
2
3
5
7
8
6.666
3
0
3
9
6
7
8
-nan
Test 2
Test 3