C/C++ Solve C++ "Simon Says" Memory Game with a for Loop

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
The discussion centers around creating a memory game called "Simon Says," where the user must replicate a sequence of characters. The task involves writing a for loop in C++ to compare two strings character by character, incrementing a score for each match until a mismatch occurs, at which point the loop should exit using a break statement. Participants clarify that the comparison should start from index 0 and continue until a character differs or the end of the string is reached. A sample code snippet is provided, and there is a focus on using a for loop instead of a while loop, emphasizing that the length of the string can be determined using the size or length methods. The conversation also touches on the nuances of C++ strings, noting that they do not need to be null-terminated and can contain null characters within.
ineedhelpnow
Messages
649
Reaction score
0
"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 two strings starting from index 0. For each match, add one point to userScore. Upon a mismatch, exit the loop using a break statement. Ex: The following patterns yield a userScore of 4:

simonPattern: R, R, G, B, R, Y, Y, B, G, Y
userPattern: R, R, G, B, B, R, Y, B, G, Y
Sample program:

Code:
#include <iostream>
#include <string>
using namespace std;

int main() {
   string simonPattern;
   string userPattern;
   int userScore = 0;
   int i = 0;
   
   userScore = 0;
   simonPattern = "RRGBRYYBGY";
   userPattern  = "RRGBBRYBGY";

   <STUDENT CODE>

   cout << "userScore: " << userScore << endl;

   return 0;
}

having trouble understanding this activity (Wasntme) can someone like clarify and help me out with it?
 
Technology news on Phys.org
You want to compare the two pattern strings character by character (a looping construct would be appropriate with $i$ as the counter or index), and then if the corresponding characters in the two strings are equal, you want to continue, otherwise you want to quit. Then the score will be calculated from the value of $i$ when the loop finished. So, it sounds like an if-else statement will be needed inside the loop.

Can you make an attempt based on this?
 
Hi,
Rephrase your problem. Given two strings s1 and s2 of the same length l, find the first index i with s1 unequal to s2 or if no such i exists, set i=l. Since C++ strings "start" at index 0 and s1[l]==s2[l]=='\0', this solves your problem.

Student code without a break in the program:

Code:
while(i<10 && simonPattern[i]==userPattern[i]) {
   i++;
}

Now modify the while loop with an if statement in the body that uses break. You'll also want to change the "boolean" control expression of the while loop.

This is an example showing that break is never necessary, but break is merely a programmer convenience.

Oops. Sorry, I didn't see that the problem asks to use a for loop; also I didn't notice the use of the string class, but was thinking of C strings. My comment about break was really the reason I replied in the first place.
 
Last edited:
johng said:
Since C++ strings "start" at index 0 and s1[l]==s2[l]=='\0', this solves your problem.
C++ strings (objects of class [m]string[/m]) don't have to be null-terminated and may contain '\0' in the middle.

johng said:
Student code without a break in the program:

Code:
while(i<10 && simonPattern[i]==userPattern[i]) {
   i++;
}
The problem asks to write a [m]for[/m] statement, which is more appropriate for loops with a fixed number of iterations. The length of the simonPattern string can be found as [m]simonPattern.size()[/m] or [m]simonPattern.length()[/m].
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
2
Views
11K
Replies
1
Views
1K
Replies
5
Views
3K
Replies
22
Views
3K
Replies
40
Views
3K
Replies
23
Views
2K
Back
Top