Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default find the greatest and lowest number in 2D array

    Hi,

    I'm trying to make a code that calculates and shows the greatest and lowest integer in a 2D array. Here' s what I have so far. I don't know how to continue.
    Thanks in advance,
    le_albina




    public class GreatestLowestRange{
    public static void main(String [] args){

    int greatest, lowest, range;
    int [] [] table = {{10, 12, 14, 16, 18},
    {20, 22, 24, 26, 28},
    {30, 32, 34, 36, 38},
    {40, 42, 44, 46, 48}};
    greatest= Greatest(table);











    }//End main
    //************************************************** *******
    private static int Greatest(int tarray[][]){

    int i, j, greatest;
    greatest=tarray[0][0];
    //loop for rows
    for (i=0; i<tarray; i++)
    {
    //loop for columns
    for (j=0; j<tarray[i]; j++)
    {
    if(tarray[i][j]>greatest)
    greatest=tarray[i][j];

    }//end 1rst for
    }//en 2nd for

    }//end Greatest


    }//end GreatestLowestRange

  2. #2
    softslate is offline Member
    Join Date
    Mar 2009
    Location
    Schenectady, NY
    Posts
    11
    Rep Power
    0

    Default

    Looks good but in your for statements you need to compare against the *length* of the array, not the array itself:
    Java Code:
    for (i=0; i<tarray.length; i++)
    and
    Java Code:
    for (j=0; j<tarray[i].length; j++)
    I hope that helps!
    SoftSlate Commerce Java Shopping Cart
    www.softslate.com

  3. #3
    softslate is offline Member
    Join Date
    Mar 2009
    Location
    Schenectady, NY
    Posts
    11
    Rep Power
    0

    Default

    Another quick suggestion: As it is you'll have to create a separate method to find the lowest value. But that method will have to go through the same looping as the "greatest" method you have now. You might consider finding both values at the same time in the same method, and have that method return a Map of the two values rather than just one value.
    SoftSlate Commerce Java Shopping Cart
    www.softslate.com

Similar Threads

  1. Find a number from a string
    By florentp in forum New To Java
    Replies: 2
    Last Post: 03-20-2009, 09:01 PM
  2. Find all permutations of a number
    By matzahboy in forum New To Java
    Replies: 6
    Last Post: 12-02-2008, 03:59 AM
  3. Replies: 21
    Last Post: 11-05-2008, 02:31 PM
  4. Replies: 8
    Last Post: 04-22-2008, 03:21 AM
  5. Find nth root of a number
    By perito in forum New To Java
    Replies: 1
    Last Post: 03-03-2008, 06:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •