|
| 1 | +public class Arrays_2D { |
| 2 | + public static void main(String args[]){ |
| 3 | +// please sure to uncomment the examples one by one |
| 4 | +//-----------------------------TODO: Example_1---------------------------------- |
| 5 | +// TODO: Manually allocate differing size second dimensions (columns). |
| 6 | + int[][] twoD = new int[4][]; |
| 7 | + twoD[0] = new int[1]; // one column for first row |
| 8 | + twoD[1] = new int[2]; // two columns for second row |
| 9 | + twoD[2] = new int[3]; // three columns for third row |
| 10 | + twoD[3] = new int[4]; // four columns for fourth row |
| 11 | + int i, j, k = 0; |
| 12 | + // add items (values) to the array |
| 13 | + for(i=0; i<4; i++) { |
| 14 | + for (j = 0; j < i + 1; j++) { |
| 15 | + twoD[i][j] = k; |
| 16 | + k++; |
| 17 | + } |
| 18 | + } |
| 19 | + // print the array |
| 20 | + for(i=0; i<4; i++) { |
| 21 | + for(j=0; j<i+1; j++) |
| 22 | + System.out.print(twoD[i][j] + " "); |
| 23 | + System.out.println(); |
| 24 | + } |
| 25 | +// ---------------------------TODO: Example_2-------------------------- |
| 26 | +// int[][] matrix = { |
| 27 | +// {1, 2, 3}, |
| 28 | +// {4, 5, 6}, |
| 29 | +// {7, 8, 9} |
| 30 | +// }; |
| 31 | +// // Accessing elements using nested loops |
| 32 | +// for (int i = 0; i < matrix.length; i++) { |
| 33 | +// for (int j = 0; j < matrix[i].length; j++) { |
| 34 | +// System.out.print(matrix[i][j] + " "); |
| 35 | +// } |
| 36 | +// System.out.println(); |
| 37 | +// } |
| 38 | +// -------------------------TODO: Example_3-------------------------------- |
| 39 | +// TODO: Initializing a 2D Array with User Input |
| 40 | +// Scanner scanner = new Scanner(System.in); |
| 41 | +// // Get the dimensions of the 2D array from the user |
| 42 | +// System.out.print("Enter the number of rows: "); |
| 43 | +// int rows = scanner.nextInt(); |
| 44 | +// System.out.print("Enter the number of columns: "); |
| 45 | +// int columns = scanner.nextInt(); |
| 46 | +// // Initialize a 2D array based on user input |
| 47 | +// int[][] userArray = new int[rows][columns]; |
| 48 | +// // Populate the array with user input |
| 49 | +// System.out.println("Enter the elements of the array:"); |
| 50 | +// for (int i = 0; i < rows; i++) { |
| 51 | +// for (int j = 0; j < columns; j++) { |
| 52 | +// userArray[i][j] = scanner.nextInt(); |
| 53 | +// } |
| 54 | +// } |
| 55 | +// // Display the user-initialized 2D array |
| 56 | +// System.out.println("User-Initialized 2D Array:"); |
| 57 | +// for (int i = 0; i < rows; i++) { |
| 58 | +// for (int j = 0; j < columns; j++) { |
| 59 | +// System.out.print(userArray[i][j] + " "); |
| 60 | +// } |
| 61 | +// System.out.println(); |
| 62 | +// } |
| 63 | +// -----------------------------TODO: Example_4---------------------------- |
| 64 | +// TODO: Sum of Elements in a 2D Array |
| 65 | +// int[][] matrix = { |
| 66 | +// {1, 2, 3}, |
| 67 | +// {4, 5, 6}, |
| 68 | +// {7, 8, 9} |
| 69 | +// }; |
| 70 | + // Calculating the sum of all elements |
| 71 | +// int sum = 0; |
| 72 | +// int sum_all = 0; |
| 73 | +// for (int i = 0; i < matrix.length; i++) { |
| 74 | +// for (int j = 0; j < matrix[i].length; j++) { |
| 75 | +// sum_all += matrix[i][j]; |
| 76 | +// sum += matrix[i][j]; // sum of every row |
| 77 | +// } |
| 78 | +// System.out.println("sum of row ["+i+"]"+sum); |
| 79 | +// sum=0; |
| 80 | +// } |
| 81 | +// System.out.println("Sum of all elements: " + sum_all); |
| 82 | +// ---------------------------TODO: Example_5------------------------------ |
| 83 | +// TODO: Finding the Maximum Element |
| 84 | +// int[][] matrix = { |
| 85 | +// {3, 7, 1}, |
| 86 | +// {8, 2, 5}, |
| 87 | +// {4, 6, 9} |
| 88 | +// }; |
| 89 | +// // Finding the maximum element |
| 90 | +// int max = matrix[0][0]; |
| 91 | +// for (int i = 0; i < matrix.length; i++) { |
| 92 | +// for (int j = 0; j < matrix[i].length; j++) { |
| 93 | +// if (matrix[i][j] > max) { |
| 94 | +// max = matrix[i][j]; |
| 95 | +// } |
| 96 | +// } |
| 97 | +// } |
| 98 | +// System.out.println("Maximum element: " + max); |
| 99 | +// ---------------------------TODO: Example_6------------------------------ |
| 100 | +// TODO: Transposing a Matrix (swapping its rows with its columns) |
| 101 | +// int[][] matrix = { |
| 102 | +// {1, 2, 3}, |
| 103 | +// {4, 5, 6}, |
| 104 | +// {7, 8, 9} |
| 105 | +// }; |
| 106 | +//// Transposing the matrix |
| 107 | +// int[][] transposedMatrix = new int[matrix[0].length][matrix.length]; |
| 108 | +// for (int i = 0; i < matrix.length; i++) { |
| 109 | +// for (int j = 0; j < matrix[i].length; j++) { |
| 110 | +// transposedMatrix[j][i] = matrix[i][j]; |
| 111 | +// } |
| 112 | +// } |
| 113 | +// for (int i = 0; i < transposedMatrix.length; i++) { |
| 114 | +// for (int j = 0; j < transposedMatrix[i].length; j++) { |
| 115 | +// System.out.print(transposedMatrix[i][j] + " "); |
| 116 | +// } |
| 117 | +// System.out.println(); |
| 118 | +// } |
| 119 | +// ----------------------------TODO: Example_7----------------------------- |
| 120 | +// TODO: Matrix Addition (the operation of adding two matrices by adding their corresponding elements) |
| 121 | +// int[][] matrix1 = { |
| 122 | +// {1, 2, 3}, |
| 123 | +// {4, 5, 6}, |
| 124 | +// {7, 8, 9} |
| 125 | +// }; |
| 126 | +// int[][] matrix2 = { |
| 127 | +// {9, 8, 7}, |
| 128 | +// {6, 5, 4}, |
| 129 | +// {3, 2, 1} |
| 130 | +// }; |
| 131 | +// // Performing matrix addition |
| 132 | +// int[][] resultMatrix = new int[matrix1.length][matrix1[0].length]; |
| 133 | +// for (int i = 0; i < matrix1.length; i++) { |
| 134 | +// for (int j = 0; j < matrix1[i].length; j++) { |
| 135 | +// resultMatrix[i][j] = matrix1[i][j] + matrix2[i][j]; |
| 136 | +// } |
| 137 | +// } |
| 138 | +// // Displaying the result matrix |
| 139 | +// System.out.println("Matrix Addition Result:"); |
| 140 | +// for (int i = 0; i < resultMatrix.length; i++) { |
| 141 | +// for (int j = 0; j < resultMatrix[i].length; j++) { |
| 142 | +// System.out.print(resultMatrix[i][j] + " "); |
| 143 | +// } |
| 144 | +// System.out.println(); |
| 145 | +// } |
| 146 | +// -----------------------------TODO: Example_8---------------------------- |
| 147 | +// TODO: Matrix Multiplication |
| 148 | + |
| 149 | +// int[][] matrix1 = { |
| 150 | +// {1, 2}, |
| 151 | +// {3, 4}, |
| 152 | +// {5, 6} |
| 153 | +// }; |
| 154 | +// int[][] matrix2 = { |
| 155 | +// {7, 8, 9}, |
| 156 | +// {10, 11, 12} |
| 157 | +// }; |
| 158 | +// // Performing matrix multiplication |
| 159 | +// int[][] resultMatrix = new int[matrix1.length][matrix2[0].length]; |
| 160 | +// for (int i = 0; i < matrix1.length; i++) { |
| 161 | +// for (int j = 0; j < matrix2[0].length; j++) { |
| 162 | +// for (int k = 0; k < matrix1[0].length; k++) { |
| 163 | +// resultMatrix[i][j] += matrix1[i][k] * matrix2[k][j]; |
| 164 | +// } |
| 165 | +// } |
| 166 | +// } |
| 167 | +// // Displaying the result matrix |
| 168 | +// System.out.println("Matrix Multiplication Result:"); |
| 169 | +// for (int i = 0; i < resultMatrix.length; i++) { |
| 170 | +// for (int j = 0; j < resultMatrix[i].length; j++) { |
| 171 | +// System.out.print(resultMatrix[i][j] + " "); |
| 172 | +// } |
| 173 | +// System.out.println(); |
| 174 | +// } |
| 175 | +// -----------------------------TODO: Example_9---------------------------- |
| 176 | +// TODO: Matrix Scalar Multiplication |
| 177 | + // Initializing a matrix |
| 178 | +// int[][] matrix = { |
| 179 | +// {1, 2, 3}, |
| 180 | +// {4, 5, 6}, |
| 181 | +// {7, 8, 9} |
| 182 | +// }; |
| 183 | +// // Performing scalar multiplication |
| 184 | +// int scalar = 2; |
| 185 | +// int[][] resultMatrix = new int[matrix.length][matrix[0].length]; |
| 186 | +// for (int i = 0; i < matrix.length; i++) { |
| 187 | +// for (int j = 0; j < matrix[i].length; j++) { |
| 188 | +// resultMatrix[i][j] = scalar * matrix[i][j]; |
| 189 | +// } |
| 190 | +// } |
| 191 | +// // Displaying the result matrix |
| 192 | +// System.out.println("Scalar Multiplication Result:"); |
| 193 | +// for (int i = 0; i < resultMatrix.length; i++) { |
| 194 | +// for (int j = 0; j < resultMatrix[i].length; j++) { |
| 195 | +// System.out.print(resultMatrix[i][j] + " "); |
| 196 | +// } |
| 197 | +// System.out.println(); |
| 198 | +// } |
| 199 | +// -----------------------------TODO: Example_10---------------------------- |
| 200 | +// TODO: Finding the Sum of Each Row in a 2D Array |
| 201 | + |
| 202 | +// int[][] matrix = { |
| 203 | +// {1, 2, 3}, |
| 204 | +// {4, 5, 6}, |
| 205 | +// {7, 8, 9} |
| 206 | +// }; |
| 207 | +// // Finding the sum of each row |
| 208 | +// for (int i = 0; i < matrix.length; i++) { |
| 209 | +// int rowSum = 0; |
| 210 | +// for (int j = 0; j < matrix[i].length; j++) { |
| 211 | +// rowSum += matrix[i][j]; |
| 212 | +// } |
| 213 | +// System.out.println("Sum of elements in Row " + (i + 1) + ": " + rowSum); |
| 214 | +// } |
| 215 | +// ---------------------------TODO: Example_11------------------------------ |
| 216 | +// TODO: Rotating a Matrix 90 Degrees |
| 217 | + // Initializing a 3x3 matrix |
| 218 | +// int[][] matrix = { |
| 219 | +// {1, 2, 3}, |
| 220 | +// {4, 5, 6}, |
| 221 | +// {7, 8, 9} |
| 222 | +// }; |
| 223 | +// // Rotating the matrix 90 degrees clockwise |
| 224 | +// int[][] rotatedMatrix = new int[matrix[0].length][matrix.length]; |
| 225 | +// for (int i = 0; i < matrix.length; i++) { |
| 226 | +// for (int j = 0; j < matrix[i].length; j++) { |
| 227 | +// rotatedMatrix[j][matrix.length - 1 - i] = matrix[i][j]; |
| 228 | +// } |
| 229 | +// } |
| 230 | +// // Printing the rotated matrix |
| 231 | +// for (int i = 0; i < rotatedMatrix.length; i++) { |
| 232 | +// for (int j = 0; j < rotatedMatrix[i].length; j++) { |
| 233 | +// System.out.print(rotatedMatrix[i][j] + " "); |
| 234 | +// } |
| 235 | +// System.out.println(); |
| 236 | +// } |
| 237 | +// -----------------------------TODO: Example_12---------------------------- |
| 238 | +// TODO: finding the sum of each column |
| 239 | +// int[][] matrix = { |
| 240 | +// {1, 2, 3}, |
| 241 | +// {4, 5, 6}, |
| 242 | +// {7, 8, 9} |
| 243 | +// }; |
| 244 | +// // Finding the sum of each column |
| 245 | +// int columns = matrix[0].length; |
| 246 | +// for (int j = 0; j < columns; j++) { |
| 247 | +// int columnSum = 0; |
| 248 | +// for (int i = 0; i < matrix.length; i++) { |
| 249 | +// columnSum += matrix[i][j]; |
| 250 | +// } |
| 251 | +// System.out.println("Sum of elements in Column " + (j + 1) + ": " + columnSum); |
| 252 | +// } |
| 253 | +// ---------------------------TODO: Example_13------------------------------ |
| 254 | +// TODO: Transposing a 2D Array Without Creating a New Array |
| 255 | +// int[][] matrix = { |
| 256 | +// {1, 2, 3}, |
| 257 | +// {4, 5, 6}, |
| 258 | +// {7, 8, 9} |
| 259 | +// }; |
| 260 | +// // Transposing the matrix in-place |
| 261 | +// for (int i = 0; i < matrix.length; i++) { |
| 262 | +// for (int j = i + 1; j < matrix[i].length; j++) { |
| 263 | +// // Swap elements at (i, j) and (j, i) |
| 264 | +// int temp = matrix[i][j]; |
| 265 | +// matrix[i][j] = matrix[j][i]; |
| 266 | +// matrix[j][i] = temp; |
| 267 | +// } |
| 268 | +// } |
| 269 | +// // Displaying the transposed matrix |
| 270 | +// System.out.println("In-Place Transposed Matrix:"); |
| 271 | +// for (int i = 0; i < matrix.length; i++) { |
| 272 | +// for (int j = 0; j < matrix[i].length; j++) { |
| 273 | +// System.out.print(matrix[i][j] + " "); |
| 274 | +// } |
| 275 | +// System.out.println(); |
| 276 | +// } |
| 277 | +// ---------------------------TODO: Example_14------------------------------ |
| 278 | +// TODO: Finding the Sum of Diagonals in a Square Matrix |
| 279 | + // Initializing a 4x4 matrix |
| 280 | +// int[][] matrix = { |
| 281 | +// {1, 2, 3, 4}, |
| 282 | +// {5, 6, 7, 8}, |
| 283 | +// {9, 10, 11, 12}, |
| 284 | +// {13, 14, 15, 16} |
| 285 | +// }; |
| 286 | +// int primaryDiagonalSum = 0; |
| 287 | +// int secondaryDiagonalSum = 0; |
| 288 | +// // Calculating the sum of the primary diagonal |
| 289 | +// for (int i = 0; i < matrix.length; i++) { |
| 290 | +// primaryDiagonalSum += matrix[i][i]; |
| 291 | +// } |
| 292 | +// // Calculating the sum of the secondary diagonal |
| 293 | +// for (int i = 0; i < matrix.length; i++) { |
| 294 | +// secondaryDiagonalSum += matrix[i][matrix.length - 1 - i]; |
| 295 | +// } |
| 296 | +// System.out.println("Sum of primary diagonal: " + primaryDiagonalSum); |
| 297 | +// System.out.println("Sum of secondary diagonal: " + secondaryDiagonalSum); |
| 298 | + } |
| 299 | +} |
0 commit comments