Tuesday, November 10, 2020

Menu Driven C Program to Matrix Manipulation

Write a menu driven program in C for the following:
a) A function to input data to a Matrix A and B of length 4 by 4.
b) A function to display the addition of A and B.
c) A function to display the multiplication of A and B.

Answer

 #include <stdio.h>

#define ROWS 4
#define COLS 4
void  arrInput(int mat[][COLS]);
void  arrPrint(int mat[][COLS]);
void matrixAdd(int mat1[][COLS], int mat2[][COLS], int res[][COLS]);
void matrixMul(int mat1[][COLS], int mat2[][COLS], int res[][COLS]);

int main() {
    char T,Sel;
    int M1[ROWS][COLS];
    int M2[ROWS][COLS];
    int M1PM2[ROWS][COLS];
    int M1MM2[ROWS][COLS];
    T = 1;
    //menu Start
    do
    {

        printf("%s", "Enter 1 for Add Elements to Matrix A and B\n");
        printf("%s", "Enter 2 for Print Matrix A and B\n");
        printf("%s", "Enter 3 for Add Matrix A and B and Print new Matrix\n");
        printf("%s", "Enter 4 for Multiply Matrix A and B and Print new Matrix\n");
        printf("%s", "Enter 5 Exit\n");
        printf("%s", "Enter a Select : 1 - 5 ?");
        scanf_s("%c", &Sel);
        switch (Sel)
        {
        case '1':
            printf("%s\n", "Enter Elements for Matrix A");
            arrInput(M1);
            printf("%s\n", "Enter Elements for Matrix B");
            arrInput(M2);
            break;
        case '2':
            printf("%s\n", "Matrix A");
            arrPrint(M1);
            printf("%c", "\n");
            printf("%s\n", "Matrix B");
            arrPrint(M2);
            break;
        case '3': 
            matrixAdd(M1, M2, M1PM2);
            printf("%s\n", "After Add  A and B");
            arrPrint(M1PM2);
            break;
        case '4':
            matrixMul(M1, M2, M1MM2);
            printf("%s\n", "After Multiplied  A and B");
            arrPrint(M1MM2);
            break;
        case '5':
            T = 0;
            break;
        default:
            printf("%s", "Invalid Selection Enter again!...");
            continue;

        }
    }while (T == 1);
    //menu end
}

//function to input matrix data
void  arrInput(int mat[][COLS])
{
    int i, j;
    for (i = 0; i < 4; i++) {
        for (j = 0;j < 4;j++) {
            printf("Enter matrix value for arr[%d][%d]:", i + 1, j + 1);
            scanf_s("%d", &mat[i][j]);
        }
    }   
}

//function to print Matrix
void  arrPrint(int mat[][COLS])
{
    int i, j;
    printf("Print Matrix elements:\n");
    for (i = 0; i < 4; i++) {
        for (j = 0;j < 4;j++) {
            printf("%d\t ", mat[i][j]);
        }
        printf("\n");

    }
}

//function to add two matrices
void matrixAdd(int mat1[][COLS], int mat2[][COLS], int res[][COLS])
{
    int i, j;
    for (i = 0; i < ROWS; i++)
    {
        for (j = 0; j < COLS; j++)
        {
            res[i][j] = mat1[i][j] + mat2[i][j];
        }
    }
}

//function to multiply matrices
void matrixMul(int mat1[][COLS], int mat2[][COLS], int res[][COLS])
{
    int i, j, k;
    printf("multiply of the matrix=\n");
    for (i = 0;i < ROWS;i++)
    {
        for (j = 0;j < COLS;j++)
        {
            res[i][j] = 0;
            for (k = 0;k < COLS;k++)
            {
                res[i][j] += mat1[i][k] * mat2[k][j];
            }
        }
    }
}

No comments:

Post a Comment