Wednesday, March 28, 2012

Numerical Methods: Multiplication of two matrices using two dimensional array in C/C++

Source Code


int main()
    int r1, c1, r2, c2, matrix1[10][10], matrix2[10][10], result[10][10];
    int i, j, k;
    printf("Enter the row and column of the first matrix: ");
    printf("Enter the row and column of the second matrix: ");
    if(c1 != r2){
        printf("Matrix multiplication impossible");
    printf("Enter the first matrix: \n");
    for(i = 0; i <r1; i++)
       for(j = 0; j < c1; j++)
            scanf("%d", &matrix1[i][j]);
    printf("Enter the second matrix: \n");
    for(i = 0; i <r2; i++)
       for(j = 0; j < c2; j++)
            scanf("%d", &matrix2[i][j]);
    for(i = 0; i < r1; i++ ){
        for(j = 0; j < c2; j++){
            result[i][j] = 0;
            for(k = 0; k < c1; k++){
                result[i][j] += matrix1[i][k] * matrix2[k][j];
    printf("The multiplication of the matrix is: \n");
    for(i = 0; i < r1; i++){
        for(j = 0; j < c2; j++){
            printf("%d", result[i][j]);
            printf(" ");
    return 0;


  1. this is wrong.... the multiplication logic is not right....

  2. Shayan thanks for reply ... but i don't think the logic is wrong. Can you prove it?

  3. I think your source code in c/c++ for two Matrix multiplication is correct and not wrong, which Mr Shayan mean, thank you for very nice and in simply way to write your programm about numerical Mathematic, i hope more in analytical Geometrie like point,victor,line,plan in c/c++, my compiler is dev c++ from blood sheet

  4. Bibek, your code for Marixmultiplication is korekt and gut, may be mohr in analytical Geometry source code in c/c++