How to resolve the algorithm Guess the number step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Guess the number step by step in the C programming language

Table of Contents

Problem Statement

Write a program where the program chooses a number between   1   and   10. A player is then prompted to enter a guess.   If the player guesses wrong,   then the prompt appears again until the guess is correct. When the player has made a successful guess the computer will issue a   "Well guessed!"   message,   and the program exits. A   conditional loop   may be used to repeat the guessing until the user is correct.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Guess the number step by step in the C programming language

This C program simulates the classic number guessing game where the computer thinks of a random number between 1 and 10, and the user tries to guess it.

Here's a breakdown of the code:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

These lines include necessary standard C libraries:

  • <stdlib.h>: Defines the rand() and srand() functions for generating random numbers.
  • <stdio.h>: Contains functions for input and output operations, such as printf() and scanf().
  • <time.h>: Includes the time() function used to seed the random number generator with the current time.
int main(void)
{
   int n;
   int g;
   char c;
  • int main(void): This is the entry point of the program. It returns an integer (0 in this case) to indicate the exit status.
  • int n;: Variable to store the random number generated by the computer.
  • int g;: Variable to store the user's guess.
  • char c;: Used to ignore non-number characters entered by the user.
   srand(time(NULL));
  • This line initializes the random number generator using the current time as the seed. This ensures that the random numbers generated are different each time the program is run.
   n = 1 + (rand() % 10);
  • Generates a random number between 1 and 10 and stores it in n.
   puts("I'm thinking of a number between 1 and 10.");
   puts("Try to guess it:");
  • Prints instructions to the user, asking them to guess the computer's number.
   while (1) {
  • This is an infinite loop that continues until the user correctly guesses the number.
       if (scanf("%d", &g) != 1) {
  • This line reads a single integer from standard input (usually the keyboard) into the variable g. If the user enters an invalid input (non-integer), this statement evaluates to false (0).
   	/* ignore one char, in case user gave a non-number */
   	scanf("%c", &c);
  • This code is executed if the user enters an invalid input. It reads and ignores the invalid character from the input, effectively clearing the input buffer.
       if (g == n) {
  • This condition checks if the user's guess g is equal to the computer's number n.
       puts("Correct!");
       return 0;
  • If the guess is correct, it prints "Correct!" and exits the program with an exit status of 0.
       puts("That's not my number. Try another guess:");
  • If the guess is incorrect, this code is executed, and it prompts the user to enter another guess.
   }
}
  • The loop continues until the user guesses the correct number or the program is terminated for some other reason (e.g., by pressing Ctrl+C).

Source code in the c programming language

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main(void)
{
    int n;
    int g;
    char c;

    srand(time(NULL));
    n = 1 + (rand() % 10);

    puts("I'm thinking of a number between 1 and 10.");
    puts("Try to guess it:");

    while (1) {
        if (scanf("%d", &g) != 1) {
		/* ignore one char, in case user gave a non-number */
		scanf("%c", &c);
		continue;
	}

        if (g == n) {
	    puts("Correct!");
	    return 0;
	}
        puts("That's not my number. Try another guess:");
    }
}


  

You may also check:How to resolve the algorithm Deal cards for FreeCell step by step in the Haskell programming language
You may also check:How to resolve the algorithm Combinations with repetitions step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Proper divisors step by step in the Action! programming language
You may also check:How to resolve the algorithm Floyd-Warshall algorithm step by step in the SequenceL programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the Harbour programming language