OiO.lk Blog C++ Matrix multiplication code runs without error but gives no output
C++

Matrix multiplication code runs without error but gives no output


This code for matrix multiplication asks the user for input and everything but doesn’t give any output. Can anyone please help?

#include <stdio.h>

int i, j, k, l = 0;
int r1, r2, c1, c2;

void matMul(int m1[r1][c1], int m2[r2][c2], int res[r1][c2]){
    for (int a = 0; a < r1; a++){
        for (int  b = 0; b < c2; b++){
            res[a][b] = 0;
        }
    }

    while (i < r1) { 
        if (j == c1) {
            j,k = 0;
            l++;
        }
        else if (l == c2) {
            l = 0;
            i++;
        }
        else if (i>r1){
            break;
        }
        res[i][l] += m1[i][j]*m2[k][l];
        j++;
        k++;
    }
    printf("Resultant Matrix:\n");
    for (i = 0; i < r1; i++){
        for (j = 0; j < c2; j++){
            printf("%d\t", res[i][j]);
        }
        printf("\n");
    }
}

void main(){
    printf("Enter the number of rows for first matrix: ");
    scanf("%d", &r1);
    printf("Enter the number of columns for first matrix: ");
    scanf("%d", &c1);
    printf("Enter the elements of first matrix:\n");
    int m1[r1][c1];
    for (int a = 0; a < r1; a++){
        for (int b = 0; b < c1; b++){
            scanf("%d", &m1[a][b]);
        }
    }

    printf("Enter the number of rows for second matrix: ");
    scanf("%d", &r2);
    printf("Enter the number of columns for second matrix: ");
    scanf("%d", &c2);
    if (c1!=r2) {
        printf("Matrices can't be multiplied with each other.\n");
        return;
    }
    printf("Enter the elements of second matrix:\n");
    int m2[r2][c2];
    for (int a = 0; a < r2; a++){
        for (int b = 0; b < c2; b++){
            scanf("%d", &m2[i][j]);
        }
    }

    int res[r1][c2];
    matMul(m1, m2, res);
}



You need to sign in to view this answers

Exit mobile version