Question
IN C LANGUAGE, please debug and correct the following code so program outputs right numbers. /*---------------------------------------------------------------------------- - SE 185: Lab 04 - Debugging Code -
IN C LANGUAGE, please debug and correct the following code so program outputs right numbers.
/*----------------------------------------------------------------------------
- SE 185: Lab 04 - Debugging Code -
- Name: -
- Section: -
- NetID: -
- Date: -
-----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
- Includes -
-----------------------------------------------------------------------------*/
#include
#include
/*-----------------------------------------------------------------------------
- Prototypes -
------------------------------------------------------------------------------*/
char ask_to_play(int times_played);
int select_random_number();
/*----------------------------------------------------------------------------
- Notes -
-----------------------------------------------------------------------------*/
// Compile with gcc lab04-3.c -o lab04-3
// Run with ./lab04-3
/* This program will play a simple Guessing Game with the computer. */
/*----------------------------------------------------------------------------
- Implementation -
-----------------------------------------------------------------------------*/
int main(int argc, char *argv[])
{
char prompt = '-';
int played = 0, computer_guess = 0;
prompt = ask_to_play(played);
played = 1;
while (prompt == 'y') /* This line does not contain an error */
{
computer_guess = select_random_number();
run_game(computer_guess);
prompt = ask_to_play(played);
}
printf(" Thanks for playing! ");
return 0;
}
/**
* Asks the player if they want to play the Guessing Game.
*
* @param played_before - Whether the player has played a round of the game before or not.
* @return - Whether the player wants to play again or not.
*/
char ask_to_play(int played_before)
{
char yes_or_no;
if (!played_before) /* This line does not contain an error */
{
printf("Do you want to play a game? "
"Enter 'y' to play, anything else not to play. :( -> ");
scanf(" %c", yes_or_no);
} else
{
scanf(" %c", &yes_or_no);
}
printf("%c", yes_or_no);
return yes_or_no;
}
/**
* Generates a random number between 1 to 100, inclusive.
*
* @return - A number between 1 and 100, inclusive.
*/
int select_random_number()
{
srand(time(NULL));
return rand() % 100;
}
/**
* Starts the Guessing Game for you to play!
*
* @param computer_number - The randomly generated number to be used for the game.
*/
void run_game(int computer_number)
{
int number = 0;
printf(" You are guessing a number. The options are 1 through 100. ");
printf("What is your guess on what number I will select? -> ");
scanf("%c", &number);
while (!correct) /* This line does not contain an error */
{
if (number < 1 || number > 100)
{
printf(" Your number is not within the correct range of numbers. Guess again -> ");
} else if (number = computer_number)
{
printf(" The number was %d! ", computer_number);
printf(" You guessed the number correctly! "
"Do you want to play again? ('y' for yes) -> ");
correct = 1;
} else if (number < computer_number)
{
printf(" You guessed too low. Enter another guess. -> ");
} else
{
printf(" You guessed too high. Enter another guess. -> ");
}
scanf("%d", &number);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started