Compare array elements

This is a discussion on Compare array elements within the C Programming forums, part of the General Programming Boards category; I have a 2 dimensional integer array of size NxN with values from 0 to N^2-1. I'll be scanning these ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174

    Compare array elements

    I have a 2 dimensional integer array of size NxN with values from 0 to N^2-1. I'll be scanning these values in from the user so I need to check that all the integers entered are correct (there cannot be duplicates). Would it be easier to first organize the integers from smallest to largest and then simply check each neighbouring integers (which will also make finding the min and max easy) or would it be shorter and simpler to merely compare all the values?

    I would prefer the second approach but I also couldn't think of how I'd start it.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not sure if I am understanding correctly.

    Say N is 3.

    3x3, and you want them to enter 0 to 8? They can enter them in any order they want, but they can't enter duplicates? Initialize your array to hold N*N (9 in our case). Then:
    Code:
    if( grid[ number % N ][ number / N ] != (N*N) )
        printf( "dupe, try again\n" );
    else
        ...set it to number
    That looks about right.


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

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Yes, your interpretation was correct, but I don't see how your code will be doing what's needed.

    Code:
    if( grid[ number % N ][ number / N ] != (N*N) )
    So in this case N=3, number is whatever the user entered in the position (i,j) where i and j range from 0-2, and thus what your line of code seems to be looking for is the entries i == number % 3 and j == number / N, if the value at this position != 9 then there are duplicates? That doesn't quite sound right.

    If I can maybe check the first value at i=0, j=0 and then compare it to all other values, then compare the value at i=1, j=0 to all the other values except i=0, j=0... etc.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If the numbers can only range from 0 to N^2-1, as you say, then all you do (and all quzah did, although he dressed it up as a 2-D array) is make an array that goes from 0 to n^2-1, set all the elements to some sentinel value (such as say N*N, which is an invalid value), and then as the numbers come in you change the corresponding array element. (I.e., when they enter 5, you change array[5]; when they enter 17, you change array[17], etc.)

  5. #5
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    So if I'm understanding this correctly, I can initialise an N*N array's elements all to zero, then as I scan each value in from the user, if the first number is, say, 7 then I should add 1 to array[7]. After I check through all the inputs, since I've checked N*N integers, if I look through my array and find that one of its values is not 1, then either there has been a duplicate (one of the values in the array will be 2) or there is a value that is outside of the array.
    Would this work? I'm kind of sceptical about trying to access an element out of the array size.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Never mind, I tested my idea and it works!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-11-2008, 11:57 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. Table elements compare...
    By ihtus in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2006, 10:30 AM
  4. How to compare structure elements
    By khpuce in forum C Programming
    Replies: 6
    Last Post: 04-10-2005, 12:40 PM

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