Home Tutorialhelp Comment --sum in each column--

 
 

Comment

tin
--sum in each column--,
February 2, 2009 at 9:25 PM

Modify the program so that it computes and prints the sum of each column of the array.

import java.io.* ;
public class arraylabExer4 {
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 sum = 0;
for ( int col=0; col < data.length-1; col++)
{
sum = 0;

for ( int row= 0; row < data.length; row++)
{
sum = sum + data[row][col];
}

System.out.println("The sum of the elements of column "
+ (col + 1) + " = " + sum);
}

}

}

View All Comments | View Tutorial
Advertisement
Related Tutorial and Articles
Advertisements Advertisements
 

 

 

DMCA.com