0% found this document useful (0 votes)
15 views4 pages

Matrix Operations in C Programming

The document is a C program that implements basic matrix operations including addition, multiplication, and transposition. It provides functions to input and display matrices, as well as to perform the specified operations based on user input through a menu. The program continues to prompt the user for operations until they choose to exit.

Uploaded by

losagab472
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Matrix Operations in C Programming

The document is a C program that implements basic matrix operations including addition, multiplication, and transposition. It provides functions to input and display matrices, as well as to perform the specified operations based on user input through a menu. The program continues to prompt the user for operations until they choose to exit.

Uploaded by

losagab472
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include <stdio.

h>

// Function to input matrix


void inputMatrix(int rows, int cols, int mat[rows][cols]) {
printf("Enter elements of matrix (%dx%d):\n", rows, cols);
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &mat[i][j]);
}
}
}

// Function to display matrix


void displayMatrix(int rows, int cols, int mat[rows][cols]) {
printf("\nMatrix (%dx%d):\n", rows, cols);
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
}

// Function for matrix addition


void addMatrices(int rows, int cols, int A[rows][cols], int B[rows][cols], int result[rows]
[cols]) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
result[i][j] = A[i][j] + B[i][j];
}
}
}

// Function for matrix multiplication


int multiplyMatrices(int r1, int c1, int A[r1][c1], int r2, int c2, int B[r2][c2], int
result[r1][c2]) {
if(c1 != r2) {
printf("\nError: Matrix multiplication not possible!\n");
printf("Columns of first matrix must equal rows of second matrix.\n");
return 0;
}

for(int i = 0; i < r1; i++) {


for(int j = 0; j < c2; j++) {
result[i][j] = 0;
for(int k = 0; k < c1; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
return 1;
}

// Function for matrix transpose


void transposeMatrix(int rows, int cols, int mat[rows][cols], int trans[cols][rows]) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
trans[j][i] = mat[i][j];
}
}
}

// Operation 1: Addition
void performAddition() {
int rows, cols;

printf("\n--- MATRIX ADDITION ---\n");


printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);

int A[rows][cols], B[rows][cols], result[rows][cols];

printf("\nMatrix A:\n");
inputMatrix(rows, cols, A);

printf("\nMatrix B:\n");
inputMatrix(rows, cols, B);

addMatrices(rows, cols, A, B, result);

printf("\n--- RESULT ---");


displayMatrix(rows, cols, A);
printf("+");
displayMatrix(rows, cols, B);
printf("=");
displayMatrix(rows, cols, result);
}

// Operation 2: Multiplication
void performMultiplication() {
int r1, c1, r2, c2;

printf("\n--- MATRIX MULTIPLICATION ---\n");


printf("Matrix A dimensions:\n");
printf("Enter number of rows: ");
scanf("%d", &r1);
printf("Enter number of columns: ");
scanf("%d", &c1);

printf("\nMatrix B dimensions:\n");
printf("Enter number of rows: ");
scanf("%d", &r2);
printf("Enter number of columns: ");
scanf("%d", &c2);

int A[r1][c1], B[r2][c2], result[r1][c2];

printf("\nMatrix A:\n");
inputMatrix(r1, c1, A);

printf("\nMatrix B:\n");
inputMatrix(r2, c2, B);

if(multiplyMatrices(r1, c1, A, r2, c2, B, result)) {


printf("\n--- RESULT ---");
displayMatrix(r1, c1, A);
printf("×");
displayMatrix(r2, c2, B);
printf("=");
displayMatrix(r1, c2, result);
}
}

// Operation 3: Transpose
void performTranspose() {
int rows, cols;

printf("\n--- MATRIX TRANSPOSE ---\n");


printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);

int mat[rows][cols], trans[cols][rows];

inputMatrix(rows, cols, mat);


transposeMatrix(rows, cols, mat, trans);

printf("\n--- RESULT ---");


printf("\nOriginal Matrix:");
displayMatrix(rows, cols, mat);
printf("\nTranspose Matrix:");
displayMatrix(cols, rows, trans);
}

int main() {
int choice;

do {
printf("\n");
printf("=====================================\n");
printf(" MATRIX OPERATIONS MENU\n");
printf("=====================================\n");
printf("1. Addition of Two Matrices\n");
printf("2. Multiplication of Two Matrices\n");
printf("3. Transpose of Matrix\n");
printf("4. Exit\n");
printf("=====================================\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch(choice) {
case 1:
performAddition();
break;
case 2:
performMultiplication();
break;
case 3:
performTranspose();
break;
case 4:
printf("\nExiting program. Goodbye!\n");
break;
default:
printf("\nInvalid choice! Enter 1-4.\n");
}
} while(choice != 4);

return 0;
}

Common questions

Powered by AI

The code structures user inputs for matrices by first prompting for the number of rows and columns, followed by individual element inputs for each matrix. This structured approach ensures that the dimensions are clearly defined, preventing dimension mismatches, and that all necessary elements are provided for operations. This structured input is crucial for maintaining correct matrix operations since proper input guarantees that subsequent calculations and operations can proceed without errors or omissions .

The code handles an error in matrix multiplication by checking if the number of columns of the first matrix does not equal the number of rows of the second matrix. If this condition is not met, it prints an error message: "Error: Matrix multiplication not possible! Columns of first matrix must equal rows of second matrix." It then returns 0, indicating that the operation was not successful .

In the provided code framework, matrix multiplication can only be performed if the number of columns of the first matrix (A) is equal to the number of rows of the second matrix (B). This condition ensures that the inner dimensions match, which is necessary for matrix multiplication .

The use of dynamically determined matrix dimensions allows the program to handle matrices of varying sizes without needing to specify fixed dimensions at compile time. This flexibility is crucial for applications that require processing matrices of unknown sizes, ensuring that the code can adapt to a wide range of inputs and enhance usability in different scenarios .

Matrix addition is implemented by iterating over each element of the two input matrices and summing their corresponding elements together to form a new matrix (result matrix). The results are displayed by first showing each input matrix and then the resultant matrix after the summation, effectively giving the operation result as a visual output .

The transposition of a matrix in the code is achieved by iterating through each element of the original matrix and assigning its value to the transposed position in a new matrix. Specifically, for each element at position (i, j) in the original matrix, the value is assigned to the position (j, i) in the transposed matrix. This effectively flips the matrix over its diagonal .

To exit the matrix operations program, users must select option 4, labeled as 'Exit', from the main menu. Upon choosing this option, the program provides the feedback message, "Exiting program. Goodbye!", indicating the program termination and successful exit .

User interaction is facilitated through a menu interface in the code that prompts users to choose from three matrix operations: addition, multiplication, and transposition. Users are guided to input the dimensions and elements of the matrices as required for each operation. Feedback and results from operations are displayed to the user, providing an interactive and responsive user experience .

The "displayMatrix" function is responsible for printing matrices to the console in a structured and readable format. It is integrated with other operations such as addition, multiplication, and transposition by displaying the input matrices and resultant matrices after each operation. This function helps verify operations and provides users with clear visual feedback on their inputs and results .

The code includes an error-checking mechanism during matrix multiplication that verifies if the number of columns of the first matrix equals the number of rows of the second matrix before proceeding. This effectively prevents incorrect operations and runtime errors, ensuring appropriate operations based on matrix multiplication rules. Additionally, it provides user feedback in the form of error messages to indicate any discrepancies, which enhances robustness and error handling .

You might also like