Problem 1. Consider the following game for two players Pl and P2. P1 picks an in
ID: 3590939 • Letter: P
Question
Problem 1. Consider the following game for two players Pl and P2. P1 picks an integer i between 1 and an upper limit k. P2 tries to determine i by a series of guesses. A guess consists of P2 naming a number j and P1 stating whether J 1. The game is over when P2 guesses J-1 Assuming an optimal strategy is used, how many guesses does P2 need in order to be sure to Briefly explain each of your answers. find i in each of the following cases? 1, k= 1023. 2, k = 1023 and P1 tells P2 before the first guess that i is even. 3. k = 1023 and PI tells P2 after answering the second guess that i is even. 4, k = 2047 and Pi tells P2 before the first guess that i is divisible by 8. 5. k = 255 and P1 tells P2 be 6. A = 64 and Pi tells P2 before the first guess that i is a power of 2, that is, t-2p for some p 20. z+. fore the first guess that i is a square number, that is, viExplanation / Answer
There are total 6 programs:
1.
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main(){
int k,count,guess;
srand(time(NULL));
int a = rand()%1023;
count = 0;
while(1){
cout << "Enter your guess :";
cin >> guess;
count++;
if (a == guess)
break;
if (guess > a){
cout <<"Guess is high" << endl;
}
if (guess < a){
cout <<"Guess is low" << endl;
}
}
cout << "Your guessed it right. Yout guess count is " << count << endl;
}
2.
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main(){
int k,count,guess;
srand(time(NULL));
int a = rand()%1023;
while (a%2!=0)
a = rand()%1023;
count = 0;
cout << "The number is even ";
while(1){
cout << "Enter your guess :";
cin >> guess;
count++;
if (a == guess)
break;
if (guess > a){
cout <<"Guess is high" << endl;
}
if (guess < a){
cout <<"Guess is low" << endl;
}
}
cout << "Your guessed it right. Yout guess count is " << count << endl;
}
3.
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main(){
int k,count,guess;
srand(time(NULL));
int a = rand()%1023;
while (a%2!=0)
a = rand()%1023;
count = 0;
while(1){
cout << "Enter your guess :";
cin >> guess;
count++;
if (a == guess)
break;
if (guess > a){
cout <<"Guess is high" << endl;
}
if (guess < a){
cout <<"Guess is low" << endl;
}
if (count ==2)
cout << "The number is even ";
}
cout << "Your guessed it right. Yout guess count is " << count << endl;
}
4.
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main(){
int k,count,guess;
srand(time(NULL));
int a = rand()%2047;
while (a%8!=0)
a = rand()%2047;
count = 0;
cout << "The number is divisible by 8 ";
while(1){
cout << "Enter your guess :";
cin >> guess;
count++;
if (a == guess)
break;
if (guess > a){
cout <<"Guess is high" << endl;
}
if (guess < a){
cout <<"Guess is low" << endl;
}
}
cout << "Your guessed it right. Yout guess count is " << count << endl;
}
5.
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main(){
int k,count,guess;
srand(time(NULL));
int a;
int rem = 1;
int i = 2;
while (rem!=0){
a = rand()%255;
for (int i = 1; i<15; i++){
if (a % i == 0){
rem = 0;
break;
}
}
}
count = 0;
cout << "The number is a square number ";
while(1){
cout << "Enter your guess :";
cin >> guess;
count++;
if (a == guess)
break;
if (guess > a){
cout <<"Guess is high" << endl;
}
if (guess < a){
cout <<"Guess is low" << endl;
}
}
cout << "Your guessed it right. Yout guess count is " << count << endl;
}
6.
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main(){
int k,count,guess;
srand(time(NULL));
int a;
int rem = 1;
int i = 1;
while (rem!=0){
a = rand()%64;
i = i * 2;
rem = a % i;
}
count = 0;
cout << "The number is a power of 2 ";
while(1){
cout << "Enter your guess :";
cin >> guess;
count++;
if (a == guess)
break;
if (guess > a){
cout <<"Guess is high" << endl;
}
if (guess < a){
cout <<"Guess is low" << endl;
}
}
cout << "Your guessed it right. Yout guess count is " << count << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.