Results 1 to 3 of 3
Thread: two dimensional arrays
- 11-06-2010, 12:23 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
two dimensional arrays
I have created a two dimensional array that gets the number of rows, columns, number to start with, and how much to count by :
int [][] matrix = new int[r][c];
for (int i = 0; i<r; i++) {
for (int j = 0; j<c; j++){
matrix [i][j] = starting;
starting= starting + count;
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
Which prints out:
Original matrix:
2 6 10 14 18
22 26 30 34 38
42 46 50 54 58
How do I change this so the rows are printed as columns and the columns are printed as rows? So it prints:
Transposition matrix:
2 22 42
6 26 46
10 30 50
14 34 54
18 38 58
-
Don't print out the results in the same nested for loops where you fill your array. Instead fill the array first, then use a new set of nested for loops to print out your array values, but in this second set of for loops, you'll reverse the order of the loops -- the outer loop will loop on the columns and the inner loop on the rows.
- 11-06-2010, 12:43 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
Perfect! Thanks a bunch!
int [][] matrix = new int[r][c];
for (int i = 0; i<r; i++) {
for (int j = 0; j<c; j++){
matrix [i][j] = starting;
starting= starting + count;
}
}
for (int i = 0; i<r; i++) {
for (int j = 0; j<c; j++){
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
System.out.println("");
System.out.println("Transposition matrix:");
for (int j = 0; j<c; j++){
for (int i = 0; i<r; i++){
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
Similar Threads
-
Printing Two Dimensional Arrays with for loops
By mcnam4119 in forum JCreatorReplies: 3Last Post: 10-06-2010, 06:27 AM -
How Multi-Dimensional arrays are represented in memory
By Riaz Ali in forum New To JavaReplies: 4Last Post: 08-01-2010, 10:25 AM -
dynamic two dimensional arrays?
By dinosoep in forum New To JavaReplies: 4Last Post: 12-05-2009, 07:12 PM -
Multi-dimensional arrays
By Implode in forum New To JavaReplies: 1Last Post: 09-15-2009, 09:50 AM -
[SOLVED] Multi-dimensional arrays
By thelinuxguy in forum Advanced JavaReplies: 3Last Post: 05-07-2009, 03:52 PM
Bookmarks