3 Replies - 19651 Views - Last Post: 04 March 2010 - 12:37 PM Rate Topic: -----

#1 tawrlr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-February 10

add elements in a 2d array java

Posted 28 February 2010 - 10:11 AM

Hi I have created java program to populate a 2d array, using input from the keyboard
I am good with that, now I want to add the inputted data, per row, col and dia... based on the size of the array, which was also input by the user, how would i go about that.

this is a snippet of my code

//Now ask user to enter the data
         for (int row=0; row<square.length; row++)//Outer loop - Fills the rows 
	     {
	       for (int col=0; col<square[row].length; col++)//Inner loop Fills the columns
	        {
	         System.out.println ("   Please Enter the data for row and column location: "+
	         "[" + row + "]"+ " , [" + col + "]: ");
	         System.out.print ("   ");
	         int value = keyboard.nextInt();
	         //System.out.print ("  ");
	         square [row][col] = value;
	        }


I think that I need to say something like

sumrow1 = [0][0] + [0][1] + [0][2]

but with this program i do not know how many rows, because it is input by the enduser, so i can not hard code the addition... any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: add elements in a 2d array java

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5884
  • View blog
  • Posts: 12,768
  • Joined: 16-October 07

Re: add elements in a 2d array java

Posted 28 February 2010 - 10:17 AM

You're very close. I'd say, just think about it. But I'll assume you already have.

Here's a solution. Hope it helps.

for (int row=0; row<square.length; row++) {
	int sumrow = 0; // the sum of your row starts at 0
	for (int col=0; col<square[row].length; col++) {
		sumrow += square [row][col]; // add each column
	}
	System.out.println("the sum of row " + row + " = " + sumrow);
}


Was This Post Helpful? 0
  • +
  • -

#3 tawrlr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-February 10

Re: add elements in a 2d array java

Posted 04 March 2010 - 11:24 AM

Hi Baavgai,

Thank you for the response, I did think about it and came up with

int downDia = 0; // Initializes the down diagonal sum

for (int countrow=0; countrow<square.length; countrow++)//Outer loop - steps through the rows
{
  int rowSum = 0;  // Initialize row sum
  int colSum = 0;// Initializes the col sum
  int sumDia = 0; // Initializes the down diagonal sum
  int upDia = 0; // Initializes the up diagonal sum
  for (int countcol=0; countcol<square.length; countcol++)//Inner loop - counts and adds the value for columns
  {

      rowSum = rowSum + square[countrow][countcol];
      colSum = colSum + square[countcol][countrow];

   }


   System.out.println ("   The sum for the row: " + countrow + "  is  " + rowSum );
   System.out.println ("   The sum for the col: " + countrow + "  is  " + colSum );






but now i am stuck on the diangonls... i need to sum top left dia down, and top right down... any insight?
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5884
  • View blog
  • Posts: 12,768
  • Joined: 16-October 07

Re: add elements in a 2d array java

Posted 04 March 2010 - 12:37 PM

View Posttawrlr, on 04 March 2010 - 12:24 PM, said:

but now i am stuck on the diangonls... i need to sum top left dia down, and top right down... any insight?


Start at row=0 and col=0. Then, row++, col++, until you hit a wall.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1