Using arrays, while and for loops, and if/else statlments: Write a program that
ID: 3593881 • Letter: U
Question
Using arrays, while and for loops, and if/else statlments: Write a program that will read a variable number of real numbers. The program will then compute the mean (or average), minimum, and maximum of the numbers entered 1. The program will ask for the user to enter numbers. Entry of the numbers will end when 20 numbers have been entered, or when the user inputs . q. (or any other zgnoned.ic character). hny numbers beyond the enexkinin of 20 waill be ignored. iBnne pro thend P 2. The program will then print the (up to 20) numbers that were entered. 3. Finally, the program will compute the average, maximum, and minimum of the numbers entered and print out the resulting statistics. An example session is shown below. 4. If no valid numbers are entered, you should print out an error message to that effect This is how the output should look in 3 senerios: Enter real numbers, up to 20, q to quit 10 37 15 21 18 q You entered the following values: 10.0 37.0 15.0 21.0 18.0 The values have mean 20.2, max 37.0, and min 10.0 Enter real numbers, up to 20, q to quit No valid numbers entered Enter real numbers, up to 20, q to quit 1 2 3 4 5 6 7 8 9 10 11 12 13- 14 15 16 17 18 19 20 21 You entered the following values: 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.e 9.0 10.e 11.e 12.e 13.0 14.0 15.0 16.0 17.0 18.0 19.0 20.0 The values have mean 10.5, max 20.0, and min 1.0Explanation / Answer
#include <stdio.h>
int main() {
// your code goes here
int a[22],n=0,i=0;
int max=0,min=20;
float sum=0,avg=0;
printf("Enter the numbers");
a[0]=0;
for(i=0;i<20;i++)
{
scanf("%d",&a[i]);
if(a[i]<20)
{
sum=sum+a[i];
n++;
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
else
break;
}
avg=sum/n;
printf(" avg:%f min:%d max:%d",avg,min,max);
return 0;
}
Enter the numbers
2 4 6 7 5 3 1 3 4 6 4 2 6 4 4 1 1 1 1 1
avg:3.300000
min:1
max:7
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.