Cover Image for Matrix Calculator in C
111 views

Matrix Calculator in C

Creating a matrix calculator in C involves implementing various matrix operations such as addition, subtraction, multiplication, and transposition. Below is a simplified example of a matrix calculator program in C that performs these basic operations. This program uses 2D arrays to represent matrices and provides a menu-driven interface for performing operations on them.

C
#include <stdio.h>

// Function to input a matrix
void inputMatrix(int matrix[][100], int rows, int cols) {
    printf("Enter matrix elements:\n");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
}

// Function to display a matrix
void displayMatrix(int matrix[][100], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d\t", matrix[i][j]);
        }
        printf("\n");
    }
}

// Function to add two matrices
void addMatrices(int matrix1[][100], int matrix2[][100], int result[][100], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            result[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }
}

// Function to subtract two matrices
void subtractMatrices(int matrix1[][100], int matrix2[][100], int result[][100], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            result[i][j] = matrix1[i][j] - matrix2[i][j];
        }
    }
}

// Function to multiply two matrices
void multiplyMatrices(int matrix1[][100], int rows1, int cols1, int matrix2[][100], int rows2, int cols2, int result[][100]) {
    if (cols1 != rows2) {
        printf("Matrix multiplication is not possible. Number of columns in the first matrix must be equal to the number of rows in the second matrix.\n");
        return;
    }

    for (int i = 0; i < rows1; i++) {
        for (int j = 0; j < cols2; j++) {
            result[i][j] = 0;
            for (int k = 0; k < cols1; k++) {
                result[i][j] += matrix1[i][k] * matrix2[k][j];
            }
        }
    }
}

// Function to transpose a matrix
void transposeMatrix(int matrix[][100], int rows, int cols, int result[][100]) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            result[j][i] = matrix[i][j];
        }
    }
}

int main() {
    int choice;
    int rows, cols;
    int matrix1[100][100], matrix2[100][100], result[100][100];

    printf("Matrix Calculator\n");

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

    printf("Enter matrix 1:\n");
    inputMatrix(matrix1, rows, cols);

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

    printf("Enter matrix 2:\n");
    inputMatrix(matrix2, rows, cols);

    while (1) {
        printf("\nMatrix Operations Menu:\n");
        printf("1. Add Matrices\n");
        printf("2. Subtract Matrices\n");
        printf("3. Multiply Matrices\n");
        printf("4. Transpose Matrix\n");
        printf("5. Display Result\n");
        printf("6. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addMatrices(matrix1, matrix2, result, rows, cols);
                printf("Matrix addition result:\n");
                displayMatrix(result, rows, cols);
                break;
            case 2:
                subtractMatrices(matrix1, matrix2, result, rows, cols);
                printf("Matrix subtraction result:\n");
                displayMatrix(result, rows, cols);
                break;
            case 3:
                multiplyMatrices(matrix1, rows, cols, matrix2, rows, cols, result);
                printf("Matrix multiplication result:\n");
                displayMatrix(result, rows, cols);
                break;
            case 4:
                transposeMatrix(matrix1, rows, cols, result);
                printf("Transpose of matrix 1:\n");
                displayMatrix(result, cols, rows);
                break;
            case 5:
                printf("Matrix 1:\n");
                displayMatrix(matrix1, rows, cols);
                printf("Matrix 2:\n");
                displayMatrix(matrix2, rows, cols);
                break;
            case 6:
                exit(0);
            default:
                printf("Invalid choice. Please try again.\n");
        }
    }

    return 0;
}

This program allows you to input two matrices, perform addition, subtraction, multiplication, transposition, and display the result. You can expand this program by adding error handling, dynamic memory allocation, and more matrix operations as needed.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS