Results 1 to 3 of 3
Thread: two dimensional arrays
 11052010, 11:23 PM #1Member
 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.
 11052010, 11:43 PM #3Member
 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: 10062010, 05:27 AM 
How MultiDimensional arrays are represented in memory
By Riaz Ali in forum New To JavaReplies: 4Last Post: 08012010, 09:25 AM 
dynamic two dimensional arrays?
By dinosoep in forum New To JavaReplies: 4Last Post: 12052009, 06:12 PM 
Multidimensional arrays
By Implode in forum New To JavaReplies: 1Last Post: 09152009, 08:50 AM 
[SOLVED] Multidimensional arrays
By thelinuxguy in forum Advanced JavaReplies: 3Last Post: 05072009, 02:52 PM
Bookmarks