Find last largest integer in array?

This is a discussion on Find last largest integer in array? within the C Programming forums, part of the General Programming Boards category; I am having trouble writing code that will print the last index of the last largest integer an a 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,528
    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, 06:44 PM
  3. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  4. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 10:42 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21