Sunday, December 2, 2018

C code review shows to multiply two 3*3 matrices

This C code shows to multiply two 3*3 matrices. And code is helpful while user is in between any matrices manipulation task.

code:

//Tested OK on Code::Blocks IDE 17.12 & Cygwin+GCC tools.

#include<stdio.h>
//#include<conio.h>

/*Program to multiply two 3*3 matrices*/

void main()
{
    int a[3][3],b[3][3],c[3][3],i,j,k;
    //clrscr();

    printf("\nEnter elements of A: \n");
    for(i=0; i<=2; i++)
        for(j=0; j<=2; j++)
            scanf("%d",&a[i][j]);

    printf("\nEnter elements of B: \n");
    for(i=0; i<=2; i++)
        for(j=0; j<=2; j++)
            scanf("%d",&b[i][j]);

    printf("\nA:");
    for(i=0; i<=2; i++)
    {
        for(j=0; j<=2; j++)
            printf("%d    ",a[i][j]);
        printf(""); //To change line.
    }

    printf("\nB:");
    for(i=0; i<=2; i++)
    {
        for(j=0; j<=2; j++)
            printf("%d    ",b[i][j]);
        printf(" ");
    }
    k=0;
    while(k<=2)
    {
        for(i=0; i<=2; i++)
        {
            int sum=0;
            for(j=0; j<=2; j++)
                sum=sum+a[i][j]*b[j][k];
            c[i][k]=sum;
        }
        k++;
    }
    printf("\nResult:");
    for(i=0; i<=2; i++)
    {
        for(j=0; j<=2; j++)
            printf("%d    ",c[i][j]);
        printf("  ");
    }
    getchar();
}

//end of program

To see such C code reviews refer blog archieve at right side or click CCode lable for till posted posts list.

...till next post, bye-bye and take care.

No comments:

Post a Comment