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!
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <Windows.h>
using namespace std;
void parseBuffer( char buffArray[], char alphaArray[], int sizeOne, int sizeTwo );
int main()
{
// precode alphabet into an array with null terminating character
char alphabetArray[]={'a','b','c','d','e','f','g','h','i','j','k','l','m',n',
'o','p','q','r','s','t','u','v','w','x','y','z','\0'};
char buffer[11];
cout << "Enter the string up to 10 letters." << endl;
cin.get(buffer, 11);
parseBuffer(buffer, alphabetArray, 11, 11);
system("Pause");
return 0;
}
void parseBuffer(char buffArray[], char alphaArray[], int sizeOne, int sizeTwo)
{
int countFreq = 0;
cout << "This is buffer array: " << buffArray << endl;
cout << "This is alphabet array: " << alphaArray << endl<< endl;
for(int i = 0; i < (sizeTwo - 1); i++)
{
alphaArray[i];
for(int j = 0; j < (sizeOne -1); j++)
{
buffArray[j];
if(alphaArray[i] == buffArray[j] )
{
countFreq = countFreq + 1;
}
else
{
countFreq = 0;
}
cout << "It's a match. " << alphaArray[i] << " shows up " << countFreq << " times." << endl << endl;
}
}
} // end "parseBuffer"
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!
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <Windows.h>
using namespace std;
int main()
{
char alphabetArray[]={'a','b','c','d','e','f','g','h','i','j','k',
'l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\0'};
char buffer[11];
cout << "Enter the string up to 10 letters." << endl;
cin.get(buffer, sizeof(buffer) );
int countFreq = 0;
// loop through alphabet array
for(int i = 0; i < sizeof(alphabetArray); )
{
cout << "The letter " << alphabetArray[i] << " matched " << countFreq << " times." << endl;
i++;
countFreq = 0;
for(int j = 0; j < sizeof(buffer); j++)
{
if( alphabetArray[i] == buffer[j] )
countFreq = countFreq + 1;
}
}
system("Pause");
return 0;
}
---------------------------------------
OUTPUT:
Enter the string up to 10 letters.
bbbbccccdd
The letter a matched 0 times.
The letter b matched 4 times.
The letter c matched 4 times.
The letter d matched 2 times.
The letter e matched 0 times.
The letter f matched 0 times.
The letter g matched 0 times.
The letter h matched 0 times.
The letter i matched 0 times.
The letter j matched 0 times.
The letter k matched 0 times.
The letter l matched 0 times.
The letter m matched 0 times.
The letter n matched 0 times.
The letter o matched 0 times.
The letter p matched 0 times.
The letter q matched 0 times.
The letter r matched 0 times.
The letter s matched 0 times.
The letter t matched 0 times.
The letter u matched 0 times.
The letter v matched 0 times.
The letter w matched 0 times.
The letter x matched 0 times.
The letter y matched 0 times.
The letter z matched 0 times.
The letter matched 1 times.
Press any key to continue . . .