Thread: Compare Arrays

  1. #1
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68

    Compare Arrays

    Hi Everyone,

    I'm trying to compare two 9 element arrays. Their contents have to be identical but not necessarily in order.

    For example:

    Code:
    Array1 = 000123000
    Array2 = 010020030
    These would return as identical.

    I guess i could spend some time doing it manually but i think theres an easier way?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I'd have 2 loops, 1 nested in the other. I dunno, someone's probably got a better idea

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you are saying that all elements of one array has to occur in the other array exactly the same number of times, or would
    Code:
    Array1 = 000123001
    Array2 = 111020030
    also be equal?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Ah, sorry matsp, should have been more specific.

    The elements have to occur the same number of times. Just their position can change.

    I know how to do it if the positions are the same but its this point which is confusing me.

    Thanks.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Keep track of if each element has occurred, eg:

    Code:
    int element[10] = {0};  /* 10 digits, [0,9] */
    Then go through each array with the loops like I said, setting element[digit] to 1 every time you find a unique digit.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I guess one way would be to sort both arrays and compare them in sorted order...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Or that... which would greatly help if you had to compare n arrays.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Tis a good idea! Once sorted is there a way to compare both arrays in one go. Since they will both now be identical in position and value?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, if the arrays are sorted, you should be able to just compare the corresponding elements until you either find a difference or reach the end of the array. Pretty basic comparison really.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    what about when the number of arrays are unknown?

    i can do it for an unknown number of elements but if there is more than one array i'm not sure how to do it.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You could use a counter. Use a map to store a value and increment that value the number of times that specific character or number occours in the array.
    Then iterate your map for both arrays and compare all the character occourances. If all characters have the same count, they're equal.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by AmbliKai View Post
    what about when the number of arrays are unknown?

    i can do it for an unknown number of elements but if there is more than one array i'm not sure how to do it.
    Again, not sure what you want to do, is it one array that you need to find a match for, or multiple arrays, any of which could possibly be equal to any other?

    Although, really, it makes little difference - you only compare one array to one other array at any time.

    Obviously, if you have an unound number of arrays, you either have to not store any "old" arrays, or you have to use some dynamic allocation method or such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    sort and then strcmp

  14. #14
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Ok, i've finished the part of my program which generates the arrays.

    They end up in a 2 dimensional array:
    for example:

    Code:
    disjoint_subset_row_array = 
    002040009
    002045000
    000045600
    002040009
    Notice the top row and bottom row are identical.

    My problem is this:
    I want to compare the ROWS in the array and find how many of them are identical and to return that row.

    So for the example above:

    Code:
    Rows : 0, 3.
    002040009
    Its just that the number of rows changes each time. As does the number which are identical (if any)
    I'm a bit stuck on this!!
    Any help would be very greatly appreciated.
    Thank you.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, I guess you get these in a file or some such, right?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Compare elements in arrays
    By axe in forum C Programming
    Replies: 13
    Last Post: 11-16-2007, 03:04 AM
  2. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  3. String Compare Using Dynamic Arrays
    By djwicks in forum C Programming
    Replies: 4
    Last Post: 03-31-2005, 08:01 PM
  4. How do I compare two arrays?
    By lime in forum C Programming
    Replies: 6
    Last Post: 07-13-2003, 09:55 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM