4. Read the following example showing a type of while loop you have seen before,
ID: 3725079 • Letter: 4
Question
4. Read the following example showing a type of while loop you have seen before, used for input validation Although you try to make your application's instructions as clear as possible, you know that users often make mistakes and enter input incorrectly. Complete the code to catch the error if the user's input is not withirn the specified range. The user should be informed of the problem and allowed to enter the input again. int guess; printf ("Please guess a lucky integer between 1 and 10, inclusive: " scanf ("d", while( ) /lout of range? printf ("Sorry, 5, Do we know how many times the loop in Problem #4 will repeat? why or why not? Explain. 6, what type of while loop is shown in Problem # 4 above? Circle the correct letter from the list below: a. Count-controlled while loop b. Sentinel value-controlled while loop c. Event or flag-controlled while loop d. EOF-controlled while loop e. Search-controlled while loopExplanation / Answer
Answer:
Q.4)
int guess;
printf("please guess a lucky integer between 1 and 10, inclusive: ");
scanf("%d",&guess); /* Takes the data from the keyboard and stores in the variable guess*/
while(!(guess>=1&&guess<=10)) /* If the variable guess doesn't contain the values from 1 to 10, body of the loop will be executed */
{
printf("sorry, please guess a lucky integer between 1 and 10, inclusive: "); /*prompts the user to enter the valid input*/
scanf("%d",&guess); /* user enters another value */
if(guess>=1&&guess<=10) printf("Your Input Is Valid."); /* If the value of the guess is within the range, the message will be printed, otherwise it will not print any message, goes to condition evaluation of while loop*/
}
Q.5)
If the user enters the correct input for the first time, then the control will not be entered into the loop.
So, the number of times loop repeated will be Zero (0).
If the user enters the incorrect input for the first time, then the control will be entered into the loop and takes the input once again in the loop. If we do the same for n times (n incorrect inputs), the loop will be repeated for n times.
Q.6)
The Answer is Sentinel value-controlled while loop.
The loop will be executed till the entered value of the variable guess is not between 1 and 10 (Inclusive of 1 and 10). This is a sentinel controlled loop and here the variable guess is a sentinel variable.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.