Perform matrix multiplication using array and pointer - C

Q. WAP to perform matrix multiplication using array and pointer.
#BIT_FirstSem_2015_6No_8Marks_Solution
==>Code goes like this:

#include<stdio.h>
int main()
{
    int i,j,k,row1,col1,row2,col2,sum=0;
    int a[10][10],b[10][10],mult[10][10];
   //For First Matrix
    printf("Enter order of first matrix: ");
    scanf("%d %d",&row1,&col1);
    printf("Enter elements of first matrix: ");
    for ( i = 0; i < row1; i++)
    {
        for ( j = 0; j < col1; j++)
        {
            scanf("%d",(*(a+i)+j));
        }
    }
    //For Second Matrix
    printf("\nEnter order of second matrix: ");
    scanf("%d %d",&row2,&col2);
    if(col1!=row2)
    printf("Matrix multiplication not possible");
    else
   {
    printf("Enter elements of second matrix: ");
    for ( i = 0; i < row2; i++)
    {
        for ( j = 0; j < col2; j++)
        {
            scanf("%d",(*(b+i)+j));
        }  
    }
//For multiplication
 for (i = 0; i < row1; i++) {
      for (j = 0; j < col2; j++) {
        for (k = 0; k < row2; k++) {
          sum += *(*(a+i)+k) * *(*(b+k)+j);
        }
        *(*(mult+i)+j) = sum;
        sum = 0;
      }
    }
//Displaying multiplicaton result
    printf("Product of the matrices:\n");

    for (i = 0; i < row1; i++)
    {
      for (j = 0; j < col2; j++)
      {
        printf("%d\t", *(*(mult+i)+j));
      }
      printf("\n");
    }
  }
    return 0;
}

Output:



















If you are using Turbo C++ IDE, then use conio.h in link section after stdio.h
and use getch(); to hold the output screen, just before the end of main function.
If you have any confusion regarding the program, please, comment below.
Happy Coding !
Thank you
Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment