Thread: Finding greatest element of an array

  1. #1
    Unregistered
    Guest

    Question Finding greatest element of an array

    I have a 3x3 array of int's and I am working on an algorithm that will find the highest-valued element of the array and end up by assigning the relevant row and column to "rowChoice" and "colChoice", respectively. If there are two or more elements with the same value, I want my algorithm to choose randomly between them.

    If I know the values are all different, I can just do something like this:

    PHP Code:
    // This is just a C code "fragment"...

    // I'm assuming myArray[][] is an existing 3x3 array

    int row=0col=0greatestSoFar=0rowChoice=0colChoice=0;

    for(
    row=0row<=2row++)
    {
         for(
    col=0col<=2col++)
         {
              if(
    myArray[row][col]>greatestSoFar)
              {
                   
    greatestSoFar=myArray[row][col];
                   
    rowChoice=row;
                   
    colChoice=col;
              }
         }
    }

    // Program continues... 
    Where I need help is the "choose randomly if two elements are the same" part. Any suggestions?

    Thanks in advance for your help...

    Andrew
    (I'll be registering as soon as I get around to it... )

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Hmm... randomly? That is actually kinda complicated; it involves adding the row and column to a list every time you encounter a match. Actually, if you don't mind a biased distribution, it can be done in a somewhat elegant way...
    Code:
    // This is just a C code "fragment"...
    
    // I'm assuming myArray[][] is an existing 3x3 array
    
    int row=0, col=0, greatestSoFar=0, rowChoice=0, colChoice=0;
    
    for(row=0; row<=2; row++)
    {
         for(col=0; col<=2; col++)
         {
              // This is the trick... if while going through the list, you find
              //  a value that's == greatestSoFar, you flip a coin 
              //  (rand () % 1) to see whether or not to keep your 
              //  old value.
              if (myArray[row][col] == greatestSoFar && rand() % 1)
              {
                   greatestSoFar=myArray[row][col];
                   rowChoice=row;
                   colChoice=col;
              }
    
              if(myArray[row][col] > greatestSoFar)
              {
                   greatestSoFar=myArray[row][col];
                   rowChoice=row;
                   colChoice=col;
              }
         }
    }
    rand() is in stdlib.h, and if you use it, you have to srand(time()) at the beginning of your program.

    Also, this is not a uniform distribution... if you have two values that are both the greatest value, then there's an even chance... but if you have 3 values, the last value has a 50% chance, and the first and second both have 25% chances.
    It's random, but not fair.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Unregistered
    Guest

    Lightbulb Solved my own problem! (well, sort of...)

    A little more thought led to this solution, although it's not terribly effecient, so I would still appreciate any better ideas...

    (I have not tested it yet, either, so there may be a bug or two)

    PHP Code:
    // This is just a C code "fragment"...

    int row=0col=0greatestValue=0pickerFlag=0random=0rowChoice=0colChoice=0;
    int picker[3][3]={{0,0,0},{0,0,0},{0,0,0}};

    srand(time(NULL));

    for(
    row=0row<=2row++)
    {
         for(
    col=0col<=2col++)
         {
              
    picker[row][col]=myArray[row][col];
         }
    }

    do
    {
         
    pickerFlag=0;

         for(
    row=0row<=2row++)
         {
              for(
    col=0col<=2col++)
              {
                   if(
    picker[row][col]>greatestValue)
                   {
                        
    greatestValue=picker[row][col];
                        
    rowChoice=row;
                        
    colChoice=col;
                   }
                   else if(
    myArray[row][col]=greatestValue
                   
    {
                        
    pickerFlag=1;
                   }       
              }
         }

         if(
    pickerFlag==1)
         {
              for(
    row=0row<=2row++)
              {
                   for(
    col=0col <=2col++)
                   {
                        if(
    picker[row][col]==greatestValue)
                        {
                             
    random=rand();
                             
    picker[row][col]=random;
                        }
                        else
                        {
                             
    picker[row][col]=0;
                        }
                   }
              }
         }
    } while(
    pickerFlag==1);

    // Program continues... 
    Can you see what I am doing here, and, more importantly, help me improve it? Completely different ideas would be good too. Meanwhile, I'll continue building my app using the code above.

    Andrew

  4. #4
    Unregistered
    Guest

    Arrow OOPS!

    Sorry, where I have

    "else if(myArray[row][col]=greatestValue"

    it should be

    "else if(picker[row][col]==greatestValue)"

    Andrew

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    2
    Just bumping my Question back to the top.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-05-2008, 11:30 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 07:23 PM
  4. Deleting an element from an array
    By Matt13 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 04:42 AM
  5. Cannot delete last element in array
    By Simon in forum C Programming
    Replies: 10
    Last Post: 09-12-2002, 08:29 PM