Results 1 to 2 of 2
- 10-06-2011, 08:16 PM #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];
}
}
return total;
}
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;
}
}
- 10-06-2011, 08:36 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
insert row and column and delete row and column
By daredavil82 in forum New To JavaReplies: 13Last Post: 09-22-2011, 06:10 PM -
I want to know how to calculate and display the sum of each column in 2D array
By Javanoobs in forum New To JavaReplies: 1Last Post: 03-03-2011, 06:43 AM -
How to write column by column to text file
By trkece in forum JDBCReplies: 9Last Post: 02-15-2011, 01:13 AM -
selecting column names dynamically from table column header
By neha_sinha in forum AWT / SwingReplies: 1Last Post: 07-06-2010, 04:50 PM -
Extracting a Row/Column from An Array
By besweeet in forum New To JavaReplies: 10Last Post: 04-18-2010, 07:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks