168k views
4 votes
Write a function int guessing_game(num, rangemin, rangemax)that takes an integer and plays a guessing game with the user. Use the int ask_in_range(min, max)to get the user to guess a number num. If the guess is too low or too high, let the user know and ask for another guess. Keep asking for guesses until the user enters a correct guess that equals num. Keep track of number of guesses that the user took, and output that number when the game ends. Also make your guessing_game(num, rangemin, rangemax)function return that number.

1 Answer

4 votes

Answer:

Check the explanation

Step-by-step explanation:

Code:

#include <stdio.h>

int ask_in_range(int min, int max)

{

int n;

printf("Please guess a number: ");

scanf("%d", &n);

while(n<min || n>max)

{

printf("Your number is outside of [-100, 100] range. Please enter a number: ");

scanf("%d", &n);

}

return n;

}

int guessing_game(int num, int rangemin, int rangemax)

{

printf("Hello and welcome to the game.\\");

printf("You need to guess a number between -100 and 100.\\");

int count = 1;

int guess = ask_in_range(rangemin, rangemax);

while(guess!=num)

{

if(guess<num)

printf("Too low!\\");

else

printf("Too high!\\");

guess = ask_in_range(rangemin, rangemax);

count++;

}

printf("Good job! You took %d guesses.\\", count);

return count;

}

int main()

{

int count = guessing_game(13, -100, 100);

return 0;

}

Kindly check the attached images below to see the CODE SCREENSHOT and CODE OUTPUT.

Write a function int guessing_game(num, rangemin, rangemax)that takes an integer and-example-1
Write a function int guessing_game(num, rangemin, rangemax)that takes an integer and-example-2
User Jayjw
by
6.5k points