Results 1 to 5 of 5
- 12-09-2008, 10:31 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 2
- Rep Power
- 0
- 12-09-2008, 11:16 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
An array of sums and two nested loops.
Give it a try, and then post the problematic code and give a complete description of your actual problem.
- 12-09-2008, 12:25 PM #3
Member
- Join Date
- Dec 2008
- Posts
- 2
- Rep Power
- 0
this are the code im working....
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int[][] matrix = new int[4][3];
int row, col, value = 1;
final int NUM_ROWS = matrix.length;
final int NUM_COLS = matrix[0].length;
for (row = 0; row < NUM_ROWS; row++) {
for (col = 0; col < NUM_COLS; col++) {
matrix[row][col] = value;
value++;
}
}
for (row = 0; row < NUM_ROWS; row++) {
for (col = 0; col < NUM_COLS; col++) {
System.out.print(matrix[row][col] + "\t");
}
System.out.println();
}
}
}
I need help with my code that will find the sum of the 3 rows which equal 6, 15, 24, 33 then add the columns that equal 22, 26 , 30.
here is my code so far:
- 12-09-2008, 12:32 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Well, like I said, create a single dimensional array for the sums (that will correlate, of course, to the second dimension in your 2 dimensional array). Then, using nested loops, sum the values from the second dimension columns using the same index into the sum array.
At least try it.
- 12-09-2008, 04:30 PM #5
Member
- Join Date
- Dec 2008
- Location
- Enschede, Overijssel, the Netherlands
- Posts
- 19
- Rep Power
- 0
Please use code tags for formatting.Java Code:public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { int[][] matrix = new int[4][3]; int row, col, value = 1; final int NUM_ROWS = matrix.length; final int NUM_COLS = matrix[0].length; for (row = 0; row < NUM_ROWS; row++) { for (col = 0; col < NUM_COLS; col++) { matrix[row][col] = value; value++; } } for (row = 0; row < NUM_ROWS; row++) { for (col = 0; col < NUM_COLS; col++) { System.out.print(matrix[row][col] + "\t"); } System.out.println(); } } }


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks