Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This is a C practice problem. Answer is better with explanations, thank you! Below is the file avg1.c and avg2.c avg1.c: #include int main(void) {
This is a C practice problem.
Answer is better with explanations, thank you!
Below is the file avg1.c and avg2.c
avg1.c:
#includeint main(void) { int num_read; int sum = 0; int count = 0; printf("This program computes the average of a list of integers. "); printf("Please enter the first integer in the list: "); while (scanf("%d", &num_read) != EOF) { count++; sum += num_read; printf("Enter the next integer,"); printf(" or type Ctrl+D to mark the end of the list: "); } if (count > 0) { printf(" The list had %d integers. ", count); printf("The average value was %g. ", (double) sum / count); } else printf(" No numbers in list -- can't compute an average. "); return 0; }
below is the file avg2.c:
#includeExercise D: scanf in an infinite loop Read This First Some of the text here reviews ideas seen in lectures and/or in code you looked at in Lab 1. But there may be a few new ideas mixed in, so please read this carefully.) scanf is a Standard C Library function that can be used to read terminal input into variables. The first argument to scanf is a string constant that looks somewhat like a printf control string. The remaining arguments are addresses of variables into which scanf will put input data. To detect input error after calling scanf, a program must check the return value from scanf. Before continuing to discuss scanf, it is important to make sure you know exactly what is meant by the term return value. This is most easily explained by example. Suppose scan.count and k are both variables of type int. Then in the function call scan-count scanf ("%d", &k); the return value is what is copied into scan_count The function call will probably also change the value of k, but it is wrong and confusing to say that "scanf returns a value into k". It is correct to say: An important side effect of calling scanf is to change the value of k." When scanf is used to read a single number, it returns one of three values: 1. 0, or EOF. EOF is a negative constant defined in#include int main(void) { int num_for_list; int sum = 0; int count = 0; int scan_count; printf("This program computes the average of a list of integers. "); printf("Please enter the first integer in the list: "); while (1) { scan_count = scanf("%d", &num_for_list); if (scan_count != 1) break; count++; sum += num_for_list; printf("Enter the next integer,"); printf(" or type Ctrl+D to mark the end of the list: "); } if (scan_count == 0) { printf(" Failure to convert input to an int. Program terminated. "); exit(1); } if (count > 0) { printf(" The list had %d integers. ", count); printf("The average value was %g. ", (double) sum / count); } else printf(" No numbers in list -- can't compute an average. "); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started