Question

Write a C program that asks the user to think of an integer number from 1...

Write a C program that asks the user to think of an integer number from 1 to 20. Have the computer guess what the number is by using a binary tree to determine the next guess. You can create the binary tree “by hand” by typing in a set of malloc commands and explicitly linking the nodes. (make the tree completely, then traverse the tree)
Use a menu to provide the user with a selection to answer the results of the guesses. Do something like
(not exactly like this – write your own fun/cool menu with a border):

My guess is 5.
Is your number
a. 5
b. less than 5
c. more than 5
Please type in a, b or c: _

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code:

#include<stdio.h>
struct tree
{
int data;
struct tree * left;
struct tree * right;
};
int print(struct tree * root)
{
if(root==NULL)return;

print(root->left);
printf("%d ",root->data);
print(root->right);


}

struct tree* root,*node;
void create_tree()
{
root=(struct tree*)malloc(sizeof(struct tree));
root->data=10;
root->left=NULL;
root->right=NULL;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=5;
node->left=NULL;
node->right=NULL;
root->left=node;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=15;
node->left=NULL;
node->right=NULL;
root->right=node;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=3;
node->left=NULL;
node->right=NULL;
root->left->left=node;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=7;
node->left=NULL;
node->right=NULL;
root->left->right=node;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=2;
node->left=NULL;
node->right=NULL;
root->left->left->left=node;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=1;
node->left=NULL;
node->right=NULL;
root->left->left->left->left=node;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=4;
node->left=NULL;
node->right=NULL;
root->left->left->right=node;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=6;
node->left=NULL;
node->right=NULL;
root->left->right->left=node;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=8;
node->left=NULL;
node->right=NULL;
root->left->right->right=node;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=9;
node->left=NULL;
node->right=NULL;
root->left->right->right->right=node;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=13;
node->left=NULL;
node->right=NULL;
root->right->left=node;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=17;
node->left=NULL;
node->right=NULL;
root->right->right=node;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=11;
node->left=NULL;
node->right=NULL;
root->right->left->left=node;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=14;
node->left=NULL;
node->right=NULL;
root->right->left->right=node;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=16;
node->left=NULL;
node->right=NULL;
root->right->right->left=node;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=18;
node->left=NULL;
node->right=NULL;
root->right->right->right=node;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=12;
node->left=NULL;
node->right=NULL;
root->right->left->left->right=node;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=19;
node->left=NULL;
node->right=NULL;
root->right->right->right->right=node;

node=(struct tree*)malloc(sizeof(struct tree));
node->data=20;
node->left=NULL;
node->right=NULL;
root->right->right->right->right->right=node;

}
void game()
{
printf("Please guess a number between 1-20\n");
struct tree *node;
node=root;
int found=0;
int ch;
while(node)
{
printf("\nMy guess is %d.",node->data);
printf("\nIs your number....");
printf("\t\n1:%d",node->data);
printf("\t\n2:less that %d",node->data);
printf("\t\n3:greater than %d\n",node->data);
printf("\nPlease select choice:");
scanf("%d", &ch);

switch(ch)
{
case 1:
printf("\nGreat found your number which is equal to %d",node->data);
found=1;
break;
case 2:
node=node->left;
break;
case 3:
node=node->right;
break;
default:
printf("\nwrong choice");
}
if(found)
break;

}
}
int main()
{
create_tree();
print(root);
game();
return 1;

}

Output:

1 2 345678 9 10 11 12 13 14 15 16 17 18 19 20 Please guess a number between 1-20 My guess is 10. Is your number.. .. 1:10 2:l

Dear friend,

I have added one extra function print to print the tree which may not be required in this case it is just for debug purpose. Hope this helps you.

Add a comment
Know the answer?
Add Answer to:
Write a C program that asks the user to think of an integer number from 1...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (c++) Write a program that generates a random number between 1 and 100 and asks the...

    (c++) Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Be sure that your program...

  • In Python, write a program that asks the user for a positive integer number. If the...

    In Python, write a program that asks the user for a positive integer number. If the user enters anything else, the program would ask the user to re-enter a number or enter -1 to quit. If it is a positive number, the program then prints out a multiplication table that goes up to that number. For example, if the user enters 10, the output would look something like this. https://www.vectorstock.com/royalty-free-vector/multiplication-table-on-white-background-vector-2721686

  • Write a program that prompts the user for an integer that the player (maybe the user,...

    Write a program that prompts the user for an integer that the player (maybe the user, maybe someone else) will try to guess. If the player's guess is higher than the target number, the program should display "too high" If the user's guess is lower than the target number, the program should display "too low" The program should use a loop that repeats until the user correctly guesses the number. Then the program should print how many guesses it took....

  • C++ Visual Studios Your program will attempt to guess a number that the user is thinking...

    C++ Visual Studios Your program will attempt to guess a number that the user is thinking of within the range [1, 20) (1 inclusive 20 exclusive). Your program will have 5 tries and each time the user should be able to tell your program if it is either correct, high, or low. If your program attempts 5 times but is not told that it is correct on the 5th try you should output "You cheated...". Otherwise your program should always...

  • Solve Question using While loops-MATLAB Write a program that generates a random number and asks the...

    Solve Question using While loops-MATLAB Write a program that generates a random number and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." The program should use a loop that repeats until the user correctly guesses the random number.

  • Write a Program that has a menu with a  layout below, that asks the user if they...

    Write a Program that has a menu with a  layout below, that asks the user if they want to: 1. Converts from feet and inches to meter and centimeters 2. Converts from meter and centimeters to feet and inches 3. Quit the program The program should continue as long as the user asks it to. The program will use a switch statement for the menu option selection. Either a do loo or a do-while loop can be used for the overall...

  • Computer Guesses - Create a page that allows the user to think of a number from...

    Computer Guesses - Create a page that allows the user to think of a number from 1 to 100. Write a function that will guess the number the user chose. The user will provide clues: higher or lower and correct. It should take no more than 7 guesses to get the correct number. User Guesses - Create a page that generates a random number from 1 to 100. Allow the user only 7 guesses to guess the number. Provide clues:...

  • Write a C++ program using "for" loop to allow user to guess a certain number say...

    Write a C++ program using "for" loop to allow user to guess a certain number say 55. The loop will end after 5 guesses or if he guesses the right number, which ever comes first. If user enters a number greater than 55, your program should print "You entered a bigger number". If user enters a number smaller than 55, your program should print "You entered a smaller number". If user enters 55 in less than 5 attempt, your program...

  • Write a program that allows a user to play a guessing game. Pick a random number...

    Write a program that allows a user to play a guessing game. Pick a random number in between 1 and 100, and then prompt the user for a guess. For their first guess, if it’s not correct, respond to the user that their guess was “hot.” For all subsequent guesses, respond that the user was “hot”if their new guess is strictly closer to the secret number than their old guess and respond with“cold”, otherwise. Continue getting guesses from the user...

  • USING PYTHON 1. Write a small program that asks for an integer number from the user...

    USING PYTHON 1. Write a small program that asks for an integer number from the user and print all the prime numbers (2,3,5,7,etc) less than the input number. 2. How long it takes for your program to print the prime numbers less than 100. (Use magic functions)

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT