# multiplication of two matrices

Page 1 of 1

## Replies To: multiplication of two matrices

### #2 arnabraha1989

Reputation: 3
• Posts: 14
• Joined: 02-August 08

## Re: multiplication of two matrices

Posted 06 September 2008 - 01:42 AM

I have written a program on matrix multiplication using arrays like this
```//program to multiply 2 matrices
#include<stdio.h>
#include<conio.h>
#define max 10
void main()
{
int a[max][max];
int a1[max][max];
int a2[max][max];
int i,j,n,m,k,l,e;
clrscr();
printf("\nEnter the number of rows of first matrix:");//insertion of first matrix
scanf("%d",&n);
printf("\nEnter the number of columns of second matrix:");
scanf("%d",&m);
printf("\nEnter the elements of first matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the number of rows of second matrix:");//insertion of second matrix
scanf("%d",&k);
printf("\nEnter the number of columns of second matrix:");
scanf("%d",&l);
printf("\nEnter the elements of second matrix:\n");
for(i=0;i<k;i++)
for(j=0;j<l;j++)
scanf("%d",&a1[i][j]);
printf("\n First Matrix you entered:\n");//display of first matrix
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d ",a[i][j]);
printf("\n");
}
printf("\n Second Matrix you entered:\n");//display of second matrix
for(i=0;i<k;i++)
{
for(j=0;j<l;j++)
printf("%d ",a1[i][j]);
printf("\n");
}
//Multiplication of these two matrices
for(i=0;i<n;i++)
{
for(j=0;j<k;j++)
{
a2[i][j]=0;
for(e=0;e<k;e++)
{
a2[i][j]=a2[i][j] + (a[i][e] * a1[e][j]);
}
}
}
printf("\n Multiplication of matrix is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<l;j++)
printf("%d ",a2[i][j]);
printf("\n");
}
getch();
}
```

It runs perfectly.
I recently saw a question to write a C program on matrix multiplication using array of pointers.
Whats the difference. Do I have to refer all the elements in the matrix using pointers or what?

### #3 joske

• D.I.C Regular

Reputation: 43
• Posts: 297
• Joined: 04-September 07

## Re: multiplication of two matrices

Posted 06 September 2008 - 11:57 AM

In your current code you have to define the maximum size of the matrices beforehand, with max. When you use pointers, you can create matrices with dynamic size. Your current program will crash when the user enters a number of rows or columns larger than max, and you start filling in the elements...

### #4 Martyr2

• Programming Theoretician

Reputation: 5186
• Posts: 13,914
• Joined: 18-April 07

## Re: multiplication of two matrices

Posted 06 September 2008 - 12:17 PM

Just to jump in here and give you a source that may help you out in the future with this problem you can check out my blog on the topic. I do matrice math with waffles! A humorous twist to a C++ topic.

Martyr2's Programming Underground - Waffle Matrices... Mmmm where is the syrup?

It shows code and everything in C++ for multiplying matrices.

This post has been edited by Martyr2: 06 September 2008 - 12:18 PM

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }