finding common numbers

This is a discussion on finding common numbers within the C Programming forums, part of the General Programming Boards category; I am trying to find the numbers in the two arrays that are in common. Say they are: int A[3] ...

  1. #1
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72

    finding common numbers

    I am trying to find the numbers in the two arrays that are in common.

    Say they are:

    int A[3] = {1, 2, 5};
    int B[4] = {1, 5, 9, 10};

    Answer should say A \ B = {1, 5}


    Code:
    #include <stdio.h>
    
    int main (void)
    {
        int A[3] = {1, 2, 5};
        int B[4] = {1, 5, 9, 10};
    
        int x, z;
    
       for( int x = 0; x < 3; x++ )
        for( int z = 0; z < 4; z++ )
    
    }
    I have declared the arrays and I am think the rest is correct.

    I'm not sure how to find the common numbers or how to associate the loops with the arrays.

    New to C and Arrays.

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    What you have to do

    Two arrays lets say A and B

    Code:
        for i = 0  to n - 1 of A
            for j = 0 to n -1 of B
               if value of A[i] == B[j] then add to answer array
    before adding to the answer array just check its already there or not

  3. #3
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    Code:
    #include <stdio.h>
    
    int main (void)
    {
        int A[3] = {1, 2, 5};
        int B[4] = {1, 5, 9, 10};
    
        int x, z;
    
       for( int x = 0; x < 3; x++ )
        for( int z = 0; z < 4; z++ )
            if( A[x] == B[z] )
                printf("A \ B = {%d, %d\n}", A[x], B[z] );
    
        return 0;
    }
    My output: A \ B = {1,1}
    A \ B = {5, 5}

    The numbers are correct.

    What's wrong.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    When
    A[x] == B[z]
    then
    Code:
    printf("A \ B = {%d, %d\n}", A[x], B[z] );
    A[x] and B[z] are same you need to print any one.

    EDIT : You are declaring x and z twice.
    Last edited by zalezog; 11-09-2009 at 12:07 PM.

  5. #5
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    Ahh, makes since.

    So

    Code:
     printf("A \ B = {%d}", A[x] );
    Why is it printing it twice?

    It doing this, I dont understand it.

    A \ B = {1,1}
    A \ B = {5, 5}

    Im wanting jusr A \ B = {1, 5}

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Quote Originally Posted by RockyMarrone View Post
    What you have to do

    Two arrays lets say A and B

    Code:
        for i = 0  to n - 1 of A
            for j = 0 to n -1 of B
               if value of A[i] == B[j] then add to answer array
    before adding to the answer array just check its already there or not
    If A and B each held 10000 numbers or more, would you still use the same algorithm?

    The arrays are sorted. If it's a safe assumption that they are always sorted then there's a much quicker approach. Try studying the C++ standard library code for set_intersection.

    Can we also assume there are no duplicates?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by iMalc View Post
    If A and B each held 10000 numbers or more, would you still use the same algorithm?

    The arrays are sorted. If it's a safe assumption that they are always sorted then there's a much quicker approach. Try studying the C++ standard library code for set_intersection.

    Can we also assume there are no duplicates?

    Ok iMac Please tell me if the numbers are unsorted then the algo

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Quote Originally Posted by RockyMarrone
    Ok iMac Please tell me if the numbers are unsorted then the algo
    ... would be to sort them and then use an algorithm that works when they are sorted.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by laserlight View Post
    ... would be to sort them and then use an algorithm that works when they are sorted.
    So how many compressions will be there less the one above of yours or more ????
    And which will be faster

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Quote Originally Posted by RockyMarrone
    So how many compressions will be there less the one above of yours or more ????
    And which will be faster
    I think you mean "comparisons" instead of "compressions". Unfortunately, I do not really understand your sentence. What algorithms are you trying to compare?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by laserlight View Post
    I think you mean "comparisons" instead of "compressions". Unfortunately, I do not really understand your sentence. What algorithms are you trying to compare?
    Then without knowing the algo how can u write


    Quote:
    Originally Posted by RockyMarrone
    Ok iMac Please tell me if the numbers are unsorted then the algo
    ... would be to sort them and then use an algorithm that works when they are sorted.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    Quote Originally Posted by BB89 View Post
    Ahh, makes since.

    So

    Code:
     printf("A \ B = {%d}", A[x] );
    Why is it printing it twice?

    It doing this, I dont understand it.

    A \ B = {1,1}
    A \ B = {5, 5}

    Im wanting jusr A \ B = {1, 5}
    you have your printf("a\b...}"); in the middle of a for loop. so everytime you find a matching pair you are printing the entire line. you need to take it out of the for loop and print it only once. you can either stick the statement before the for loops, or after the for loops.

    also i cant see an easy way to print matching values inside nested for loops (due to the required format of the output, particularly the comma). you'll need to store the matching values somewhere and print them later, its about 5 extra lines.

    (you should also fix up your indenting. either 3 spaces or 4 spaces. pick one and use it all the time)
    Last edited by Brain_Child; 11-10-2009 at 04:32 AM.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Quote Originally Posted by RockyMarrone
    Then without knowing the algo how can u write
    Ah, but I do know the algorithm that iMalc was talking about
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    I figured it out.

    Thanks for the help.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Quote Originally Posted by BB89
    I figured it out.
    Great. What did you use in the end?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to handle multiple cases which use common code?
    By tmaxx in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 07:42 AM
  2. finding most common char in a string
    By scwizzo in forum C++ Programming
    Replies: 6
    Last Post: 11-23-2007, 12:22 PM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21