# Largest elements of a row in a 2D array

Page 1 of 1

## 7 Replies - 22551 Views - Last Post: 24 December 2009 - 11:24 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=146764&amp;s=c5f71f3c27c9607c28437259b80719ba&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 AmateurC

Reputation: -5
• Posts: 71
• Joined: 12-June 09

# Largest elements of a row in a 2D array

Posted 23 December 2009 - 04:43 AM

I am trying to write a program that finds the largest number in each row in a 2D array. For example, in {{1,5,68, 79}, {7,0,2}} the largest number in the first row would be 79 and the second row 7. But my program doesn't do what it is supposed to do. What should I do with my code? Please offer some code examples to illustrate your suggestions.
import java.io.*;

class ArrayRowMax
{

public static void main ( String[] args ) throws IOException
{
int[][] data = {{3, 2, 5},
{1, 4, 4, 8, 13},
{9, 1, 0, 2},
{0, 2, 6, 3, -1, -8} };

int max = data[0][0];

for ( int row=0; row < data.length; row++)
{
if ( data[row][row] > max )
max = data[row][row];

System.out.println("Maximum in row[" + row + "]: " + max);
}

}
}

This post has been edited by AmateurC: 23 December 2009 - 04:46 AM

Is This A Good Question/Topic? 0

## Replies To: Largest elements of a row in a 2D array

### #2 Riveted

Reputation: 14
• Posts: 87
• Joined: 03-December 09

## Re: Largest elements of a row in a 2D array

Posted 23 December 2009 - 05:43 AM

Alright, I don't get it. Are you giving us every problem in your book? This is nearly identical to http://www.dreaminco...topic146620.htm.

### #3 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Largest elements of a row in a 2D array

Posted 23 December 2009 - 06:50 AM

You need a second for loop which will actually do the iteration through the row [each column index in the row].

I would initialize max to zero, there's no reason to set it to [0][0]. Then it's a simple linear iteration and comparison to find the max for each row.

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Largest elements of a row in a 2D array

Posted 23 December 2009 - 12:20 PM

You don't loop for the column

for ( int row=0; row < data.length; row++)
{
int max = data[row][0];
for(col = 1; col < data[row].length; col++) {
if ( data[row][col] > max )
max = data[row][col];
}
System.out.println("Maximum in row[" + row + "]: " + max);
}

### #5 AmateurC

Reputation: -5
• Posts: 71
• Joined: 12-June 09

## Re: Largest elements of a row in a 2D array

Posted 23 December 2009 - 07:41 PM

I don't like to keep asking for help, but sometimes when I couldn't figure out a problem, even though I spend hours trying to solve it, I ask for help because ,for one thing, I am still new to Java.

This post has been edited by AmateurC: 23 December 2009 - 07:53 PM

### #6 AmateurC

Reputation: -5
• Posts: 71
• Joined: 12-June 09

## Re: Largest elements of a row in a 2D array

Posted 24 December 2009 - 04:28 AM

pbl, on 23 Dec, 2009 - 11:20 AM, said:

You don't loop for the column

for ( int row=0; row < data.length; row++)
{
int max = data[row][0];
for(col = 1; col < data[row].length; col++) {
if ( data[row][col] > max )
max = data[row][col];
}
System.out.println("Maximum in row[" + row + "]: " + max);
}

You forgot to put an "int" in the col.

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Largest elements of a row in a 2D array

Posted 24 December 2009 - 04:39 AM

Sorry, I am still new to Java

### #8 AmateurC

Reputation: -5
• Posts: 71
• Joined: 12-June 09

## Re: Largest elements of a row in a 2D array

Posted 24 December 2009 - 11:24 PM

That's OK. No harm done.