In 2 to 3 paragraphs describe the C program written below (What the program is s
ID: 3801090 • Letter: I
Question
In 2 to 3 paragraphs describe the C program written below (What the program is supposed to do). State the requirements for the program (How does the program meet the requirements) and discuss the logical process your program uses to meet those requirements (The process steps to meet the requirements).
#include<stdio.h>
int main()
{
//declaration and initialization of variables.
int n=0, ran, count=0;
//initialize the random seed with time(NULL) to get random number each time
srand(time(NULL));
//rand()%(max-min+1)+min generates the random number within a range.
ran=rand()%(20)+1;
//loop until n is not equal to random number
while(n!=ran)
{
//user to guess a number
printf("Guess the number : ");
scanf("%d",&n);
//if the number is equal to the number guessed, then print "Correct guess"
if(n==ran)
printf("Correct guess ! ");
//if the number guessed is greater than the random number, then print "The guess was too high"
else if(n>ran)
printf("The guess was too high ");
else
//if the number guessed is lesser than the random number, then print "The guess was too low"
printf("The guess was too low ");
//count=Number of times the loop runs=Number of guess made
count++;
}
//once the correct guess is made, then the loop terminates.
//print the number of guess made.
printf("Number of guess(es) made %d : ",count);
return 0;
}
Explanation / Answer
It supposed to do: match the user guessed value with random genereated value based on single or multiple attempts.
Met Requirement:
>>>random number which was generated by the system between 1-20.
>>> user Guessed number.
Logical steps:
>>> First random number will be genereated and prompts the user to enter the guess. next guessed number will be compared with random number. If it is too high then says that to use and again prompts for next guess.
>>> If guessed number is too low that will also tells to user and again prompts the user for next Guess.
>>> Like above program will executes till user guess and Random number are equal. Then program will prints congrats message and exit the program.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.