Answered step by step
Verified Expert Solution
Link Copied!

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.

image text in transcribed

image text in transcribed

Answer is better with explanations, thank you!

Below is the file avg1.c and avg2.c

avg1.c:

#include  int 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:

#include  #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; }
Exercise 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 . The value 1 means that scanf succeeded, that it actually read a number. The value 0 means that scanf encountered characters it couldn't convert into a number. The value EOF means that scanf reached the end of an input file or that some other error occurred. Now that you know that scanf returns a value, you may wonder why a statement like scanf ("%d", &k); is allowed in C. It's allowed because in C the value of an expression may be ignored in forming a statement, as in the allowed but useless statement x t y; A frequent special case of this rule is that it's allowable to ignore the return value from a function call. For instance, the return value from printf is almost always ignored. It's a bad idea to ignore the return value from scanf, because in doing so you pass up a chance to detect an input error Here's one more thing you should know about scanf: When it fails to read a number because the first character it reads can't be used as part of a number, it leaves that character on the input stream, and the next call to scanf or to some other input function will see that same character again. As you will see in this exercise, that can cause an infinite loop

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Apache Cassandra Development

Authors: Vivek Mishra

1st Edition

1484201426, 9781484201428

More Books

Students also viewed these Databases questions

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago