How to use C+ to create a binary-to-decimal memory game The computer generates a
ID: 3550277 • Letter: H
Question
How to use C+ to create a binary-to-decimal memory game
The computer generates a random sequence of 0s and 1s creating a binary number.
In each round, the computer adds one more bit to the previous sequence, only displaying the added bit.
The user then needs to enter the decimal value of the binary number represented by the sequence of 0s and 1s displayed so far.
At the beginning of the game, the computer asks the user to provide a number between 0 and 5 to be used as the number of mistakes allowed per game.
A mistake happens every time the user enters a wrong decimal number.
When the user exhausts the number of allowed mistakes, the game terminates and the user loses.
Explanation / Answer
using namespace std;
#include<iostream>
#include<cmath>
#include<stdlib.h>
#include<time.h>
int main()
{int n,i=0,num,dec,odec=0,f=0;
srand (time(NULL));
cout<<"RULE !!! In every step a binary digit of the number will be given to you, u have to tell the decimal velue of the final binary number ";
cout<<"every next step will added in the left of the initial final no. ";
cout<<"enter the no. of mistakes allowed between 0 to 5 ";
cin>>n;
cout<<"here is the number in bynary ";
while(i<100)
{ num=rand() % 2;
cout<<num;
cout<<" enter the decimal value =";
cin>>dec;
odec+=num*pow(2,i);
if(dec==odec)
{ cout<<"Correct answer next digit= ";
}
else
{cout<<"wrong answer!!!";
f++;
cout<<"correct answer is"<< odec<<" ";
}
if(f==n)
break;
i++;
}
cout<<"You have gone till "<<i<<" moves";
return 0;
}
output ------------------------------------------------------------------------------------------------------------------
RULE !!!
In every step a binary digit of the number will be given to you, u have to tell the decimal velue of the final binary number
every next step will added in the left of the initial final no.
enter the no. of mistakes allowed between 0 to 5
4
here is the number in bynary
1
enter the decimal value =1
Correct answer
next digit=
1
enter the decimal value =3
Correct answer
next digit=
0
enter the decimal value =3
Correct answer
next digit=
0
enter the decimal value =3
Correct answer
next digit=
0
enter the decimal value =3
Correct answer
next digit=
1
enter the decimal value =4
wrong answer!!!correct answer is35
0
enter the decimal value =35
Correct answer
next digit=
0
enter the decimal value =35
Correct answer
next digit=
1
enter the decimal value =56
wrong answer!!!correct answer is291
0
enter the decimal value =291
Correct answer
next digit=
0
enter the decimal value =291
Correct answer
next digit=
1
enter the decimal value =289
wrong answer!!!correct answer is2339
1
enter the decimal value =4545
wrong answer!!!correct answer is6435
You have gone till 12 moves
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.