๐Ÿ’ป
B.TECH I YEAR โ€ข I SEMESTER โ€ข R25

Programming for Problem Solving - Lab Manual

C Programming Laboratory โ€ข Program Solution

๐Ÿ“˜ R25 Regulation ๐ŸŽ“ I Year ๐Ÿ“š I Semester ๐Ÿ’ป C Programming ๐Ÿงช Lab Program
Selected Program

Write a C program that uses functions to perform Multiplication of Two Matrices

โ† All Lab Programs

Write a C program that uses functions to perform Multiplication of Two Matrices

Algorithm

start
    step1: print "enter rows and columns of matrix A"
    step2: read rowA, colA

    step3: print "enter rows and columns of matrix B"
    step4: read rowB, colB

    step5: if colA != rowB then
    step6:     print "matrix multiplication is not possible"
    step7:     stop
   
    step8: call inputmatrix(A, rowA, colA)
    step9: call inputmatrix(B, rowB, colB)

    step10: call multiplymatrices(A, B, result, rowA, colA, rowB, colB)

    step11: print "matrix A"
    step12: call displaymatrix(A, rowa, colA)

    step13: print "matrix B"
    step14: call displaymatrix(B, rowB, colB)

    step15: print "resultant matrix (A * B)"
    step16: call displaymatrix(result, rowA, colB)

stop


function inputmatrix(matrix, rows, cols)
    for i = 0 to rows - 1
        for j = 0 to cols - 1
            read matrix[i][j]
end function


function multiplymatrices(a, b, result, rowa, cola, rowb, colb)
    for i = 0 to rowa - 1
        for j = 0 to colb - 1
            result[i][j] = 0
            for k = 0 to cola - 1
                result[i][j] = result[i][j] + a[i][k] * b[k][j]
end function


function displaymatrix(matrix, rows, cols)
    for i = 0 to rows - 1
        for j = 0 to cols - 1
            print matrix[i][j]
        print newline
end function



Flowchart
flowchart for Multiplication of Two Matrices in c

C Programming
// Multiplication of Two Matrices 

#include <stdio.h>

// Function to perform matrix multiplication
void multiplyMatrices(int A[10][10], int B[10][10], int result[10][10], int rowA, int colA, int rowB, int colB) {
    int i,j,k;
    for (i = 0; i < rowA; i++) {
        for (j = 0; j < colB; j++) {
            result[i][j] = 0;		// Initializing the result matrix with 0
            for (k = 0; k < colA; k++) {
                result[i][j] += A[i][k] * B[k][j];
            }
        }
    }
}

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

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

int main() {
    int A[10][10], B[10][10], result[10][10];
    int rowA, colA, rowB, colB;

    // Input matrix dimensions
    printf("Enter the number of rows and columns for Matrix A: ");
    scanf("%d %d", &rowA, &colA);

    printf("Enter the number of rows and columns for Matrix B: ");
    scanf("%d %d", &rowB, &colB);

    // Check if multiplication is possible (columns of A must be equal to rows of B)
    if (colA != rowB) {
        printf("Matrix multiplication is not possible. The number of columns of A must be equal to the number of rows of B.\n");
        return 1; // Exit the program if multiplication is not possible
    }

    // Input matrices A and B
    printf("Input matrix A:\n");
    inputMatrix(A, rowA, colA);

    printf("Input matrix B:\n");
    inputMatrix(B, rowB, colB);

    // Perform matrix multiplication
    multiplyMatrices(A, B, result, rowA, colA, rowB, colB);

    // Display the result
    printf("\nMatrix A:\n");
    displayMatrix(A, rowA, colA);

    printf("\nMatrix B:\n");
    displayMatrix(B, rowB, colB);

    printf("\nResultant Matrix (A * B):\n");
    displayMatrix(result, rowA, colB);

    return 0;
}

OUTPUT
Enter the number of rows and columns for Matrix A: 3 3
Enter the number of rows and columns for Matrix B: 3 3
Input matrix A:
Enter elements of the matrix (3x3):
1 2 3 
4 5 6
7 8 9
Input matrix B:
Enter elements of the matrix (3x3):
1 0 0
0 1 0
0 0 1

Matrix A:
 1  2  3 
 4  5  6 
 7  8  9 

Matrix B:
 1  0  0 
 0  1  0 
 0  0  1 

Resultant Matrix (A * B):
 1  2  3 
 4  5  6 
 7  8  9 

๐Ÿ’ป Run This C Program

Write a C program that uses functions to perform Multiplication of Two Matrices
The selected C program is loaded automatically. Edit the code, provide input through STDIN and click Run.

The compiler is embedded using OneCompiler. Your program runs in the embedded C environment; Study Glance does not execute C code directly on your web server.
โ† Back to PPS Lab Programs ๐Ÿ“š All Lab Manuals