
Compare array elements
I have a 2 dimensional integer array of size NxN with values from 0 to N^21. 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.

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.

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 02, 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.

If the numbers can only range from 0 to N^21, as you say, then all you do (and all quzah did, although he dressed it up as a 2D array) is make an array that goes from 0 to n^21, 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.)

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.

Never mind, I tested my idea and it works!