Thread: C++ compare two arrays and display duplicates

  1. #16
    Registered User
    Join Date
    Feb 2012
    Posts
    5
    Here, look at this:
    Code:
    for(int i = 0; i < sizeof(alphabetArray);i++ )    
       {
    
          countFreq = 0;
                  
          for(int j = 0; j < sizeof(buffer); j++)
             {    
               if( alphabetArray[i] == buffer[j] )
                  countFreq = countFreq + 1;
             }
    
           if(countFreq > 0)
               cout << "The letter " << alphabetArray[i] << "  matched " .......
           
    
        }
    By having cout after the inner for loop but before looping back to the start of the outer loop you can leave i++ where it was. Also, use if(countFreq > 0) to skip printing on those letters where there wasn't a match (if you want).

    Also, it looks like maybe you are printing out one too many lines? Maybe j < sizeof(buffer) should be j < sizeof(buffer)-1 ? And manybe the same for sizeof(alphabetArray)? You decide what, if anything, needs to be done.
    Last edited by Coder Head; 02-06-2012 at 06:06 PM.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Although it is correct, instead of writing this:
    Code:
    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'};
    Write the more concise version:
    Code:
    char alphabetArray[] = "abcdefghijklmnopqrstuvwxyz";
    Next, note that when used on an array, the sizeof operator results in the number of bytes of the elements of the array. alphabetArray actually contains 27 elements, each of one byte. As such, your loop that loops through alphabetArray also accesses alphabetArray[26], which is a null character. It probably does not make sense to include that, so it would be better to use strlen instead of sizeof (e.g., call strlen(alphabetArray) once just before the loop, then use the return value), or to subtract one.

    As a matter of style, we would typically write this:
    Code:
    countFreq = countFreq + 1;
    as:
    Code:
    ++countFreq;
    Finally, it is good to see that you have used indentation to show the structure of your code, but you could be a little more consistent, e.g.,
    Code:
       // 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;
           }
        }
    could have been:
    Code:
        // 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;
            }
        }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Aug 2011
    Posts
    28

    A fresher, cleaner version morphed per your tips and my original outline

    Hello Coder Head and Laserlight,
    All of your suggestions have been great to learn from.
    For the record, this practice project does include outputting
    all of the letters of the alphabet, matches and no matches.
    I am attaching the finished code. I am going to submit another version using the ASCII table and some new member functions.
    In the meantime, here it is with a nice definition of strlen commented in:
    Code:
    #include <iostream>
    #include <iomanip>
    #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, strlen(buffer) );
        
        checkBuffer(buffer, strlen(buffer) );
        parseBuffer( alphabetArray, buffer, strlen(alphabetArray), strlen(buffer) );
        
         // www.cplusplus.com definition of strlen:
         //char mystr[100]="test string"; defines an array of characters with a size of 100 chars,_
         // but the C string with which "mystr" has been initialized has a length of only 11 characters._
         // Therefore, while "sizeof(mystr)" evaluates to 100, "strlen(mystr)" returns 11.
        
        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; 
          }
    }
    -----------------------
    OUTPUT:
    Enter the string up to 10 letters.
    itsahwzome
    The letter a matched 1 times.
    The letter b matched 0 times.
    The letter c matched 0 times.
    The letter d matched 0 times.
    The letter e matched 1 times.
    The letter f matched 0 times.
    The letter g matched 0 times.
    The letter h matched 1 times.
    The letter i matched 1 times.
    The letter j matched 0 times.
    The letter k matched 0 times.
    The letter l matched 0 times.
    The letter m matched 1 times.
    The letter n matched 0 times.
    The letter o matched 1 times.
    The letter p matched 0 times.
    The letter q matched 0 times.
    The letter r matched 0 times.
    The letter s matched 1 times.
    The letter t matched 1 times.
    The letter u matched 0 times.
    The letter v matched 0 times.
    The letter w matched 1 times.
    The letter x matched 0 times.
    The letter y matched 0 times.
    The letter z matched 1 times.
    Press any key to continue . . .
    Last edited by codechick; 02-06-2012 at 11:55 PM.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Err... now you're overdoing the strlen

    The idea behind strlen is that you have strings and you want to loop over the characters in the strings.

    The thing is, when you're reading into buffer, buffer is just an array that does not contain a string, therefore sizeof(buffer) is correct whereas strlen(buffer) is wrong.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Hi Laserlight,
    Thank you for guiding me on the fine points of loops and data structures.
    I just logged in right now, so I need to take a look at exactly what you are saying.
    It's exactly these kind of details that I am really focused on learning.
    I have a different question for today that I will post separately right now.

  6. #21
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Hello Coder Head,
    Happy Thursday, I hope your day is going fine.
    This post is about a different subject, getting my first Programming gig.
    I asked Alex, the head of this site, if it was appropriate to "talk shop" and he gave me the green light.
    However, if you choose not to, that's totally fine as well.

    The deal is I have a phone interview next Wed. for a Software Developer Engineer position at Amazon.
    My path in to getting the interview is most likely from an employee referral from a friend's husband.

    Anyway, I am a recent graduate in Computer Science. I have a Computer Science Certificate,
    a bachelors degree in Communications, and 7 years experience as an Analyst in the health insurance industry.
    In addition to programming, I have taken A+ Certification, built my own computer. Taken Photoshop and Flash, and
    created my own movie which is on my site. I coded my site in HTML and CSS right after gradutation in May 2011.
    I've also trained in SQL Server, ASP.NET, and Unix, so I feel I have very well-rounded training.
    My heart is with the code, hence my nickname Code Chick which is also on my site. My goal is for the end user to
    have an easier and more efficient experience. I'd like to program applications and/or games. I'd really love to use physics
    in programming and artificial intelligence. However, as I am a newbie, I'd just like to get better at being a Programmer using
    object oriented languages, and have a lot of work to do.
    The position Amazon is interviewing me for asks for 2+ years experience. I clarified with them, that I am an
    Advanced Beginner. I basically know all the fundamentals of programming, however I need practice, practice, practice.
    So, the $64? is do you have any tips on preparing for an interview that will ask me about data structures, algorithms,
    design patterns and coding of course? I have "Cracking the Coding Interview" and can obviously bone up and practice as
    much as possible between now and then. My question is: "What is the most effective manner for me to interview for an SDE
    position while I am currently an Advanced Beginner?" Knowing myself well, I know that it won't take long before I'm making
    a significant contribution and becoming Code Chick full force.
    Thanks for listening, I wanted you and Laserlight to know what my situation is and gain from your professional expertise.

  7. #22
    Registered User
    Join Date
    Aug 2011
    Posts
    28
    Hello Laserlight,
    I just posted my question regarding an upcoming SDE interview I have with Amazon next week.
    I think that you are able to see it in this thread. If not, please let me know if you'd like to see it.
    Any advice you feel like providing, or not, would be great. If you or Coder Head aren't comfortable
    with "talking shop" that's fine. I'll just catch you on the next project, which I'm getting ready to
    submit today or tomorrow, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare Arrays
    By AmbliKai in forum C Programming
    Replies: 16
    Last Post: 11-27-2007, 11:04 AM
  2. How to Compare elements in arrays
    By axe in forum C Programming
    Replies: 13
    Last Post: 11-16-2007, 03:04 AM
  3. String Compare Using Dynamic Arrays
    By djwicks in forum C Programming
    Replies: 4
    Last Post: 03-31-2005, 08:01 PM
  4. Replies: 6
    Last Post: 11-28-2004, 11:01 AM
  5. How do I compare two arrays?
    By lime in forum C Programming
    Replies: 6
    Last Post: 07-13-2003, 09:55 PM