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

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    6

    Unhappy 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. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    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. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    6

    Smile Thanks!

    Thanks for the quick Help!!!

    67StangMan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a fast way to check if a point is within a line.
    By mike_g in forum Game Programming
    Replies: 5
    Last Post: 08-05-2008, 12:24 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. can i dynamicaly check the size of an array?
    By matheo917 in forum C++ Programming
    Replies: 2
    Last Post: 11-08-2001, 09:12 AM