From 633d0fc2872909b78886c343d4e6781670948180 Mon Sep 17 00:00:00 2001 From: Evan Ferrao Date: Wed, 14 Aug 2024 02:11:32 +0530 Subject: [PATCH] add array questions > thats some crazy pascals triangle code --- Arrays/PascalsTriangle.java | 100 ++++++++++++++++++++++ Arrays/RotationMatrix.java | 76 ++++++++++++++++ Arrays/SpiralMatrixGeneration.java | 61 +++++++++++++ Arrays/SpiralMatrixTraversal.java | 51 +++++++++++ {Transpose => Arrays}/TansposeMatrix.java | 3 +- 5 files changed, 289 insertions(+), 2 deletions(-) create mode 100644 Arrays/PascalsTriangle.java create mode 100644 Arrays/RotationMatrix.java create mode 100644 Arrays/SpiralMatrixGeneration.java create mode 100644 Arrays/SpiralMatrixTraversal.java rename {Transpose => Arrays}/TansposeMatrix.java (95%) diff --git a/Arrays/PascalsTriangle.java b/Arrays/PascalsTriangle.java new file mode 100644 index 0000000..0e0958b --- /dev/null +++ b/Arrays/PascalsTriangle.java @@ -0,0 +1,100 @@ +// write a code in java to print the pascals triangle for a given number of rows using a function +import java.util.Scanner; + +class PascalsTriangle{ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("Enter the number of rows: "); + int rows = sc.nextInt(); + System.out.println("Pascal's Triangle: "); + // for(int i=0; i=0; j--){ + if(j == 0 || j == i){ + dp[j] = 1; + }else{ + dp[j] = dp[j-1] + dp[j]; + } + } + + // does not save the entire triangle, only the current row + // its indented in the loop to print the triangle + for(int j=0; j<=i; j++){ + System.out.print(dp[j] + " "); + } + System.out.println(); + } + } +} \ No newline at end of file diff --git a/Arrays/RotationMatrix.java b/Arrays/RotationMatrix.java new file mode 100644 index 0000000..545361a --- /dev/null +++ b/Arrays/RotationMatrix.java @@ -0,0 +1,76 @@ +// wrie a code in java to print the rotation matrix using the functions calculateRotation and calculateTranspose +import java.util.Scanner; +class RotationMatrix{ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.print("Enter the number of rows and columns: "); + int rows = sc.nextInt(); + int columns = sc.nextInt(); + int[][] matrix = new int[rows][columns]; + System.out.println("Enter the elements of the matrix: "); + for(int i = 0; i < rows; i++){ + for(int j = 0; j < columns; j++){ + matrix[i][j] = sc.nextInt(); + } + } + System.out.println("Original Matrix: "); + printMatrix(matrix); + System.out.println("Transpose Matrix: "); + printMatrix(calculateTranspose(matrix)); + System.out.println("Rotation Matrix: "); + printMatrix(calculateRotation(matrix)); + sc.close(); + } + public static int[][] calculateTranspose(int[][] matrix){ + int rows = matrix.length; + int columns = matrix[0].length; + int[][] transpose = new int[columns][rows]; + for(int i = 0; i < rows; i++){ + for(int j = 0; j < columns; j++){ + transpose[j][i] = matrix[i][j]; + } + } + return transpose; + } + public static int[][] calculateRotation(int[][] matrix){ + int rows = matrix.length; + int columns = matrix[0].length; + int[][] rotation = new int[columns][rows]; + for(int i = 0; i < rows; i++){ + for(int j = 0; j < columns; j++){ + rotation[j][rows - i - 1] = matrix[i][j]; + } + } + return rotation; + } + public static void printMatrix(int[][] matrix){ + for(int[] row : matrix){ + for(int element : row){ + System.out.print(element + " "); + } + System.out.println(); + } + } + + // calculate rotation from transpose + + public static void reverseArray(int[] arr){ + int start = 0; + int end = arr.length - 1; + while(start < end){ + int temp = arr[start]; + arr[start] = arr[end]; + arr[end] = temp; + start++; + end--; + } + } + public static int[][] calculateRotationFromTranspose(int[][] matrix){ + int transpose[][] = calculateTranspose(matrix); + for (int i = 0; i < transpose.length; i++) { + reverseArray(transpose[i]); + } + + return transpose; + } +} \ No newline at end of file diff --git a/Arrays/SpiralMatrixGeneration.java b/Arrays/SpiralMatrixGeneration.java new file mode 100644 index 0000000..0d8be67 --- /dev/null +++ b/Arrays/SpiralMatrixGeneration.java @@ -0,0 +1,61 @@ +// write a program to generate a n * n matrix in spiral order given user input n and the elements of the matrix + +import java.util.Scanner; +class SpiralMatrixGeneration{ + + public static void main(String[] args){ + Scanner sc = new Scanner(System.in); + System.out.println("Enter the number of rows and columns of the matrix"); + int rows = sc.nextInt(); + int cols = sc.nextInt(); + int length = rows*cols; + int[][] matrix = new int[rows][cols]; + int lengthArray[] = new int[length]; + System.out.println("Enter the elements of the matrix"); + for(int i=0;i=left;i--){ + matrix[bottom][i] = lengthArray[length_index++]; + } + bottom--; + } + if(left<=right){ + for(int i=bottom;i>=top;i--){ + matrix[i][left] = lengthArray[length_index++]; + } + left++; + } + } + } + +} \ No newline at end of file diff --git a/Arrays/SpiralMatrixTraversal.java b/Arrays/SpiralMatrixTraversal.java new file mode 100644 index 0000000..de2e6f0 --- /dev/null +++ b/Arrays/SpiralMatrixTraversal.java @@ -0,0 +1,51 @@ +// write a code in java to print the elements in a matrix in a spiral order using functions + +import java.util.Scanner; +class SpiralMatrixTraversal{ + public static void main(String[] args){ + Scanner sc = new Scanner(System.in); + System.out.println("Enter the number of rows and columns of the matrix"); + int rows = sc.nextInt(); + int cols = sc.nextInt(); + int[][] matrix = new int[rows][cols]; + System.out.println("Enter the elements of the matrix"); + for(int i=0;i=left;i--){ + System.out.print(matrix[bottom][i]+" "); + } + bottom--; + } + if(left<=right){ + for(int i=bottom;i>=top;i--){ + System.out.print(matrix[i][left]+" "); + } + left++; + } + } + } +} \ No newline at end of file diff --git a/Transpose/TansposeMatrix.java b/Arrays/TansposeMatrix.java similarity index 95% rename from Transpose/TansposeMatrix.java rename to Arrays/TansposeMatrix.java index 1da3ba4..d489475 100644 --- a/Transpose/TansposeMatrix.java +++ b/Arrays/TansposeMatrix.java @@ -45,7 +45,7 @@ calss TransposeMatrix { return result; } - public static int[][] transposeOriginalMatrix(int[][] matrix) { + public static void transposeOriginalMatrix(int[][] matrix) { int rows = matrix.length; int cols = matrix[0].length; for (int i = 0; i < rows; i++) { @@ -55,6 +55,5 @@ calss TransposeMatrix { matrix[j][i] = temp; } } - return matrix; } } \ No newline at end of file