wRITE A PSEUDOCODE FOR THIS PROGRAM #include<stdio.h> #include <time.h> #include
ID: 3729627 • Letter: W
Question
wRITE A PSEUDOCODE FOR THIS PROGRAM
#include<stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PASSES 3 /*constant for maximum number of passes*/
int turn=0; /*variable to keep the track of turns*/
int passesLeft[]={MAX_PASSES,MAX_PASSES}; /*array to hold the passes left for two players*/
int previouslyPassed[]={0,0}; /*another array, to denote if the player has previously passed a turn (continuously)*/
/*method to prompt the user and receive the player number (either 1 or 2)*/
int getPlayerNumber(){
int num=0;
printf("Player Number: ");
scanf("%d",&num);
fflush(stdin); /*flushing the buffer*/
if(num==turn){
/*valid player number*/
return num;
}else{
if(num==1 || num==2){
/*not the right turn*/
printf("You have to wait for your turn ");
}else{
/*entered something other than 1 and 2*/
printf("Invalid number, try again! ");
}
/*prompt again and return*/
return getPlayerNumber();
}
}
/*method to prompt the user to enter a guess and return it*/
int getGuess(){
printf("Enter your guess: ");
char text[10];
/*getting guess as text*/
scanf("%s",&text);
if(strcmp(text,"PASS")==0){
/*player entered PASS, returning -1 to denote PASS*/
return -1;
}else{
/*converting text to integer and returning it*/
int num=atoi(text);
return num;
}
}
int main(){
/*seeding random number generator*/
srand(time(NULL));
/*generating a number between 1 and 100*/
int number=rand()%100 +1;
/*deciding which player should play first, will generate eithe 1 or 2*/
turn=rand()%2 +1;
printf("Player %d will play first ",turn);
/*variable to keep the track of user guess*/
int guess=0;
/*loops until the right guess is entered*/
while(guess!=number){
/*getting valid player number*/
int p=getPlayerNumber();
/*getting guess*/
guess=getGuess();
if(guess==-1){
/*player choose to PASS*/
if(passesLeft[p-1]==0){
/*no passes left*/
printf("You dont have any passes left! ");
}else{
/*checking if the player passed a turn consecutively*/
if(previouslyPassed[p-1]==1){
printf("You cant pass for two times in a row! ");
}else{
/*passing the turn, decrementing the passes left*/
passesLeft[p-1]--;
/*displaying the remaining pass details*/
printf("You have passed %d number of times, you have %d more times left ",MAX_PASSES-passesLeft[p-1],passesLeft[p-1]);
/*setting a flag in previouslyPassed array to show that the player has passes a turn */
previouslyPassed[p-1]=1;
/*swapping the turns*/
if(turn==1){
turn=2;
}else{
turn=1;
}
}
}
}else{
/*checking if the guess is higher than the number*/
if(guess>number){
printf("Too high! ");
}else if(guess<number){
/*the guess is lower than the number*/
printf("Too low! ");
}else{
/*right guess*/
printf("Right guess! Player %d wins ",p);
}
/*setting the previouslyPassed flag to 0, to denote that the player has not passed the current turn*/
previouslyPassed[p-1]=0;
/*swapping the turns*/
if(turn==1){
turn=2;
}else{
turn=1;
}
}
}
}
Explanation / Answer
Done the psudoCode. Please let me know if you have any doubts.
set MAX_PASSES to 3 /*constant for maximum number of passes*/
Set turn to 0; /*variable to keep the track of turns*/
declare int array to hold the passes left for two players
declare int array to denote if the player has previously passed a turn (continuously)
function : getPlayerNumber. It is method to prompt the user and receive the player number (either 1 or 2)
Pass out: int
pass in : Nothing
set variable num to 0
read user input for Player Number and set it to local variable num.
check if the player Number if valid or not.
if num = turn ( means a valid player)
return num
else
if num is equal to 1 or 2 . That means its not right turn
print That you have to wait for your turn
else
print Invalid number, try again
call getPlayerNumber again
end funtion getPlayerNumber
/*method to prompt the user to enter a guess and return it*/
function getGuess
Pass out: int
Pass in : Nothing
create char[10];
print Enter your guess
read user input
if input = "pass"
return -1
else
convert string enter by user to int and return
end funtion getGuess
Function main
Pass In: nothing
declare int guess = 0
generate a sequence of pseudo-random integer between 1 and 100
decide which player should play first, will generate either 1 or 2
print Player choose above will play first turn
While the right guess is not entered
int p = call function getPlayerNumber
int guess = call getGuess
if guess = -1 that means player choose to PASS
if no more passes are left
print You dont have any passes left
else
if player passed a turn consecutively
print You cant pass for two times in a row
else
decrement passes left by 1
print How many times user has passed and how any more times left
set previouslyPassed =1
if turn=1
set turn = 2
else
set turn =1
else if guess>number
print number is too high
else if guess <number
print number too low
else
print Right guess and print which Player wins
reset any variables if needed in the start of the loop like counter to maintain if user has passed already or not
if turn =1
set turn to 2
else
set turn to 1
endwhile
end function main
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.