Skip to main content

Implement Matrix multiplication and validate the rules of multiplication Program and Algorithm Computer Science Engineering VTU

Matrix Multiplication

#include<stdio.h>

#include<stdlib.h>

void main()

{

int a[10] [10],b[10] [10],c[10] [10],i,j,k,m,n,p,q;

printf("enter the order of the matrix A\n");

scanf("%d%d",&m,&n);

printf("enter the order of the matrix B\n");

scanf("%d%d",&p,&q);

if(n!=p)

{

    printf("matrix multiplication is not possible\n");

    exit(0);

}

printf("enter the elements of A\n");

for(i=0;i<m;i++)

{

    for(j=0;j<n;j++)

    {

        scanf("%d",&a[i][j]);

    }

}

printf("enter the elements of 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++)

    {

        c[i][j]=0;

        for(k=0;k<n;k++)

        {

            c[i][j]+=a[i][k]*b[k][j];

        }

        }

    }

    printf("matrix A is\n");

    for(i=0;i<m;i++)

    {

        for(j=0;j<p;j++)

        {

            printf("%d\t",a[i][j]);

        }

        printf("\n");

    }

    printf("matrix B is\n");

    for(i=0;i<p;i++)

    {

        for(j=0;j<q;j++)

        {

            printf("%d\t",b[i][j]);

        }

        printf("\n");

    }

        

    printf("the product of two matrix is\n");

    for(i=0;i<m;i++)

    {

        for(j=0;j<q;j++)

        {

            printf("%d\t",c[i][j]);

        }

        printf("\n");

    }

    }

Matrix Multiplication algorithm
Start

Read order of A matrix (m x n) and order of B Matrix (p x q).

Check if n is equal to p, if equal goto next step else print error and stop.

Read the A matrix (m x n) elements

Read the B matrix (p x q) elements

for i: 0 to m-1 in steps of 1 do

for j: 0 to q-1 in steps of 1 do

c[i][j] = 0

for k: 0 to n-1 in steps of 1 do

c[i][j] = c[i][j] + a[i][k]*b[k][j]

Display matrix A, B, C

Stop

Comments

Popular posts from this blog

Raju is a Civil Engineer. He is writing software to automate his work. As a part of his requirement, he wants to calculate the circle diameter, circumference, and area. Help Raju to complete his task. Get radius as input.

OUTPUT

Write functions to implement string operations such as compare, concatenate, string length. Convince the parameter passing techniques Program and Algorithm Computer Science Engineering VTU

STRINGconcatenation #include<stdio.h> #include<stdlib.h> int length (char str[]); int compare (char str1[],char str2[]); void concatenate (char str1[],char str2[]); void main() { char str1[30],str2[30]; int choice,a,i,j; printf("enter 1-string comparision\n"); printf("enter 2-string length\n"); printf("enter 3-string concatenation\n"); printf("enter 4-exit\n"); scanf("%d",&choice); switch(choice) { case 1:printf("enter string 1\n"); scanf("%s",str1); printf("enter string 2\n"); scanf("%s",str2); a=compare(str1,str2); if(a==0) { printf("%s and%s are identical\n",str1,str2); } else { printf("%s and%s are not identical\n",str1,str2); } break; ...