4 Replies - 9935 Views - Last Post: 23 December 2009 - 10:44 AM Rate Topic: -----

#1 AmateurC  Icon User is offline

  • D.I.C Head

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

Sum of an array row

Posted 22 December 2009 - 12:47 AM

I am trying to write a program that computes the sum the row of each array. Any suggestions?
import java.io.*;

class SumofaRow
{

  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} };
	
	// declare the sum
	int sum = 0;
	
	// compute the sums for each row
	for ( int row=0; row < data.length; row++)
	{
	  
	  for ( int col=0; col < data[row].length; col++) 
	  {
		sum = sum + data[col][];
	  }
	  
	  // write the sum for this row
	  System.out.println(sum);
	}
	  

  }
}	  



Is This A Good Question/Topic? 0
  • +

Replies To: Sum of an array row

#2 Riveted  Icon User is offline

  • D.I.C Head

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

Re: Sum of an array row

Posted 22 December 2009 - 01:59 AM

You're missing a row number when accessing data, and you also have col in the wrong spot. Since you are basing your inner loop off of data[row], you need to have your call look like data[row][col].
Was This Post Helpful? 1
  • +
  • -

#3 AmateurC  Icon User is offline

  • D.I.C Head

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

Re: Sum of an array row

Posted 23 December 2009 - 04:15 AM

I am still stumped. Do you mind posting a few code examples to illustrate your point?
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

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

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Sum of an array row

Posted 23 December 2009 - 06:53 AM

sum = sum + data[row][col];



What do you think sum = sum + data[col][]; means?

How is the computer supposed to access an unidentified index?
Was This Post Helpful? 1
  • +
  • -

#5 waltf  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 10
  • Joined: 23-December 09

Re: Sum of an array row

Posted 23 December 2009 - 10:44 AM

You are also not resetting your sum variable to 0 after summing / printing the sum of each row,
your code:
	// declare the sum
	int sum = 0;
   
	// compute the sums for each row
	for ( int row=0; row < data.length; row++)
	{
	 
	  for ( int col=0; col < data[row].length; col++)
	  {
		sum = sum + data[col][];
	  }
	 
	  // write the sum for this row
	  System.out.println(sum);
	}


should be:
   // declare the sum
   int sum = 0;
   
   // compute the sums for each row
   for ( int row=0; row < data.length; row++)
   {
	  // reset the sum for the this row
	  sum = 0;

	  for ( int col=0; col < data[row].length; col++)
	  {
		 sum = sum + data[row][col];
	  }
	 
	  // write the sum for this row
	  System.out.println(sum);
   }



Also you could change your print statement to give a little more infO;

System.out.println("The sum for row "+row+" is "+sum);

:)
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1