Thread: How do I compare two arrays?

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    32

    How do I compare two arrays?

    I'm a bit new to programming in c and have come across the problem of trying to compare two two-dimensional arrays (let's say 10 by 10).

    Could I write for instance

    if(array1==array2)
    printf("the arrays are the same");

    or do I have to write
    if(array1[10][10]==array2[10][10]")
    printf("the arrays are the same");

    ?

    I also would like to figure out, (if anyone can help me) how to figure out if an array is all full of zeroes, i.e. how to figure out if it's empty.

    Any help would be appreciated. Thank you.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You'll have to loop through and look at every value in the array.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    no you can use the strcmp from the <string.h> library.

    so if strcmp( array1, array2 ) == 0
    then they are equal
    if strcmp( array1, array2 ) == 1
    then array1 is bigger then array2
    if strcmp( array1, array2) == -1
    then array1 is less then array2
    there are only 10 people in the world, those who know binary and those who dont

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    32
    I got it to work. Thanks a lot.

  5. #5
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    >>Could I write for instance

    >>if(array1==array2)
    >>printf("the arrays are the same");

    This will not work. You have to run through with for loops to look at each element, i suggest using 1 variable set to 0, and if atleast 1 of the elements are not equal, then set it to 1, you can also use a break statement to save time by gettingout of the for loops. Then if its 1 at the end, its not equal, if its 0, it is equal.

    [edit]
    Stop replying to quickly. lol
    [/edit]
    The keyboard is the standard device used to cause computer errors!

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    to find out if the array is full of zeroes you could loop through the array and create a flag.
    initialise this flag to false to begin with and the go through the array and see if the value is zero. if it is make the flag true, cycle through and the moment you hit a non-zero value then you should break out of the loop and return the value of the flag which would be false. if the flag remains true all through the loop then its full of zeroes.
    there are only 10 people in the world, those who know binary and those who dont

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by kurz7
    no you can use the strcmp from the <string.h> library.
    This only works if they are character arrays. It won't work for any other data type. (ie: float, int, struct, etc)

    Just a simple loop will suffice.

    Quzah.
    Hope is the first step on the road to disappointment.

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. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. String Compare Using Dynamic Arrays
    By djwicks in forum C Programming
    Replies: 4
    Last Post: 03-31-2005, 08:01 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM