Skip to content

Commit 162e5c7

Browse files
committed
Rename twoDArrayPrograms to RowsCols and fix style
1 parent c930c96 commit 162e5c7

File tree

3 files changed

+144
-100
lines changed

3 files changed

+144
-100
lines changed

src/com/codefortomorrow/intermediate/chapter10/practice/twoDArrayPrograms.java renamed to src/com/codefortomorrow/intermediate/chapter10/practice/RowsCols.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.codefortomorrow.intermediate.chapter10.practice;
2-
2+
// TODO break up into multiple problems
33
/*
44
Fill a 4 X 6 integer array with 24 random values from 7 to 77.
55
Print the values in 4 rows of 6 elements.
@@ -24,13 +24,22 @@
2424
Print the values in 5 rows of 4 elements.
2525
Use nested loops to fill the values into the array,
2626
rather than just declaring the values initially.
27-
*hint*: You can set the values of the first row.
28-
(You will then need a loop to print the first row
29-
separately from the rest of the array.)
3027
*/
3128

32-
public class twoDArrayPrograms {
29+
public class RowsCols {
3330
public static void main(String[] args) {
3431
// write code here
3532
}
33+
34+
/**
35+
* Returns a random integer
36+
* in the range [min, max]
37+
* @param min minimum random integer
38+
* @param max maximum random integer
39+
* @return a random integer in the range
40+
* [min, max]
41+
*/
42+
public static int randInt(int min, int max) {
43+
return min + (int) (Math.random() * ((max - min) + 1));
44+
}
3645
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.codefortomorrow.intermediate.chapter10.solutions;
2+
3+
/*
4+
Fill a 4 X 6 integer array with 24 random values from 7 to 77.
5+
Print the values in 4 rows of 6 elements.
6+
Using the same array, keep track of the sum of all the values in the array.
7+
Display the sum on its own line under the values.
8+
Keep track of the maximum and minimum value in the array.
9+
Display the max and min on the line below the sum.
10+
11+
Fill a 3 X 3 string array so that the values in each row are “*”, “**”, “***”.
12+
Print the values in 3 rows of 3 elements, each separated by a tab.
13+
Fill the array in a loop, with a separate variable that will
14+
keep track of the number of asterisks in each term.
15+
16+
Fill a 5 X 4 integer array according to the following pattern:
17+
18+
4 9 16 25
19+
8 27 64 125
20+
16 81 256 625
21+
32 243 1024 3125
22+
64 729 4096 15625
23+
24+
Print the values in 5 rows of 4 elements.
25+
Use nested loops to fill the values into the array,
26+
rather than just declaring the values initially.
27+
*hint*: You can set the values of the first row.
28+
(You will then need a loop to print the first row
29+
separately from the rest of the array.)
30+
*/
31+
32+
// TODO explain min and max value
33+
34+
public class RowsCols {
35+
public static void main(String[] args) {
36+
int[][] a = new int[4][6]; // create 4x6 array
37+
int sum = 0; // sum of all elements in array
38+
int max = Integer.MIN_VALUE; // max element in array
39+
int min = Integer.MAX_VALUE; // min element in array
40+
41+
// iterate through each element in a
42+
for (int row = 0; row < 4; row++) {
43+
for (int col = 0; col < 6; col++) {
44+
// generate a random number from 7 to 77
45+
// and store it in the array
46+
a[row][col] = randInt(7, 77);
47+
48+
// print that element
49+
System.out.print(a[row][col] + "\t");
50+
51+
// keep a running total of all elements
52+
sum += a[row][col];
53+
54+
// update min or max as needed
55+
if (a[row][col] < min) {
56+
min = a[row][col];
57+
}
58+
59+
if (a[row][col] > max) {
60+
max = a[row][col];
61+
}
62+
}
63+
System.out.println(); // move to next row
64+
}
65+
66+
// display the sum of all elements
67+
// and the max and min element
68+
System.out.println("Sum: " + sum);
69+
System.out.println("Max: " + max);
70+
System.out.println("Min: " + min);
71+
72+
// add some space between arrays
73+
System.out.println();
74+
System.out.println();
75+
76+
// create a 3x3 array
77+
String[][] b = new String[3][3];
78+
String asterisks = "*";
79+
int numberOfAsterisks = 0;
80+
81+
// iterate through the 3x3 array
82+
for (int row = 0; row < 3; row++) {
83+
for (int col = 0; col < 3; col++) {
84+
// store the asterisk(s) in the array
85+
b[row][col] = asterisks;
86+
87+
// print the current element
88+
System.out.print(b[row][col] + "\t");
89+
90+
// update total number of asterisks in array
91+
numberOfAsterisks += row + 1;
92+
}
93+
System.out.println(); // move to next row
94+
asterisks += "*"; // each row has 1 more asterisk than the last one
95+
}
96+
97+
// print the total number of asterisks in the array
98+
System.out.println("Number of asterisks: " + numberOfAsterisks);
99+
100+
// add some space between arrays
101+
System.out.println();
102+
System.out.println();
103+
104+
// create a 5x4 array
105+
int[][] c = new int[5][4];
106+
107+
for (int power = 2; power < 7; power++) {
108+
for (int base = 2; base < 6; base++) {
109+
// assign the correct element based on the pattern
110+
c[power - 2][base - 2] = (int) (Math.pow(base, power));
111+
112+
// print the current element
113+
System.out.print(c[power - 2][base - 2] + "\t");
114+
}
115+
System.out.println(); // move to next row
116+
}
117+
}
118+
119+
/**
120+
* Returns a random integer
121+
* in the range [min, max]
122+
* @param min minimum random integer
123+
* @param max maximum random integer
124+
* @return a random integer in the range
125+
* [min, max]
126+
*/
127+
public static int randInt(int min, int max) {
128+
return min + (int) (Math.random() * ((max - min) + 1));
129+
}
130+
}

src/com/codefortomorrow/intermediate/chapter10/solutions/twoDArrayPrograms.java

Lines changed: 0 additions & 95 deletions
This file was deleted.

0 commit comments

Comments
 (0)