# Thread: Array Check for uniqueness...Need Help FAST!!

1. ## Array Check for uniqueness...Need Help FAST!!

I am writing a magic square program that takes a 4X4 square and checks to see if it is a magic square. I have gotten everything to work exect that I don't know how to check if numbers that the user entered only appear in the 4X4 array once (i.e. Only one 1 value can be used in the square). What is the easiest way to do this? Am trying to use two for loops to accomplish this but don't know how to exactly do this... the for loops are also making sure that the numbers entered are in the range of 1 thru 16. Thanks!

for(row=0; row<4; row++)
{
for(col=0; col<4; col++)
{
int currentValue = grid[row][col];
if(currentValue<1 || currentValue > 16)
{
cout << "Eek ... a value is out of range: << currentValue
<< endl;
}
//stuff goes here to see if numbers are unique I think, but
//how??
}
}

2. you can do it like this

for(int i=0;i<16;i++)
{

for(int k=i+1;k<16;k++)
{

if(array(i)==array(k))
cout<<"Error the two elements are the same ";

}

}

THis will do the trik.. I have assigned k=i+1 because if an array elements compares it to itself it will obivously be the same.. What this does is it check wheather an array element is eqaul to any of the other elements in the array excluding itself and the elements it has already checked.. If you have any problems.. PM me i would be glad to help..

3. ## Thanks!

Thanks for the quick Help!!!

67StangMan