C++ compare two arrays and display duplicates

This is a discussion on C++ compare two arrays and display duplicates within the C++ Programming forums, part of the General Programming Boards category; Hello all, I am working on a practice textbook question that asks a user to input 10 characters that go ...

C++ compare two arrays and display duplicates

Hello all, I am working on a practice textbook question that asks a user to input 10 characters that go into an array and then that array is compared to a hard-coded alphabet array. The output should display the number of duplicates, if any, per letter. For example:
"There are 2 a's."
"There are 0 b's."
"There are 3 c's." .....and so on.
My code is correctly counting the number of duplicates (or not) between the 2 arrays. However, the problem is that it is displaying the count EVERY TIME THE LOOP ITERATES. I only need it to display THE TOTAL COUNT.
I tried moving the "cout" statement below the loop which doesn't work because it needs the [i] and [j] from where it loops thru the arrays.
Please point out where my error is, thanks in advance!

If you don't want it done repeatedly then it should not be inside the loop. Just move it outside, down to line 48.

Hi iMalc,

Thanks for the response....the "cout" statement isn't working correctly after the "if" statement or the "else" statement.
If it's in the "if" section, it only outputs matches, however, each time it finds a match instead of the total.
If it's in the "else" statement, it outputs the "cout" statement every time only also with the -0- matches.

Hi. I don't see where the cout statement uses the j variable, just the i one. It should be okay to make the cout statement the last line in the i loop.

Hi whiteflags,

Yes, I feel that surrendering feeling...code that doesn't work can make you feel like that...; )
I tried your suggestion and put the "cout" statement in the "i" loop, and it outputs the "cout" statement every time only also with the -0- matches.
You're right about the "j" loop, I won't be outputting anything in relation to it, it's the alphabet array used to compare to the User input. Please let me know if you have any other suggestions, thx!

The outer loop sets up the first letter of the alphabet - a. Then it goes to the inner loop and compares that "a" to all of the letters in the buffer. So each iteration of the inner loop compares "a" with one letter in the buffer, one for each iteration, and each time a match is found countFreq is incremented. But what if there was no match for "a" in the buffer, are you going to print "It's a match"? Surely you have to determine if a match occurred or not before you print anything out, right?

After the inner loop has compared "a" to all of the characters in the buffer, then the loop ends. Then you can print out "It's a match" if a match actually occurred. Also, somewhere along the way, countFreq will have to be reset back to zero to begin anew.

Oh, and since there are 26 letters in the alphabet, shouldn't you have a loop somewhere that loops 26 times? I don't see that in your code. Rewrite your code and see how well it works. If you have anymore problems, re post your program and tell us what errors you are getting. Also, you did mention that if you moved the cout statement outside the loop then i and j would be out of scope. In that case, you could have defined i and j outside of the outer loop so that it would be in scope for the rest of the program, like so:

No, leave those braces in and properly indent your code by more than just one space instead. That is what makes your code less clear: there is no structure hinted by consistent and sufficient indentation. The braces are fine.

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens

Hello all, thank you big time for all of your feedback...; ) !!
I just got my PC back from the shop today and got your posts.
Let me look at it all of your tips carefully, and follow up shortly.
Thanks for giving the techie luv.

Hi all,
Thank you for your tips.
I want you to know that I tried every tweak you suggested, as I (more than anyone) want this to work!
Here are the problems:
regarding: "somewhere along the way, countFreq will have to be reset back to zero to begin anew."

1. If I don't put the "else" statement to "reset" freqCount to -0-, it doesn't reset. I've tried putting it right above the "if" statement, inside the "if" statement, below the 2 loop statements, etc...where I have it now, with the "else" statement is the only place it is resetting.

2. While I really like the idea of putting the "int i" and "int j" declarations outside of the "for" loop, if I do this, the "cout" statement doesn't recognize "alphabetArray[i]" and outputs a blank section in that part of the "cout" if I put it after the two loops' curly braces.

I am posting the code with the basic adjustments. I am also posting the output, however, only with the match statements as otherwise it's too long. I await your wizdom:

-----------------------------------------------------------------------------------
OUTPUT:
Enter the string up to 10 letters.
aaaabbbbcc
This is buffer array: aaaabbbbcc
This is alphabet array: abcdefghijklmnopqrstuvwxyz

It's a match. a shows up 1 times.
It's a match. a shows up 2 times.
It's a match. a shows up 3 times.
It's a match. a shows up 4 times.
It's a match. b shows up 1 times.
It's a match. b shows up 2 times.
It's a match. b shows up 3 times.
It's a match. b shows up 4 times.
It's a match. c shows up 1 times.
It's a match. c shows up 2 times.

Just for starters, lines 34 and 38 do nothing, so you can remove them with no effect.
When you declare i and j outside the two for loops, you don't need to define them again inside the for loops. Doing so undoes putting them outside. But don't worry, you don't need to do that anyway. But if you WERE going to do that, then your for loop would look like this: for(; i < (sizeTwo - 1); i++). Notice how the declaration of i is missing from the for loop.

Concerning your cout statement: the inner for loop compares a single alphabet character to all of the buffer characters and keeps count of how many matches, if any, there were. After that happens, then use cout. In other words, the alphabet character "a" is compared to each element of the buffer and counts how many matches occurred, then the inner loop ends. That's when you print out the results. Oh wait..... what if there were no matches for "a"? Do you still do cout? Of course not, you only print out when there are matches. So before you use cout, you need to determine if there were matches, if not, then skip cout.

Sorry I can't just spill the beans clearly, because you have to think. Understand clearly what that inner for loop is doing first. That's critical. It works like I said above. And I will give you one clue - get rid of that else statement and the resetting of countFreq. Resetting countFreq needs to go after cout, since you print it out, right? Also, since there are 26 letters in the alphabet, then surely you need to loop 26 times somewhere, right? I don't see that in your code.

Make changes in your code, and re post it when you have done that so we can see what you have.

-------------------------------------------
OUTPUT:
Enter the string up to 10 letters.
bbbbccccdd
this is the count0
this is the count0
this is the count0
this is the count0
this is the count0
this is the count0
this is the count0
this is the count0
this is the count0
this is the count0
this is the count0
this is the count1
this is the count2
this is the count3
this is the count4
this is the count4
this is the count4
this is the count4
this is the count4
this is the count4
this is the count4
this is the count4
this is the count4
this is the count4
this is the count4
this is the count4
this is the count5
this is the count6
this is the count7
this is the count8
this is the count8
this is the count8
this is the count8
this is the count8
this is the count8
this is the count8
this is the count8
this is the count8
this is the count8
this is the count8
this is the count8
this is the count9
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count10
this is the count11
Press any key to continue . . .

Well, I'll be a son of a gun! You're getting closer! I think you are only two small adjustments away from getting this to work.

This statement: sizeof(alphabetArray) took care of looping the 26 times so that every letter of the aphabet gets compared to the buffer.

The only thing the outer for loop does is select another alphabet character for the inner loop to compare with every character in the buffer. Again, the solution to this problem lies in your clear understanding of the operation of the inner for loop.

Now, when the program starts, the outer loop provides us with the letter "a" from the alphabet. Then we move into the inner loop to compare "a" with every character in the buffer. One loop compares "a" with the first char of buffer. If the buffer size is 10, then we have to loop 10 times to compare "a" with the whole buffer to see how many times "a" matches. Surely you wait until all 10 iterations occur before you cout anything, right? In other words, it takes 10 loops to find out how many "a" there are in the buffer, then another 10 loops to find out how many "b" there are in the buffer, and 10 more loops for "c" ,etc, etc. So then, where do you place your cout statement? Think, O wise one!

After you count how many "a" there are, you need to reset countFreq for the "b" comparison and then after that reset it for the "c" comparison, etc. And you know that you have to print out countFreq, so logically after cout you no longer need the value in countFreq. So maybe then is a good time to reset it? In other words, after the end of each 10 iterations, you have to reset countFreq for then next set of 10 iterations.

Also, you don't want to cout anything if there was no match. If "a" was compared to all 10 characters in the buffer with 10 loops, and there was no match, then you don't want to print out anything. But if there was a match, say 4 matches, then you would print that out.

Also, why not restore your original cout statement, that was good.

Fix your code and re post it if your program doesn't work. And of course, let us know if it does! : )

You are almost there. Like I said, it depends on how well you grasp what is going on in your inner and outer loop!

REALLY! It works now thanks to all of your assists!

Hello Coder Head,

Thank you for sticking with me through the journey.
You gave me encouragement I was on the right track, and just needed a couple of key adjustments. This helped me to be CONFIDENT that I could figure it out with what I have.
So, here is the raw, and WORKING version of the code.
If there's anything else, please advise. Else I will post a final, full-on version with a function. Enjoy the code and output!