CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2022
    Posts
    1

    How do I find the product of two rectangular matrices in C Matrix Multiplication?

    Wanted to understand how this works, need some help on this.

  2. #2
    Join Date
    Sep 2021
    Location
    India
    Posts
    5

    Resolved Re: How do I find the product of two rectangular matrices in C Matrix Multiplication?

    Quote Originally Posted by Soham1087 View Post
    Wanted to understand how this works, need some help on this.
    Here is one example program:

    Code:
    #include<stdio.h>
    int main(){
        int m, n, p, q, i, j, k;
        int a[10][10], b[10][10], res[10][10];
        
        printf("Enter the order of first matrix\n");
        scanf("%d%d", &m, &n);
        printf("Enter the order of second matrix\n");
        scanf("%d%d", &p, &q);
        
        if(n!=p){
            printf("Matrix is incompatible for multiplication\n");
        }
        else{
            printf("Enter the elements of Matrix-A:\n");
            for(i=0;i<m;i++){
                for(j=0;j<n;j++){
                    scanf("%d",&a[i][j]);
                }
            }
            
            printf("Enter the elements of Matrix-B:\n");
            for(i=0;i<p;i++){
                for(j=0;j<q;j++){
                    scanf("%d",&b[i][j]);
                }
            }
            
            for(i=0;i<m;i++){
                for(j=0;j<q;j++){
                    res[i][j]=0;
                    for(k=0;k<p;k++){
                        res[i][j]+=a[i][k]*b[k][j];
                    }
                }  
            }
            
            printf("The product of the two matrices is:-\n");
            
            for(i=0;i<m;i++){
                for(j=0;j<q;j++){
                    printf("%d\t", res[i][j]);
                }
                printf("\n");
            }
        }
        
        return 0;
    }
    Read this article on Scaler Topics, it will clear your doubts. Thanks me later!
    Last edited by 2kaud; May 30th, 2022 at 11:22 AM. Reason: Changed tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured