-
-
Save bytecodeman/e6759417e867dcb0936f4e8d6067da0e to your computer and use it in GitHub Desktop.
CSC-111 Two Dimensional Array Example
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
// (x+1)^0 1 1 | |
// (x+1)^1 1 1 x + 1 | |
// (x+1)^2 1 2 1 x^2 + 2x + 1 | |
// (x+1)^3 1 3 3 1 x^3 + 3x^2 + 3x + 1 | |
// (x+1)^4 1 4 6 4 1 x^4 + 4x^3 + 6x^2 + 4x + 1 | |
// (x+1)^5 1 5 10 10 5 1 x^5 + 5x^4 + 10x^3 + 10x^2 = 5x + 1 | |
// (x+1)^6 1 6 15 20 15 6 1 ... | |
// ... | |
public class PascalTriangle { | |
public static void main(String args[]) { | |
int pt[][] = new int[10][10]; | |
generateTriangle(pt); | |
printTriangle(pt); | |
printXplus1Expansions(pt); | |
} | |
private static void generateTriangle(int x[][]) { | |
for (int row = 0; row < x.length; row++) { | |
x[row][0] = 1; | |
x[row][row] = 1; | |
for (int col = 1; col < row; col++) | |
x[row][col] = x[row-1][col-1] + x[row-1][col]; | |
} | |
} | |
private static void printTriangle(int x[][]) { | |
for (int row = 0; row < x.length; row++) { | |
for (int col = 0; col <= row; col++) | |
System.out.printf("%6d", x[row][col]); | |
System.out.println(); | |
} | |
} | |
private static void printXplus1Expansions(final int x[][]) { | |
if (x == null || x.length == 0) | |
return; | |
for (int row = 0; row < x.length; row++) { | |
if (row == 0) { | |
System.out.printf("1"); | |
System.out.println(); | |
} | |
else if (row == 1) { | |
System.out.printf("x + 1"); | |
System.out.println(); | |
} | |
else { | |
System.out.printf("x^%d", row); | |
for (int col = 1; col < row; col++) { | |
if (col == row - 1) { | |
System.out.printf(" + %dx", x[row][col]); | |
} | |
else { | |
System.out.printf(" + %dx^%d", x[row][col], row - col); | |
} | |
} | |
System.out.printf(" + %d", 1); | |
System.out.println(); | |
} | |
} | |
} | |
} |
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
public class PascalTriangle2 { | |
public static void main(String args[]) { | |
int pt[][] = generateTriangle(); | |
printTriangle(pt); | |
} | |
// i -> array | |
// 0 -> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] | |
// 1 -> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] | |
// 2 -> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] | |
// 3 -> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] | |
// 4 -> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] | |
// 5 -> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] | |
// ... | |
private static int[][] generateTriangle() { | |
int x[][] = new int[10][10]; | |
for (int row = 0; row < x.length; row++) { | |
x[row][0] = 1; | |
x[row][row] = 1; | |
for (int col = 1; col < row; col++) | |
x[row][col] = x[row-1][col-1] + x[row-1][col]; | |
} | |
return x; | |
} | |
private static void printTriangle(int x[][]) { | |
for (int row = 0; row < x.length; row++) { | |
for (int col = 0; col <= row; col++) | |
System.out.printf("%6d", x[row][col]); | |
System.out.println(); | |
} | |
} | |
} |
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
public class PascalTriangle3 { | |
public static void main(String args[]) { | |
var pt = initStructure(10); | |
generateTriangle(pt); | |
printTriangle(pt); | |
} | |
// i -> array | |
// 0 -> [0] | |
// 1 -> [0, 0] | |
// 2 -> [0, 0, 0] | |
// 3 -> [0, 0, 0, 0] | |
// 4 -> [0, 0, 0, 0, 0] | |
// 5 -> [0, 0, 0, 0, 0, 0] | |
// ... | |
private static int[][] initStructure(final int size) { | |
var x = new int[size][]; | |
for (int i = 0; i < size; i++) | |
x[i] = new int[i+1]; | |
return x; | |
} | |
private static void generateTriangle(final int x[][]) { | |
for (int row = 0; row < x.length; row++) { | |
x[row][0] = 1; | |
x[row][row] = 1; | |
for (int col = 1; col < row; col++) | |
x[row][col] = x[row-1][col-1] + x[row-1][col]; | |
} | |
} | |
private static void printTriangle(final int x[][]) { | |
for (int row = 0; row < x.length; row++) { | |
for (int col = 0; col <= row; col++) | |
System.out.printf("%6d", x[row][col]); | |
System.out.println(); | |
} | |
} | |
} |
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
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