Here is the program to find the product of the two integer matrices. The program asks for the order A (M, N) of the first matrix, and that B (P, Q) of the second matrix. Soon after entering the order, the cursor takes to the proper position in the screen to input the matrices. User need to hit ‘Enter’ button after each entry.
Logic : To multiply two matrices, the condition should be satisfy that the column count of the first matrix A should equal the column count of the second matrix B. (The program will check for the validity of this condition i.e. if, N = P. If fails, outputs an error message.) The first row of A is multiplied with with the first column of B. i.e. the first member of the first row of A is multiplied with the first member of first column of B, stored in a variable sum, second with the second and the product is added up, similarly, the procedure grows till the end, and sum is updated in each multiply, and the sum will be the first member of the product matrix C. The procedure grows till the end. Finally we will get the product matrix C of order (M, Q).
The procedure can be modified slightly with the same algorithm, to add, subtract, transpose or others like, to trace the diagonals etc.
#include<stdio.h>
void main()
{
int A[5][5],B[5][5],C[5][5],i,j,m,n,p,q,k;
clrscr();
printf(“nnt ENTER A ORDER OF THE FIRST MATRIX M,N…: “);
scanf(“%d,%d”,&m,&n);
printf(“nnt ENTER A ORDER OF THE SECOND MATRIX P,Q…: “);
scanf(“%d,%d”,&p,&q);
if(n == p)
{
printf(“nnt ENTER THE ELEMENTS OF THE FIRST MATRIX..:nn”);
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
gotoxy(25+j*4,14+i*2);
scanf(“%d”,&A[i][j]);
}
printf(“n”);
}
printf(“nt ENTER THE ELEMENTS OF THE SECOND MATRIX..:nn”);
for(i=1;i<=p;i++)
{
for(j=1;j<=q;j++)
{
gotoxy(25+j*4,20+m+i*2);
scanf(“%d”,&B[i][j]);
}
printf(“n”);
}
for(i=1;i<=m;i++)
for(j=1;j<=q;j++)
{
C[i][j] = 0;
for(k=1;k<=n;k++)
C[i][j] = C[i][j] + (A[i][k] * B[k][j]);
}
printf(“nt THE PRODUCT OF TWO MATRICES IS…:nnttt “);
for(i=1;i<=m;i++)
{
for(j=1;j<=q;j++)
printf(” %d”,C[i][j]);
printf(“nnttt “);
}
}
else
{
printf(“nt THE ORDERS OF TWO MATRICES ARE NOT CORRECT”);
printf(“nnt HELP : ‘N’ SHOULD BE EQUAL TO ‘P’”);
}
getch();
}
Download exe and source code here.
Description :
This is the one stop educational site for all Electronic and Computer students. If you want to learn something new then we are here to help. We work on Microcontroller projects, Basic Electronics, Digital electronics, Computer projects and also in basic c/c++ programs.
#Home #Sitemap #Submit #Terms of Use
Copyright©2011 electrofriends.com All Rights Reserved
Contact:info@electrofriends.com | Powered by Dhyeya
June 23rd, 2011 at 2:19 pm
I DON’T NO THIS “gotoxy” COMMAND.CAN ANyone explain me. email id:dhananjayancheet@live.com