473,394 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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!
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <algorithm>
  5. #include <Windows.h>
  6.  
  7. using namespace std; 
  8.  
  9. void parseBuffer( char buffArray[], char alphaArray[], int sizeOne, int sizeTwo );
  10.  
  11. int main() 
  12. {
  13.  // precode alphabet into an array with null terminating character
  14.     char alphabetArray[]={'a','b','c','d','e','f','g','h','i','j','k','l','m',n',
  15. 'o','p','q','r','s','t','u','v','w','x','y','z','\0'};
  16.  
  17.     char buffer[11];
  18.     cout << "Enter the string up to 10 letters." << endl;
  19.     cin.get(buffer, 11);
  20.     parseBuffer(buffer, alphabetArray, 11, 11);
  21.  
  22.     system("Pause");
  23.     return 0;
  24.  
  25. void parseBuffer(char buffArray[], char alphaArray[], int sizeOne, int sizeTwo)
  26. {
  27.   int countFreq = 0;
  28.   cout << "This is buffer array: " << buffArray << endl;
  29.   cout << "This is alphabet array: " << alphaArray << endl<< endl;
  30.  
  31.  for(int i = 0; i < (sizeTwo - 1); i++)
  32.   {  
  33.    alphaArray[i]; 
  34.  
  35.   for(int j = 0; j < (sizeOne -1); j++)
  36.    { 
  37.      buffArray[j];    
  38.  
  39.    if(alphaArray[i] == buffArray[j] )
  40.    { 
  41.     countFreq = countFreq + 1; 
  42.    } 
  43.    else
  44.    {
  45.     countFreq = 0;
  46.    }  
  47. cout << "It's a match.  " << alphaArray[i] << "   shows up   " << countFreq << "  times." << endl << endl;      
  48.      }
  49.     }
  50.  }  // end "parseBuffer"  
  51.  
  52.  
Feb 3 '12 #1
2 3102
Banfa
9,065 Expert Mod 8TB
Loo at our cout statement, you don't need i and j you only need i. That should give you a clue. The cout needs to be inside the for loop on i and outside the for loop on j so you get 1 line of output for each entry in alphaarray.

The statements at lines 34 and 38 do nothing and can be removed.

Once you have the cout in the right place you will find you get the wrong answers. That is because you are resetting the value of countFreq in the wrong place. Think about the logic of when you want to reset that count to zero and start counting up again.
Feb 3 '12 #2
Hello Banfa,

Thank you for responding to my post. I apologize for the delay, I actually finally figured it out a couple of days ago thanks to some other coders like yourself.
And you are exactly right about where the holes in my logic were. I am going to paste in the corrected code. And, I'll be submitting another post by the end of the week, so please check in on me. Thank you again!
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <Windows.h>
  5.  
  6. using namespace std;
  7.  
  8. bool checkBuffer( char buffArray[], int buffSize ); 
  9. void parseBuffer( char alphaArray[], char buffArray[], int alphaSize, int buffSize );
  10.  
  11. int main()
  12. {     
  13.     char alphabetArray[]= "abcdefghijklmnopqrstuvwxyz";
  14.     char buffer[11]; 
  15.     cout << "Enter the string up to 10 letters." << endl;     
  16.     cin.get(buffer, sizeof(buffer) );
  17.     checkBuffer(buffer, sizeof(buffer) );
  18.     parseBuffer( alphabetArray, buffer, sizeof(alphabetArray), sizeof(buffer) ); 
  19.  
  20.     system("Pause");
  21.     return 0;
  22.  
  23. bool checkBuffer( char buffArray[], int buffSize )
  24. {     
  25.     if(buffArray, strlen(buffArray) == 0)
  26.       {  
  27.        cout << "The buffer is empty.  The program will end in 3 seconds. "  << endl;  
  28.        Sleep(3000);      
  29.        exit(1); 
  30. }
  31. void parseBuffer( char alphaArray[], char buffArray[], int sizeOne, int sizeTwo )
  32. {       
  33.     int countFreq = 0; 
  34.     for(int i = 0; i < strlen(alphaArray); i++ )
  35.      {
  36.       countFreq = 0; 
  37.       for(int j = 0; j < strlen(buffArray); j++)
  38.        { 
  39.         if( alphaArray[i] == buffArray[j] )
  40.            countFreq = countFreq + 1;         
  41.        }
  42.         cout << "The letter " << alphaArray[i] << "  matched " << countFreq << " times." << endl; 
  43.      }
  44. }
  45.  
Feb 13 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Mike Bartels | last post by:
Hi Everyone! I have two Arrays A and B. Both arrays are byte arrays with 7 bytes each. The contents of array A and B are the same A = {1, 2, 3, 4, 5, 6, 7}; B = {1, 2, 3, 4, 5, 6, 7}; When...
2
by: Mike | last post by:
Hi! I am trying to determine if the DataRow of one dataset contains the same data as the DataRow of another. I figured I can extract ItemArray's from each dataset and then compare them. 1) Is...
2
by: nobs | last post by:
Hi I have two byte arrays (each with a length of 8 bytes) an I'd like to compare them if they are equal. The operator == shouldn't work because its not a basic Data Type so I used the method...
3
by: Lance | last post by:
What is the fastest way to determine if two arrays that contain ValueTypes are equal? For example, lets say I have the following: Dim pt1 as New Drawing.Point(1, 2) Dim pt2 as New...
1
by: Jacob Thastrup | last post by:
Hi is there an easy way to compare arrays without going through each entry in the arrays? Say I have two arrays and I want to find entries that are not present in both. Thanks Jacob Thastrup
8
by: Turbot | last post by:
Hello, Anyone know how to compare two byte arrays without going through each item in the array. I actually want to compare two bitmap objects to see if they both contain the same picture bit...
2
by: Florian G. Pflug | last post by:
Hi Since sometime yesterday, my postgresql (7.4.5) reports "ERROR: cannot compare arrays of different element types", when I analyze a specific table in my database. Here is the tables...
9
by: mukeshhtrivedi | last post by:
We have MS Access 2000 Application (on Network- file server) and it workd fine as intended. However in one of our Windows XP computer (workstation) in bookd jobs module 10 digit field shows 8...
1
by: naqqash | last post by:
HI I am a beginner i VB please someone tell me how to compare values in array for exmple i've created an array for 10 day temperature now i want to know the maximum and minimum temperature in these...
9
by: capablanca | last post by:
How can I compare arrays to see if they are equal, they have the same amount of elements for example: array = {{2,4,5,7,8},{1,3,5,7,8},{3,4,5,7,8}, ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.