# Thread: Trying to sum a column of a 2d array....

1. Member
Join Date
Oct 2011
Posts
1
Rep Power
0

## Trying to sum a column of a 2d array....

if you guys can help that would be great. thanks
what i need help with is bolded.

public class projectTwo
{

public static void main (String [] args)
{
int [][] testData = { { 100, 99, 98, 97},
{ 96, 95, 94, 93 } };

int row=0;
int col=0;

int total= getTotal(testData);
float average= getAverage(testData);
int rowTotal= getRowTotal(testData);
int columnTotal= getColumnTotal(testData);
int highestInRow= getHighestInRow(testData);
int lowestInRow= getLowestInRow(testData);

System.out.println("Test scores total:" + total + " " + "Average score:" + average + " " + "Row total:" +
rowTotal +" " + "Column total:" + columnTotal +" " + "Highest:" + highestInRow +" " + "Lowest:" + lowestInRow);

}

public static int getTotal(int [][] testData)
{
int total=0;
for (int row=0; row<testData.length; row++)
{

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

public static float getAverage(int [][] testData)
{float total = 0;

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

return (total/8);
}

public static int getRowTotal (int[][] testData)
{ int rowTotal;
return rowTotal;

}

public static int getColumnTotal (int [][] testData)
{int columnTotal;
return columnTotal;}

public static int getHighestInRow (int [][] testData)
{int highestInRow = testData[0][0]; for (int row = 0; row < testData.length; row++)
{ for (int col = 0; col < testData[row].length; col++)
{
if (testData[row][col] >= highestInRow)
highestInRow = testData[row][col]; }
}
return highestInRow;

}

public static int getLowestInRow (int [][] testData)
{ int lowestInRow = testData[0][0];
for (int row = 0; row < testData.length; row++)
{ for (int col = 0; col < testData[row].length; col++)
{ if (testData[row][col] <= lowestInRow)
{
lowestInRow = testData[row][col]; }
}
}
return lowestInRow;
}
}

2. ## Re: Trying to sum a column of a 2d array....

What are the row total(s) and colum total(s) supposed to be when you calculate them by hand?

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•