Instantly share code, notes, and snippets.

Embed
What would you like to do?
CSC-111 Two Dimensional Array Example
public class TwoDArrayExample {
private static int[][] initMatrix(int numRows, int numCols) {
int x[][] = new int[numRows][numCols];
for (int i = 0; i < x.length; i++)
for (int j = 0; j < x[i].length; j++)
x[i][j] = (int) (100 * Math.random()) + 1;
return x;
}
private static void printMatrix(int[][] x) {
for (int i = 0; i < x.length; i++) {
for (int j = 0; j < x[i].length; j++)
System.out.printf("%5d", x[i][j]);
System.out.println();
}
}
private static int biggest(int numbs[][]) {
int largest = numbs[0][0];
for (int i = 0; i < numbs.length; i++)
for (int j = 0; j < numbs[i].length; j++)
if (numbs[i][j] > largest)
largest = numbs[i][j];
return largest;
}
public static void main(String args[]) {
int x[][] = initMatrix(10, 8);
printMatrix(x);
System.out.printf("Biggest Number in Array = %d\n", biggest(x));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment