7 Replies - 5874 Views - Last Post: 24 December 2009 - 11:24 PM Rate Topic: -----

#1 AmateurC  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • 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.
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8327
  • View blog
  • Posts: 31,857
  • 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);
	}


Was This Post Helpful? 1
  • +
  • -

#5 AmateurC  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 AmateurC  Icon User is offline

  • D.I.C Head

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

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

Posted 24 December 2009 - 04:28 AM

View Postpbl, 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.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8327
  • View blog
  • Posts: 31,857
  • 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 :D
Was This Post Helpful? 0
  • +
  • -

#8 AmateurC  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • 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. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1