C Online Compiler
Example: Matrix Multiplication using Pointers in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Matrix Multiplication using Pointers #include <stdio.h> // For input/output operations (printf, scanf) #include <stdlib.h> // For dynamic memory allocation (malloc, free) // Function to allocate memory for a matrix int** allocateMatrix(int rows, int cols) { int** matrix = (int**)malloc(rows * sizeof(int*)); // Allocate memory for rows if (matrix == NULL) { printf("Memory allocation failed for matrix rows!\n"); exit(1); // Exit if allocation fails } for (int i = 0; i < rows; i++) { matrix[i] = (int*)malloc(cols * sizeof(int)); // Allocate memory for columns in each row if (matrix[i] == NULL) { printf("Memory allocation failed for matrix columns!\n"); // Free previously allocated row pointers before exiting for (int j = 0; j < i; j++) { free(matrix[j]); } free(matrix); exit(1); } } return matrix; } // Function to free memory allocated for a matrix void freeMatrix(int** matrix, int rows) { for (int i = 0; i < rows; i++) { free(matrix[i]); // Free memory for each row } free(matrix); // Free memory for the array of row pointers } // Function to read matrix elements from user void readMatrix(int** matrix, int rows, int cols, char name) { printf("Enter elements for Matrix %c (%dx%d):\n", name, rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("Enter element [%d][%d]: ", i, j); scanf("%d", &matrix[i][j]); } } } // Function to print a matrix void printMatrix(int** matrix, int rows, int cols, char name) { printf("\nMatrix %c (%dx%d):\n", name, rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { printf("%5d ", matrix[i][j]); } printf("\n"); } } int main() { int rows1, cols1, rows2, cols2; // Step 1: Get dimensions for Matrix A from user printf("Enter dimensions for Matrix A (rows cols): "); scanf("%d %d", &rows1, &cols1); // Step 2: Get dimensions for Matrix B from user printf("Enter dimensions for Matrix B (rows cols): "); scanf("%d %d", &rows2, &cols2); // Step 3: Check if multiplication is possible if (cols1 != rows2) { printf("Error: Number of columns in Matrix A must be equal to number of rows in Matrix B.\n"); return 1; // Indicate error } // Step 4: Dynamically allocate memory for matrices int** matrixA = allocateMatrix(rows1, cols1); int** matrixB = allocateMatrix(rows2, cols2); int** resultMatrix = allocateMatrix(rows1, cols2); // Result matrix will be rows1 x cols2 // Step 5: Read elements for Matrix A and Matrix B readMatrix(matrixA, rows1, cols1, 'A'); readMatrix(matrixB, rows2, cols2, 'B'); // Step 6: Print original matrices printMatrix(matrixA, rows1, cols1, 'A'); printMatrix(matrixB, rows2, cols2, 'B'); // Step 7: Perform matrix multiplication printf("\nPerforming matrix multiplication...\n"); for (int i = 0; i < rows1; i++) { for (int j = 0; j < cols2; j++) { resultMatrix[i][j] = 0; // Initialize element for (int k = 0; k < cols1; k++) { // Or k < rows2, since cols1 == rows2 resultMatrix[i][j] += matrixA[i][k] * matrixB[k][j]; } } } // Step 8: Print the result matrix printMatrix(resultMatrix, rows1, cols2, 'C'); // C for result // Step 9: Free allocated memory freeMatrix(matrixA, rows1); freeMatrix(matrixB, rows2); freeMatrix(resultMatrix, rows1); printf("\nMemory deallocated successfully.\n"); return 0; // Indicate successful execution }
Output
Clear
ADVERTISEMENTS