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

Table of Contents

Problem Statement

Write a player for the game that follows the following rules: The computer should guess intelligently based on the accumulated scores given. One way is to use a Binary search based algorithm.

Let's start with the solution:

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

The first code you provided implements a binary search to guess a number between 1 and 100. It starts with a range of [1, 100] and asks the user to input H if the number is higher than the current guess, L if it's lower, or Y if it's correct. The program will keep narrowing down the range until the guess is correct. The second code you provided also implements a binary search, but it uses the bsearch() function from the C standard library. The bsearch() function takes a pointer to the first element of an array, the size of each element in the array, the number of elements in the array, and a comparison function. The comparison function must return a negative value if the first element is less than the second, a positive value if the first element is greater than the second, or 0 if the elements are equal. In the second code, the comparison function is implemented in the my_cmp() function. The my_cmp() function takes two pointers to void, subtracts the pointer ZERO from each pointer to get the integer values, and then calls the get_value() function to get the comparison value. The get_value() function takes an integer value and returns -1 if the value is lower than the target value, 1 if the value is higher than the target value, or 0 if the value is equal to the target value. The main function in the second code calls the bsearch() function to find the target value. If the bsearch() function returns NULL, it means that the target value was not found. Otherwise, the main function prints the target value.

Source code in the c programming language

#include <stdio.h>

int main(){
  int bounds[ 2 ] = {1, 100};
  char input[ 2 ] = "  ";
    /* second char is for the newline from hitting [return] */
  int choice = (bounds[ 0 ] + bounds[ 1 ]) / 2;
    /* using a binary search */

  printf( "Choose a number between %d and %d.\n", bounds[ 0 ], bounds[ 1 ] );

  do{
    switch( input[ 0 ] ){
      case 'H':
        bounds[ 1 ] = choice;
        break;
      case 'L':
        bounds[ 0 ] = choice;
        break;
      case 'Y':
        printf( "\nAwwwright\n" );
        return 0;
    }
    choice = (bounds[ 0 ] + bounds[ 1 ]) / 2;

    printf( "Is the number %d? (Y/H/L) ", choice );
  }while( scanf( "%1s", input ) == 1 );

  return 0;
}


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

enum {
  LOWER = 0,
  UPPER = 100,
  KEY = LOWER-1 // some value that is not in the valid range
};

char dummy;
// A pointer to represent the integer 0, and the basis of our integer-as-pointer
// representation. We can't use the null pointer because bsearch() returns that
// for not found.
#define ZERO ((void *)&dummy)

int get_value(int x) {
  if (x == KEY)
    return 0;
  printf("My guess is: %d. Is it too high, too low, or correct? (H/L/C) ", x);
  char input[2] = " ";
  scanf("%1s", input);
  switch (tolower(input[0])) {
    case 'l':
      return -1;
    case 'h':
      return 1;
    case 'c':
      return 0;
  }
  fprintf(stderr, "Invalid input\n");
  exit(1);
  return 0;
}

int my_cmp(const void *x, const void *y) {
  return get_value(x - ZERO) - get_value(y - ZERO);
}

int main() {
  printf("Instructions:\n"
	 "Think of integer number from %d (inclusive) to %d (exclusive) and\n"
	 "I will guess it. After each guess, you respond with L, H, or C depending\n"
	 "on if my guess was too low, too high, or correct.\n",
	 LOWER, UPPER);
  void *result = bsearch(ZERO + KEY, ZERO + LOWER, UPPER-LOWER, 1, my_cmp);
  if (result == NULL)
    fprintf(stderr, "That is impossible.\n");
  else
    printf("Your number is %d.\n", (int)(result - ZERO));
  return 0;
}


  

You may also check:How to resolve the algorithm Disarium numbers step by step in the Ada programming language
You may also check:How to resolve the algorithm Interactive programming (repl) step by step in the E programming language
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the EDSAC order code programming language
You may also check:How to resolve the algorithm Paraffins step by step in the Racket programming language
You may also check:How to resolve the algorithm Pangram checker step by step in the Arturo programming language