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

Please write in C. Rock Paper, and Scissors. The user will input ’r’ for rock, ’

ID: 3853873 • Letter: P

Question

Please write in C.

Rock Paper, and Scissors. The user will input ’r’ for rock, ’p’ for paper, ’s’ for scissors. On any other entry your program should report an error. Your program must randomly generate its own rock, paper, or scissors, and compare it with the input 2 to determine the winner.

Example output:

Rock, paper, or scissors? r

I rolled rock. We tie!

Rock, paper, or scissors? s

Irolled paper. You win!

Rock, paper, or scissors? p

I rolled scissors. You lose!

Rock, paper, or scissors? a Error: you did not enter ’r’, ’p’, or ’s’!

Explanation / Answer

Hi,

Please find below the code for the program-

#include <stdio.h>
#include <stdlib.h>

int main() {
const char *messages[] = {
"Rock",
"Paper",
"Scissors"
};
const size_t messages_count = sizeof(messages) / sizeof(messages[0]);
char input[64];
while (1) {
scanf("%63s", input);
if(input==messages[rand() % messages_count]){
printf("I rolled %s we tie",messages[rand() % messages_count]);
}
else if(input=="Paper" && messages[rand() % messages_count]=="Scissors"){
printf("I rolled %s I won",messages[rand() % messages_count]);
}
else if(input=="Scissors" && messages[rand() % messages_count]=="Paper"){
printf("I rolled %s You won",messages[rand() % messages_count]);
}
else if(input=="Paper" && messages[rand() % messages_count]=="Paper"){
printf("I rolled %s we tie",messages[rand() % messages_count]);
}
else if(input=="Rock" && messages[rand() % messages_count]=="Rock"){
printf("I rolled %s we tie",messages[rand() % messages_count]);
}
else if(input=="Scissors" && messages[rand() % messages_count]=="Scissors"){
printf("I rolled %s we tie",messages[rand() % messages_count]);
}
printf("%s",messages[rand() % messages_count]);
printf("%s",input);
}
return 0;
}

Regards,

Vinay