I am trying to get the highest value in my rows. I have been at this awhile trying to figure it out. I assume it is suppose to return 3 values but honestly i really suck at this. Here is what I have

    public static int getHighestInRow(int[][] numbers, int row)
   {
    int highestRow = numbers[0][0];
    for (int col = 0; col < numbers[row].length; col++)
    {
         if (numbers[row][col] >= highestRow)
               highestRow = numbers[row][col];
        }

    return highestRow;
   }

If you need to see the rest of my code let me know i will gladly show it.

Recommended Answers

All 6 Replies

Hi lauren.vitiello.12, I'm not entirely sure if this will help but try changing

col < numbers[row].length

to

col < row

Or something like that. Does your code even compile like that? I ask because you are only using part of the numbers array, which doesn't make sense to me but Java isn't my strong point so there's that!

If my suggestion doesn't help, maybe post the rest of your code so that someone can see where it's gone bad.

Good luck with you assignment and thanks for posting some code to show that you are working on it and not looking for someone to just do it for you, it's nice to see!

Yes it compies. here is the rest of my code. I will try your suggestion as well.

import java.util.Scanner;


public class TwoDArray
{

    public static void main(String[] args)
    {
    int numbers[][] = {{14, 35, 46, 99},
                       {16, 1, 94, 23},
                       {57, 50, 83, 79} };
    double arrayAverage = getAverage(numbers);
    int row = 0;
    int col = 0;

    int minValue = getLowest(numbers);
    int maxValue = getHighest(numbers);
    int highRow = getHighestInRow(numbers, row);

    System.out.println("The average of the array is: " + arrayAverage);
    System.out.println("The lowest number in the array is: " + minValue);
    System.out.println("The highest number in the array is: "+ maxValue);
    System.out.print("Highest row: " + highRow);

    }

   public static int getAverage(int numbers[][])
   {

    int total = 0;


    for (int row = 0; row <numbers.length; row++)
    {
        for (int col = 0; col < numbers[row].length; col++)
            total+= numbers[row][col];

    }


        return total/numbers.length;
}
    public static int getLowest(int numbers[][])
    {
        int min = numbers[0][0];
        for (int col = 0; col < numbers.length; col++)
        {
            for (int row = 0; row < numbers[col].length; row++)
            {
                if (min > numbers[col][row])
                {
                    min = numbers[col][row];
                }
            }
        }
        return min;
    }
    public static int getHighest(int[][] numbers)
    {
        int max = numbers[0][0];
        for (int col = 0; col < numbers.length; col++)
        {
            for (int row = 0; row < numbers[col].length; row++)
            {
                if (max < numbers[col][row])
                {
                    max = numbers[col][row];
                }
            }
        }
        return max;

    }

    public static int getHighestInRow(int[][] numbers, int row)
   {
    int highestRow = numbers[0][0];
    for (int col = 0; col < numbers[row].length; col++)
    {
         if (numbers[row][col] >= highestRow)
               highestRow = numbers[row][col];
        }

    return highestRow;
   }

}

the output comes out as
The average of the array is: 199.0
The lowest number in the array is: 1
The highest number in the array is: 99
Highest row: 99

What's the highest row output supposed to be?

the books says this; getHighestinRow. this method should accept a two-dimensional array as its first argument and an integer as its second argument. the second argu,ent should be the subscript of a row in the array. the method should return the highest value in the specified row in the array .. so i only need one value but to be honest i dont really know how i did it.... just used examples from the book. im not sure how i got it or what not.

In that case, there's nothing wrong with the method except line 77: int highestrow = numbers[0][0] which should be int highestrow = numbers[row][0] to prevent the possibility of error if numbers[0][0] is higher than any number in all other rows.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.