Last active
October 29, 2019 18:27
-
-
Save DanCoughlin/84d366d397090f677aa25a006f8bdf35 to your computer and use it in GitHub Desktop.
2D Random Array and Max Value #module5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
a: | |
Create a two dimensional array that is | |
5 rows (outer loop) referenced with first set of brackets [x][] | |
by | |
10 columns (inner loop) referenced with second set of brackets [][x] | |
put a random number between 1-100 in each position. | |
b: | |
Print out all the numbers in a 5 row by 10 column format | |
c: | |
Then loop over each row and find the max value in that row and print out the | |
max value. | |
*/ | |
final int ROWS = 5; | |
final int COLS = 10; | |
int[][] randos = new int[ROWS][COLS]; | |
// create the table | |
for (int i=0;i < ROWS; i++) { | |
for (int j=0; j < COLS; j++) { | |
randos[i][j] = (int) (Math.random() * 100) + 1; | |
System.out.printf("%4d", randos[i][j]); | |
} | |
System.out.println(""); | |
} | |
// find max value | |
for (int i=0; i < ROWS; i++) { | |
int maxRowValue = 0; | |
for (int j=0; j < COLS; j++) { | |
if ( j==0 || maxRowValue < randos[i][j]) { | |
maxRowValue = randos[i][j]; | |
} | |
} | |
System.out.println("Max Value at Row " + (i+1) + " " + maxRowValue); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment