you will be writing a number guessing game where the user is asked to pick a num
ID: 3595976 • Letter: Y
Question
you will be writing a number guessing game where the user is asked to pick a number 1-10 which will be checked against a randomly generated number (50 pts) You will be writing a number guessing game where the user is asked to pick a number 1-10 which will be checked against a randomly generated number. 3. The user will continue guessing until they guess the correct answer. If the user does not guess correctly, the program will tell the user if the secret number is higher or lower than the number the user selected. Refer to the prelab 3 example if you forgot how to generate a random number. Don't forget to include "ctime" and "cstdlib" libraries Make sure you check for bad numeric data. If the user enters invalid data, continuously prompt the user until they enter a valid number. . . . Sample Output: Guess a number 1 10:5 The secret number is larger than 5 Guess a number 1 10:11 ERROR: Enter a number 1-10:6 The secret number is larger than 6 Guess a number 1 10:8 The secret number is smaller than 8 Guess a number 1 10:7 BINGO!! You guessed the secret number in 4 tries!Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
srand(time(NULL));
int r = rand() % 10 + 1;
int correct = 0;
int guess;
int counter = 0;
printf("Guess my number! ");
do {
scanf("%d", &guess);
if (guess == r) {
counter++;
printf("BINGO! You guessed the secret number in %d tries! ", counter);
correct = 1;
}
if (guess < r) {
counter++;
printf("Your secret number is larger than %d ",guess);
}
if (guess > r) {
counter++;
printf("Your secret number is smaller than %d ",guess);
}
} while (correct == 0);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.