Thread: Find last largest integer in array?

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    10

    Find last largest integer in array?

    I am having trouble writing code that will print the last index of the last largest integer an a array. I can get it to print the first index of the largest but if I have two of the largest number in the array I want it to print the last index of the largest and not the first.

    I am searching a table but the code really pertains to an array that is within a for loop.

    What I am looking to get is the following. If a certain row in the table is 10 10 10 10 10 I would like it to print the last index 4 and not 0.

    Thanks,
    Cameron

    Code:
    #include "mystuff.h"
    
    int findlastlarge(int** table, int numrows, int numcols)
    
    {
    // local declarations
    int i;
    int j;
    int large;
    
    // print title
    printf("g. The various last indices of the largest int for each and every row is:\n");
    
    // finds the odd values in each row
      for (j = 0; j < numrows; j++)
       {
        int largest = INT_MIN;
    
        for (i = 0; i < numcols; i++)
         {
            if (table[j][i] > largest)
              {
               largest = table[j][i];
               large = i;
              }
          }
       printf("    %d\n", large);
        }
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
    if (table[j][i] > largest)
              {
               largest = table[j][i];
               large = i;
              }
    So if all the numbers in the row were 10 (10 10 10 10 10 as per your example), then the bit of code in red would never get executed after the first run through. You will need to add an extra test in there to allow for numbers of the same size, so that the large variable will be incremented accordingly, even though the largest variable might stay the same.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    10
    I ended up using = and it works perfect.

    if (table[j][i] >= largest)
    {
    largest = table[j][i];
    large = i;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find the last value in an array
    By mkanimozhi in forum C Programming
    Replies: 2
    Last Post: 12-05-2009, 10:06 PM
  2. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM