Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a program that accepts as user input 1 for heads, 2 for tails and any oth

ID: 3740567 • Letter: C

Question

Create a program that accepts as user input 1 for heads, 2 for tails and any other numeric value to exit. You must use “do while” loop. You must use the same single variable to control your loop and also to accept the user’s guesses. At the end you will add up the total number of times the user guessed, the number of times the computer won and the number of times the user won. As shown in the sample output user should be allowed to exit without playing.

Hint: Math.random() method will be very useful here. You may want to use break to exit without playing.

HeadsORTails Java Applicationl C:Program FilesJava idkl.8.0 1441biniavaw Enter 1 for HEAD 2 for TAIL Computer wins Enter 1 for HEAD 2 for TAIL; any other number to exit You win Enter 1 for HEAD 2 for TAIL; any other number to exit You win Enter 1 for HEAD 2 for TAIL; any other number to exit Computer wins Enter 1 for HEAD 2 for TAIL; any other number to exit 4 Mr. User played 4 times. Computer won 2 times. Mr. User won 2 times Goodbye HeadsORTails[Java Application] C Enter 1 for HEAD 2 for TAIL Mr. User played e times. Computer won e times. Mr. User won 0 times Goodbye

Explanation / Answer

The code is written in C++. "do while" loop is used. "num" variable is used to control loop and also to accept the user guess. The results are printed as mentioned. "rand()%2 +1" is used to generate random value 0 or 1 in each case.

#include<iostream>

#include <cstdlib>

using namespace std;

int main(){

int num, val;

int pla=0, win=0, loss=0;

do{

cout<<"Enter 1 for HEAD 2 for TAIL: "<<endl;

cin>>num;

if(num ==1 || num == 2){

val =rand()%2+1;

if(num == val){

win++;

cout<<"You Win"<<endl;

}

else if(num != val){

loss++;

cout<<"Computer Wins"<<endl;

}

pla++;

}

else{

break;

}

}while(1<= num<=2);

cout<<"Mr. User played "<<pla<<" times."<<endl;

cout<<"Computer won "<<loss<<" times."<<endl;

cout<<"Mr. User won "<<win<<" times."<<endl;

cout<<"GoodBye"<<endl;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote