This is a C practice problem. Answer is better with explanations, thank you! Bel
ID: 3753838 • Letter: T
Question
This is a C practice problem.
Answer is better with explanations, thank you!
Below is the file avg1.c and avg2.c
avg1.c:
below is the file avg2.c:
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.Explanation / Answer
Required answer is below, in case of any query please write in the comments.
Answer to question 1.
Yes, the two programs behave the same with valid inputs. The sample run of the avg1.c output and avg2.c output is given below.
avg1 Output:
This program computes the average of a list of integers.
Please enter the first integer in the list:
10
Enter the next integer, or type Ctrl+D to mark the end of the list:
20
Enter the next integer, or type Ctrl+D to mark the end of the list:
30
Enter the next integer, or type Ctrl+D to mark the end of the list:
^D
The list had 3 integers.
The average value was 20.
-------------------------------------------------------------------------------------------------
avg2 Output:
This program computes the average of a list of integers.
Please enter the first integer in the list:
12
Enter the next integer, or type Ctrl+D to mark the end of the list:
14
Enter the next integer, or type Ctrl+D to mark the end of the list:
16
Enter the next integer, or type Ctrl+D to mark the end of the list:
^D
The list had 3 integers.
The average value was 14.
-----------------------------------------------------------------------------------------------------
Answer to question 2.
When we give invalid input to avg1 executable, like an alphabet or a symbol, it goes into an infinite loop.
Output run:
This program computes the average of a list of integers.
Please enter the first integer in the list:
a
Enter the next integer, or type Ctrl+D to mark the end of the list:
Enter the next integer, or type Ctrl+D to mark the end of the list:
Enter the next integer, or type Ctrl+D to mark the end of the list:
Enter the next integer, or type Ctrl+D to mark the end of the list:
Enter the next integer, or type Ctrl+D to ................................................................
---------------------------------------------------------------------------------------------------------------
The reason being scanf consumes only the input which it is formatted for. Like here the formatting is done as "%d" so scanf only expects an int to be input here. If anything else is input like a character or a punctuation mark, the scanf function stops scanning and leaves the invalid character still in the buffer, thereby not waiting for the user input. And the return type of scanf in avg (in case when we input any alphabet or anything other than an integer) is always 0. So the while loop keeps going on. And hence the infinite loop.
However In case of avg2 output, we are handling all the scanf return types. Like when it is not equal to 1 that is either it is 0 or EOF (a negative constant) . In case the scanf input is an alphabet or a symbol, the while loop breaks out and the program exits saying "Failure to convert input to an int. Program terminated." When we input Ctrl+D indicating End of file, the program calculates the average if there were any integer numbers in the input list or else it says no number in the list and exits the program. In case of avg2 output , the program does not go in an infinite loop.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.