In C inlcude <stdio.h> Prompt the user to enter 5 floating point numbers, the nu
ID: 3756209 • Letter: I
Question
In C
inlcude <stdio.h>
Prompt the user to enter 5 floating point numbers, the numbers being test scores in the range 0.0 to 100.0
You need to validate the user's input for this exercise. If the user enters a number outside the legal range, print the message "Invalid Input" and prompt the user again for a valid number. [NOTE: You do not need to test for character input.]
Store the numbers in an array of doubles. Output the numbers on one line, each number followed by a comma (except the last number).
Also output:
- the total number of points
- the average of the array elements
- the value of the maximum array element
- the value of the minimum array element
Sample Output:
Enter Score 1: 36.0
Enter Score 2: -1000
Invalid Input
Enter Score 2: 89.5
Enter Score 3: 42.0
Enter Score 4: 66.3
Enter Score 5: 93.0
You entered: 36.000000, 89.500000, 42.000000, 66.300000, 93.000000
Total Score: 326.800000
Average Score: 65.360000
Max Score: 93.000000
Min Score: 36.000000
HINTS:
- This might be a good time to use a boolean variable and the <stdbool.h> package.
- Using a temp variable in the scanf statement might be a good idea!
Explanation / Answer
#include <stdio.h>
int main(void) {
float score[5],min,max,total,avg;
int i;
max = 0.0;
min = 100.00;
total = 0.0;
for(i=0;i<5;i++)
{
printf(" Enter Score %d: ",i+1);
scanf("%f",&score[i]);
if(score[i] <0.0 || score[i]>100.00) // validation
{
printf(" Invalid Input");
i--;
}
total = total + score[i];
if(score[i] > max)
max = score[i];
if(score[i]<min)
min = score[i];
}
avg = total/5;
printf(" You entered: ");
for(i=0;i<5;i++)
{
printf("%f",score[i]);
if(i<4)
printf(",");
}
printf(" Total Score: %f",total);
printf(" Average Score: %f",avg);
printf(" Max Score: %f",max);
printf(" Min Score: %f",min);
return 0;
}
Output:
Enter Score 1: 36.0
Enter Score 2: -1000
Invalid Input
Enter Score 2: 89.5
Enter Score 3: 42.0
Enter Score 4: 66.3
Enter Score 5: 93.0
You entered: 36.000000,89.500000,42.000000,66.300003,93.000000
Total Score: 326.800000
Average Score: 65.36000
Max Score: 93.000000
Min Score: 36.000000
Do ask if any doubt. Please upvote.