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

I\'m having a hard time specifically getting the program to terminate for e and

ID: 3891064 • Letter: I

Question

I'm having a hard time specifically getting the program to terminate for e and E. Thank you!

Q6. (10 points) Average Write a code in C to calculate the average (AVG) of the integers entered by a user ask the user to enter the values. to show the end of data entry the character 'e' or 'E' should be used. Print the average like the following example: (suppose the user has entered 2, 3, 4) · The average value is 2.00 Use the following formula for calculating average of n integers x1, x2....Xn. AVG (x1+ x2+...+Xn)/n The default is the user enters just integer numbers. No need to write extra code to exclude non-integers values.

Explanation / Answer

Hi, you can use the below fully executable code,
Input has to be given like 2 3 4 ... e or E
I have added comments to guide you

#include <stdio.h>

int main()
{
  
char x;
float average =0.0;
int count=0;
while(1)
{
scanf("%c",&x);
if(x=='e' || x=='E') //terminating condition
{
break;
}
else if(x!=' ') //removing the space attributes
{
int y = x - '0';
average+=y;
count++;
}

}
average=average/count;//average
printf("Average is %f ",average);
return 0;
}

Thumbs up if this was helpful, otherwise let me know in comments

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote