Simon Says is a memory game where "Simon" outputs a sequence of 10 characters (R, G, B, Y) and the user must repeat the sequence. Create a for loop that compares the characters in two chracater vectors, simon_pattern and user_pattern. One point is added to user_score for each match. The game ends upon the first mismatch. Restrictions: The script must use break statement.

Respuesta :

Answer:

#include <stdio.h>  // header file

#include <string.h> // header file

int main()  // main function

{

char simonPattern[50];  // create array

char userPattern[50];  // create array

int userScore =0 ;  //variable declaration  

int k= 0; // variable declaration  

userScore = 0; // variable declaration

strcpy (simonPattern, "RRGBRYYBGY"); // copy the characters  

strcpy (userPattern, "RRGBBRYBGY");  // copy the characters  

for(k=0; simonPattern[k]!='\0'; k++)  // iterating the loop

{

if(simonPattern[k] == userPattern[k]) // checking the  condition

{

userScore++; // increment the value

}

else

{

break;  // break the loop

}

}

printf("User score = %d \n", userScore); // print the value

return 0;

}

Output:

user score = 4

Explanation of the program in steps is given below:

Step 1 : Declaring header files.

Step 2 : Creating main function.

Step 3 : Creating an array.

Step 4 : Declaring a variable then copy the characters with the help of "strcpy()" function.

Step 5 : Starts a loop, check the condition and incrementation in the value.

Step 6 : Print the value and return function.