Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

- Aa lic usal rulac )ulsalaeli w 1.sstel blai Write a C++ program to implement t

ID: 3732719 • Letter: #

Question

- Aa lic usal rulac )ulsalaeli w 1.sstel blai Write a C++ program to implement the Number Guessing Game. In this game the user chooses a number between 1 and 100, and the player tries to guess the number in as few attempts as possible A. Each time the player enters a guess, the computer tells him whether the guess is too high, too low, or right. Once the player guesses the number, the game is over. The game should print the number of attempts the player needed to guess the number. The game should ask the player if he would like to play again (Ctrl). You can use the following functions to generate the random numbers: srand (time (0)) int number rand ) 8 100 1: / random number between 1 and //seed random number generator You may need to include the,include

Explanation / Answer

#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */

int main ()
{
int Secret, Guess,attempts=50,i;
char ch='y';
while(ch=='y' || ch== 'Y')
{
/* initialize random seed: */
srand (time(0));

/* generate secret number between 1 and 100: */
Secret = rand() % 100 + 1;
  
printf ("Guess the number from (1 to 100), No of attempts : %d ",50);
for(i=attempts;i>0;i--)
{ printf("enter the number that you guess:");
scanf ("%d",&Guess);
if (Secret<Guess) printf("Guess is higher, so try lower ");
else if (Secret>Guess) printf ("Guess is lower,so try higher ");
else
{printf(" Guess is right ");
break;

}
}
printf("want to play again (y/n):");fflush(stdin);
scanf("%c",&ch);

}
return 0;
}