How to resolve the algorithm Guess the number/With feedback 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/With feedback step by step in the C programming language

Table of Contents

Problem Statement

Write a game (computer program) that follows the following rules:

Let's start with the solution:

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

This C code implements a simple number guessing game. Here's a detailed breakdown of the code:

  1. Header Files:

    • The code includes several standard C library header files:
      • <stdlib.h>: For srand() and rand() functions, which generate random numbers.
      • <stdio.h>: For input and output functions like printf() and scanf().
      • <time.h>: For the time() function, which is used to initialize the random number generator.
  2. Constants:

    • lower_limit: This is the minimum value of the random number that the program will generate.
    • upper_limit: This is the maximum value of the random number that the program will generate.
  3. Function main:

    • This is the entry point of the program.
  4. Random Number Generation:

    • srand( time( 0 ) );: This line initializes the random number generator using the current time as a seed. This ensures that the generated random numbers will be different each time the program is run.
    • number = lower_limit + rand() % (upper_limit - lower_limit + 1);: This line generates a random number between lower_limit and upper_limit and stores it in the number variable.
  5. Guessing Loop:

    • printf( "Guess the number between %d and %d: ", lower_limit, upper_limit );: This line prompts the user to enter a guess for the random number.
    • The program enters a loop where it continuously prompts the user for guesses until the user guesses correctly or enters an invalid guess.
    • while( scanf( "%d", &guess ) == 1 ): This loop continues as long as the user enters valid integer guesses.
    • if( number == guess ): Inside the loop, this condition checks if the user's guess matches the random number generated.
    • If the guess is correct, it prints "You guessed correctly!" and breaks out of the loop.
    • If the guess is not correct, it prints "Your guess was too high." or "Your guess was too low." and prompts the user to try again.
    • The loop continues until the user guesses correctly or enters an invalid guess.
  6. Exit:

    • The program returns 0 from the main function, indicating successful execution.

Source code in the c programming language

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

#define lower_limit 0
#define upper_limit 100

int main(){
  int number, guess;

  srand( time( 0 ) );
  number = lower_limit + rand() % (upper_limit - lower_limit + 1);

  printf( "Guess the number between %d and %d: ", lower_limit, upper_limit );

  while( scanf( "%d", &guess ) == 1 ){
    if( number == guess ){
      printf( "You guessed correctly!\n" );
      break;
    }
    printf( "Your guess was too %s.\nTry again: ", number < guess ? "high" : "low" );
  }

  return 0;
}


  

You may also check:How to resolve the algorithm Zero to the zero power step by step in the Applesoft BASIC programming language
You may also check:How to resolve the algorithm Parsing/Shunting-yard algorithm step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Shoelace formula for polygonal area step by step in the Fortran programming language
You may also check:How to resolve the algorithm Catamorphism step by step in the Objeck programming language
You may also check:How to resolve the algorithm Hamming numbers step by step in the Bracmat programming language