-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex2-ans.html
More file actions
172 lines (159 loc) · 6.63 KB
/
ex2-ans.html
File metadata and controls
172 lines (159 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Javanotes 9, Solution to Exercise 2, Chapter 7</title>
<link type="text/css" rel="stylesheet" href="../javanotes.css">
</head>
<body>
<div class="page">
<div align="right">
<small>
[ <a href="exercises.html">Exercises</a> |
<a href="index.html">Chapter Index</a> |
<a href="../index.html">Main Index</a> ]
</small>
</div>
<hr>
<div class="content">
<h2>Solution for Programming Exercise 7.2</h2>
<hr class="break">
<p>
<span class="start"><big>T</big>his page contains</span> a sample solution to
one of the exercises from <a href="../index.html">Introduction to Programming Using Java</a>.</p>
<hr>
<h3 class="exercise">Exercise 7.2:</h3>
<p>Suppose that <span class="code">M</span> is a two-dimensional array that
has <span class="code">R</span> rows and <span class="code">C</span> columns. The <span class="newword">transpose</span>
of <span class="code">M</span> is defined to be an array <span class="code">T</span> that has <span class="code">C</span> rows
and <span class="code">R</span> columns such that <span class="code">T[i][j] = M[j][i]</span> for
each <span class="code">i</span> and <span class="code">j</span>. Write a function that takes an array
of type <span class="atype">int[][]</span> as a parameter, and returns the transpose of that array.
(Assume that the parameter is a typical 2D array in which all the rows have the same length.)
Also write a subroutine to print a 2D array of integers in neat rows and columns, and
include a <span class="code">main()</span> routine to test your work.</p>
<hr>
<div class="exercisesubtitle" align="center">
<big><b>Discussion</b></big>
</div>
<hr>
<p>To create the transpose, we need to know how many rows and
how many columns are in the original array. As noted in <a href="../c7/s6.html#arrays.5.1">Subsection 7.6.1</a>,
the number of rows in a 2D array <span class="code">A</span> is given by <span class="code">A.length</span>,
and the number of columns is <span class="code">A[0].length</span>. (<span class="code">A[0].length</span> is
the length of the first row of <span class="code">A</span>, which is the same as the number of
columns in <span class="code">A</span>. Remember that all rows have the same length.) Given that,
we can create a transpose array of the correct size, and—with a little care to get
the indices right—we can copy all the items
from the original array into the transpose. If <span class="code">matrix</span> is the original
array, then the transpose can be created with</p>
<pre>int[][] transpose;
int R = matrix.length; // the number of rows in matrix
int C = matrix[0].length; // the number of columns in matrix
transpose = new int[C][R];
for ( int i = 0; i < C; i++) { // goes through ROWS of the transpose
for ( int j = 0; j < R; j++ ) { // goes through COLUMNS of the transpose
transpose[i][j] = matrix[j][i];
}
}</pre>
<p>One other note of interest is in the code for printing the array. See the
comments on the source code below.</p>
<p>By the way, one purpose of this exercise was to remind you that arrays can be parameters and
they can be the return value of a function. In this case, the parameter type
and the return type for the function are both given by <span class="atype">int[][]</span>.</p>
<hr>
<div class="exercisesubtitle" align="center">
<big><b>The Solution</b></big>
</div>
<hr>
<pre class="exercisecode">
public class TransposeMatrix {
/**
* Creates the transpose of a given 2D array of integers.
* @param matrix the original array
* @return the transpose of matrix
*/
public static int[][] computeTranspose( int[][] matrix ) {
int[][] transpose;
int R = matrix.length; // the number of rows in matrix
int C = matrix[0].length; // the number of columns in matrix
transpose = new int[C][R];
for ( int i = 0; i < C; i++) { // goes through ROWS of the transpose
for ( int j = 0; j < R; j++ ) { // goes through COLUMNS of the transpose
transpose[i][j] = matrix[j][i];
}
}
return transpose;
}
/**
* Prints out the items of a 2D array of ints in rows and columns,
* with 6 spaces in each column.
*/
public static void print( int[][] array ) {
// Note that this uses a for-each loop where the loop
// control variable is of type int[]. This works because
// a 2D array is actually a 1D array of 1D arrays, where
// each 1D array is one of the rows of the 2D array.
for ( int[] row : array ) {
// print out one row from the array
System.out.print(" ");
for ( int item : row ) {
// print with 1 blank space and 5 spaces for the integer;
// if an integer needs more than 5 spaces, the columns will
// be messed up, but all the integers will still be readable.
System.out.printf(" %5d", item);
}
System.out.println();
}
}
/**
* Test the subroutines by creating two arrays and printing them and
* their transposes. The arrays are constructed so that it is easy
* to see that the transposes are correct.
*/
public static void main(String[] args) {
int[][] orig = {
{ 1, 2, 3, 4, 5, 6 },
{ 10, 20, 30, 40, 50, 60 },
{ 100, 200, 300, 400, 500, 600 }
};
System.out.println("Original matrix:");
System.out.println();
print(orig);
System.out.println();
System.out.println("The transpose:");
System.out.println();
print( computeTranspose(orig) );
System.out.println();
System.out.println();
orig = new int[][] {
{1, 1, 1, 1, 1, 1, 1},
{2, 2, 2, 2, 2, 2, 2},
{3, 3, 3, 3, 3, 3, 3},
{4, 4, 4, 4, 4, 4, 4},
{5, 5, 5, 5, 5, 5, 5},
{6, 6, 6, 6, 6, 6, 6},
{7, 7, 7, 7, 7, 7, 7},
};
System.out.println("Original matrix:");
System.out.println();
print(orig);
System.out.println();
System.out.println("The transpose:");
System.out.println();
print( computeTranspose(orig) );
System.out.println();
}
}
</pre>
</div>
<hr>
<div align="right">
<small>
[ <a href="exercises.html">Exercises</a> |
<a href="index.html">Chapter Index</a> |
<a href="../index.html">Main Index</a> ]
</small>
</div>
</div>
</body>
</html>