-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCHAPTER 49 C
More file actions
243 lines (211 loc) · 6.95 KB
/
CHAPTER 49 C
File metadata and controls
243 lines (211 loc) · 6.95 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import java.util.*;
import javax.sound.midi.MidiMessage;
import java.io.*;
/**
Chapter 49C - Two-Dimensional Arrays Exercises 1-5
@author TODO Your Name
@version TODO Date
@author Period - TODO Your Period
@author Assignment - Ch49C_2DArrays
@author Sources - TODO list collaborators
*/
public class Ch49C_2DArrays_Ex1to5
{
Scanner scan;
public Ch49C_2DArrays_Ex1to5()
{
scan = new Scanner( System.in );
}
/**
TODO Description
*/
public int sumOfAllArrayElements( int[][] data ) //Question #1
{
// declare the sum
int sum = 0;
// compute the sum
for ( int row = 0; row < data.length; row++ )
{
for ( int col = 0; col < data[row].length; col++ )
{
sum=sum+data[row][col];
}
}
// write out the sum
System.out.println(sum);
return sum;
}
/**
TODO Description
*/
public int[] sumOfEachRow( int[][] data ) //Question #2
{
// declare the row sum array
int [] rowSums = new int[data.length];
// compute the sums for each row
for ( int row = 0; row < data.length; row++ )
{
// initialize the sum
int sum = 0;
// compute the sum for this row
for ( int col = 0; col < data[row].length; col++ )
{
sum = sum + data[row][col];
}
rowSums[row] = sum;
// write the sum for this row
System.out.println(sum);
}
return rowSums;
}
/**
* TODO Description
*/
public int[] sumOfEachColumn( int[][] data ) //Question #3
{
//find the longest row
int longestRow = data.length;
// TODO Your Code Here
// declare the row sum array
int [] colSums = new int[longestRow];
for (int row = 0; row < data.length; row++)
{
for (int col = 0; col < data[row].length; col++)
{
}
}
return colSums;
}
/**
TODO Description
*/
public int[] maxAndMinElements( int[][] data ) //Question #4
{
// declare the max and the min
int max = data[0][0];
int min = data[0][0];
// compute the sum
for ( int row = 0; row < data.length; row++ )
{
for ( int col = 0; col < data[row].length; col++ )
{
if (max < data[row][col]){
max = data[row][col];
}
if (min > data[row][col]){
min = data[row][col];
}
}
}
// write out the results
System.out.println(max);
System.out.println(min);
int[] result = { max, min };
return result;
}
/**
TODO Description
*/
public int[] largestElements( int[][] data ) //Question #5
{
// declare the largest in row array
int[] largestInRow = new int[data.length];
// TODO Your Code Here
int i = 0;
int no_of_rows = data.length;
// Initialize max to 0 at beginning
// of finding max element of each row
int max = 0;
while (i < no_of_rows) {
for (int j = 0; j < data[i].length; j++) {
if (data[i][j] > max) {
max = data[i][j];
}
}
largestInRow[i] = max;
max =0;
i++;
}
for (int j = 0; j < largestInRow.length; j++)
{
System.out.println(largestInRow[j]);
}
return largestInRow;
}
/**
Testing method: instantiates a Lesson49C Exercises object and
then invokes each method
@param args command line parameters (not used)
*/
public static void main( String[] args )
{
Scanner kbd = new Scanner( System.in );
boolean done = false;
Ch49C_2DArrays_Ex1to5 exercise = new Ch49C_2DArrays_Ex1to5();
do
{
System.out.println();
System.out.println();
System.out.println( "Make a selection" );
System.out.println();
System.out.println( " (1) Sum of All Array Elements");
System.out.println( " (2) Sum of Each Row");
System.out.println( " (3) Sum of Each Column");
System.out.println( " (4) Maximum and Minimum Elements");
System.out.println( " (5) Largest Elements");
System.out.println( " (Q) Quit" );
System.out.println();
System.out.print( "Enter a choice: " );
String response = kbd.nextLine();
if ( response.length() > 0 )
{
System.out.println();
switch ( response.charAt( 0 ) )
{
case '1':
int[][] dataEx1 = { { 3, 2, 5 },
{ 1, 4, 4, 8, 13 },
{ 9, 1, 0, 2 },
{ 0, 2, 6, 3, -1, -8 } };
int sum = exercise.sumOfAllArrayElements(dataEx1);
break;
case '2':
int[][] dataEx2 = { { 3, 2, 5 },
{ 1, 4, 4, 8, 13 },
{ 9, 1, 0, 2 },
{ 0, 2, 6, 3, -1, -8 } };
int[] rowSums = exercise.sumOfEachRow(dataEx2);
break;
case '3':
int[][] dataEx3 = { { 3, 2, 5 },
{ 1, 4, 4, 8, 13 },
{ 9, 1, 0, 2 },
{ 0, 2, 6, 3, -1, -8 } };
int[] colSums = exercise.sumOfEachColumn( dataEx3 );
break;
case '4':
int[][] dataEx4 = { { 3, 2, 5 },
{ 1, 4, 4, 8, 13 },
{ 9, 1, 0, 2 },
{ 0, 2, 6, 3, -1, -8 } };
int[] maxMin = exercise.maxAndMinElements( dataEx4 );
break;
case '5':
int[][] dataEx5 = { { 3, 2, 5 },
{ 1, 4, 4, 8, 13 },
{ 9, 1, 0, 2 },
{ 0, 2, 6, 3, -1, -8 } };
int[] largestInRow = exercise.largestElements( dataEx5 );
break;
default:
if ( response.toLowerCase().charAt( 0 ) == 'q' )
done = true;
else
System.out.print( "Invalid Choice" );
break;
}
}
} while ( !done );
System.out.println( "Goodbye!" );
}
}