C++ compare two arrays and display duplicates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • codechick
    New Member
    • Feb 2012
    • 8

    #1

    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"
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.

    Comment

    • codechick
      New Member
      • Feb 2012
      • 8

      #3
      C++ compare two arrays and display duplicates - Code works now

      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!
      Code:
      #include <iostream>
      #include <string>
      #include <algorithm>
      #include <Windows.h>
      
      using namespace std;
      
      bool checkBuffer( char buffArray[], int buffSize ); 
      void parseBuffer( char alphaArray[], char buffArray[], int alphaSize, int buffSize );
      
      int main()
      {     
          char alphabetArray[]= "abcdefghijklmnopqrstuvwxyz";
          char buffer[11]; 
          cout << "Enter the string up to 10 letters." << endl;     
          cin.get(buffer, sizeof(buffer) );
          checkBuffer(buffer, sizeof(buffer) );
          parseBuffer( alphabetArray, buffer, sizeof(alphabetArray), sizeof(buffer) ); 
      
          system("Pause");
          return 0;
      
      } 
      bool checkBuffer( char buffArray[], int buffSize )
      {     
          if(buffArray, strlen(buffArray) == 0)
            {  
             cout << "The buffer is empty.  The program will end in 3 seconds. "  << endl;  
             Sleep(3000);      
             exit(1); 
      }
      void parseBuffer( char alphaArray[], char buffArray[], int sizeOne, int sizeTwo )
      {       
          int countFreq = 0; 
          for(int i = 0; i < strlen(alphaArray); i++ )
           {
            countFreq = 0; 
            for(int j = 0; j < strlen(buffArray); j++)
             { 
              if( alphaArray[i] == buffArray[j] )
                 countFreq = countFreq + 1;         
             }
              cout << "The letter " << alphaArray[i] << "  matched " << countFreq << " times." << endl; 
           }
      }

      Comment

      Working...