Arrays 2D & 3D – Let us C https://c.praveshagrawal.com Example C Programs Sat, 02 Dec 2023 11:01:27 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 3D Array Example – Cuboid Volume Calculation https://c.praveshagrawal.com/2023/12/02/3d-array-example-cuboid-volume-calculation/ https://c.praveshagrawal.com/2023/12/02/3d-array-example-cuboid-volume-calculation/#respond Sat, 02 Dec 2023 11:01:27 +0000 https://c.praveshagrawal.com/?p=50 #include <stdio.h> #define LENGTH 2 #define WIDTH 3 #define HEIGHT 4 int main() { // Cuboid volume calculation using a 3D array int cuboid[LENGTH][WIDTH][HEIGHT]; // Initialize cuboid dimensions for (int i = 0; i < LENGTH; i++) { for (int j = 0; j < WIDTH; j++) { for (int k = 0; k < HEIGHT; k++) { cuboid[i][j][k] = i + j + k; // Arbitrary initialization for demonstration } } } // Calculate and print the cuboid volumes printf("Cuboid Volumes:\n"); for (int i = 0; i < LENGTH; i++) { for (int j = 0; j < WIDTH; j++) { for (int k = 0; k < HEIGHT; k++) { printf("Volume[%d][%d][%d]: %d\n", i, j, k, cuboid[i][j][k]); } } } return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/3d-array-example-cuboid-volume-calculation/feed/ 0 2D Array Example – Matrix Multiplication https://c.praveshagrawal.com/2023/12/02/2d-array-example-matrix-multiplication/ https://c.praveshagrawal.com/2023/12/02/2d-array-example-matrix-multiplication/#respond Sat, 02 Dec 2023 11:01:02 +0000 https://c.praveshagrawal.com/?p=48 #include <stdio.h> #define ROWS_A 2 #define COLS_A 3 #define ROWS_B 3 #define COLS_B 2 int main() { // Matrix multiplication using a 2D array int matrixA[ROWS_A][COLS_A] = {{1, 2, 3}, {4, 5, 6}}; int matrixB[ROWS_B][COLS_B] = {{7, 8}, {9, 10}, {11, 12}}; int result[ROWS_A][COLS_B]; printf("Matrix A:\n"); for (int i = 0; i < ROWS_A; i++) { for (int j = 0; j < COLS_A; j++) { printf("%d\t", matrixA[i][j]); } printf("\n"); } printf("\nMatrix B:\n"); for (int i = 0; i < ROWS_B; i++) { for (int j = 0; j < COLS_B; j++) { printf("%d\t", matrixB[i][j]); } printf("\n"); } // Matrix multiplication for (int i = 0; i < ROWS_A; i++) { for (int j = 0; j < COLS_B; j++) { result[i][j] = 0; for (int k = 0; k < COLS_A; k++) { result[i][j] += matrixA[i][k] * matrixB[k][j]; } } } printf("\nResult Matrix:\n"); for (int i = 0; i < ROWS_A; i++) { for (int j = 0; j < COLS_B; j++) { printf("%d\t", result[i][j]); } printf("\n"); } return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/2d-array-example-matrix-multiplication/feed/ 0