Here is my C language Code: #include <stdio.h> void guess_game(int); static int
ID: 3716430 • Letter: H
Question
Here is my C language Code:
#include <stdio.h>
void guess_game(int);
static int cwins=0,uwins=0,gueslimit=0;
int main()
{
int guess;
printf("Welcome to the guessing game! ");
printf("I am thinking of a number between 1 and 20.");
while(1){
printf(" Enter your guess or press '0' to stop:");
scanf("%d",&guess);
if(guess==0)
{
printf(" computer score is %d",cwins);
printf(" Human score is %d",uwins);
if(cwins==uwins)
printf(" both of them got tie");
else if(cwins>uwins)
printf(" Computer won game!!");
else
printf(" Human won game!!!");
exit(0);
}else
{
gueslimit++;
guess_game(guess);
}
if(gueslimit>=5)
{
printf(" your guess limit is over:");
printf(" Computer score is %d",cwins);
printf(" Human score is %d",uwins);
if(cwins==uwins)
printf(" both of them got tie");
else if(cwins>uwins)
printf(" Computer won game!!");
else
printf(" Human won game!!!");
exit(1);
}
}
return 0;
}
void guess_game(int guess){
int res=rand() % (20 +1-1)+1;
if(guess==res){
printf("You got it! The number was: %d",res);
uwins++;
}else if(guess>res)
{
printf("number is higher");
cwins++;
}else{
printf("number is lower");
cwins++;
}
}
Above my code is working. Here is what I need you to do. The name of the function should be twentyNum(). The score between the human and the computer must be kept for as long as you are running your homework driver program. I want the syntax different from the above code. So, please use the different syntax, which is different from my code. However, the output has to be the same. Example output:
Welcome to the guessing game!
I am thinking of a number between 1 and 20.
Enter your guess or press 0 to stop.
10
The number is higher.
Enter your guess or press 0 to stop.
15
The number is higher.
Enter your guess or press 0 to stop.
18
You got it! The number was: 18
The current score is Computer: 1 Human: 3
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static int cwins=0,uwins=0,gueslimit=0;
void twentyNum(int guess, int res){
if(guess < res) {
printf("number is lower");
cwins++;
}
else if(guess > res) {
printf("number is higher");
cwins++;
}
else if(guess == res) {
printf("You got it! The number was: %d",res);
uwins++;
}
}
void print() {
printf(" computer score is %d",cwins);
printf(" Human score is %d",uwins);
if(cwins == uwins)
printf(" both of them got tie");
else if(cwins>uwins)
printf(" Computer won game!!");
else
printf(" Human won game!!!");
}
int main()
{
int guess;
printf("Welcome to the guessing game! ");
printf("I am thinking of a number between 1 and 20.");
srand(time(NULL));
int res = rand() % (20 - 1) + 1;
while(1)
{
printf(" Enter your guess or press '0' to stop:");
scanf("%d",&guess);
if(guess == res) {
gueslimit++;
uwins++;
printf("You got it! The number was: %d",res);
print();
break;
}
else if(guess != 0) {
gueslimit++;
twentyNum(guess, res);
}
else
{
print();
break;
}
if(gueslimit>=5)
{
printf(" your guess limit is over:");
print();
break;
}
}
return 0;
}
//NOTE : I have modified the code with different syntax, lemme know if you have any queries.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.