Write a program that plays a simple dice game between the computer and the user.
ID: 3765769 • Letter: W
Question
Write a program that plays a simple dice game between the computer and the user. When the program runs, a loop will repeat 10 times.
Each iteration of the loop should do the following:
•Generate a random integer in the range of 1 through 6. This is the value of the computer's die..
•Generate a random integer in the range of 1 through 6. This is the value of the user's die..
•The die with the highest value wins. (In case of a tie, there is no winner for that particular roll of the dice.).
As the loop iterates, the program should keep count of the number of times the computer wins, and the number of times that the user wins.
After the loop performs all of it's iterations, the program should display who has the higher number of wins, the computer or the user.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int i,comp,user;
int ntie=0,ncomp=0,nuser=0;
// your code goes here
for(i=0;i<10;i++){
comp = rand() % 6 + 1;
user = rand() % 6 + 1;
if(comp == user)
ntie++;
else if (comp > user)
ncomp++;
else nuser++;
printf("Turn %d : Comp = %d and User = %d ",i+1,comp,user);
}
if(ncomp>nuser)
printf("Computer wins the game");
else if(nuser>ncomp)
printf("User wins the game");
else printf("Game ends in a Draw");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.