# problem in matrix multiplication

i have to multiply 2 n*m matrices using pointers and the most optimal solution is expected.

## Replies

• sahana
i have to multiply 2 n*m matrices using pointers
When you say pointers, do you meant that the matrices are implemented using linked list? Could you please provide more details on how the matrices are implemented.

• sahana
yeah u can implement it by a linked list.
• friendster7
if u need to multiply then check out this..this might help

``` #include
#include

main()
{
int mtrx1,mtrx2,product,ro1,ro2,col1,col2;
unsigned short int posblty;
static unsigned short int call_main = 0;
if(call_main == 0 || call_main % 10 == 0)
{
clrscr();
printf("\nThis program will calculate the product of two matrices");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
call_main++;
getnum(&ro1,1);
getnum(&col1,3);
getnum(&ro2,2);
getnum(&col2,4);
posblty = is_posble(col1,ro2);
if(!posblty)
{
getch();
repeat();
}
getmatrix(mtrx1,ro1,col1,1);
getmatrix(mtrx2,ro2,col2,2);
multiply(mtrx1,mtrx2,product,ro1,col2,col1);
print_matrix(product,ro1,col2);
find_trace(product,ro1,col2);
repeat();
}

getnum(num,message)
int *num,message;
{
do
{
switch(message)
{
case 1:
printf("\nEnter the number of rows in matrix 1:");
break;

case 2:
printf("\nEnter the number of rows in matrix 2:");
break;

case 3:
printf("\nEnter the number of columns in matrix 1:");
break;

case 4:
printf("\nEnter the number of columns in matrix 2:");
break;

default:
printf("\nEnter a number:");
}
scanf("%d",num);
}
while(*num > 5);
return;
}

is_posble(col_1,row_2)
int row_2,col_1;
{
if(col_1 != row_2)
{
printf("\nThe multiplication of given matrices is not possible");
return(0);
}
else
{
return(1);
}
}

getmatrix(array,row,column,mtrx_num)
int *array,row,column,mtrx_num;
{
int i,j;
printf("\nEnter matrix %d\n",mtrx_num);

for(i = 0;i < row;i++)
{
for(j = 0;j < column;j++)
{
scanf("%d",array+(5*i+j));
}
}
return;
}

multiply(matrix1,matrix2,product,l,m,n)
int *matrix1,*matrix2,*product,l,m,n;
{
int i,j,k,temp1,temp2,temp3;

for(i=0;i < l;i++)
{
for(j = 0;j < m;j++)
{
*(product+(5*i+j)) = 0;
for(k = 0;k < n;k++)
{
temp1 = *(matrix1+(5*i+k));
temp2 = *(matrix2+(5*k+j));
temp3 = *(product+(5*i+j));
*(product+(5*i+j)) = temp1 * temp2 + temp3;
}
}
}
return;
}

print_matrix(array,row,col)
int *array;
{
int i,j;
printf("\nThe product is :");
for(i = 0;i < row;i++)
{
printf("\n");
for(j = 0;j < col;j++)
{
printf("%5d \t",*(array+(5*i+j)));
}
}
return;
}

```
• sahana
hi friendster7,
i got your code.but is the most optimal?this code is designed for matrices which have a maximum of 5 rows and 5 columns right?so if i dont use a 5*5 matrix (say 3*3 ), its going to be a waste of space right?can we make it a little more optimal.how about dyanamic memory allocation?
please tell me if i have undserstood it wrongly.
another constraint i forgot to add in the question is that we cannot get the size of the matrix during runtime and then assign the subscript of the array.
ie something like
scanf("%d,%d",&rows,&col);
int array[row][col];

otherwise your code was really good.

You are reading an archived discussion.

## Want to study Quantum computing - help!

Can anyone plz direct me what to concentrate on if i wanna take up quantum physics as a foundation for quantum computing. I love physics n this IT stream has...

## Decode This! Hint: Opening lines of a famous book

Okay, The opening lines of a famous book have been decoded here. Use your logic and stay away from the Google. Tell me what's written below - B dj blrtquctek...

## Let's crack the MindQuiz!

I found an interesting site over here. I guess those of us who have all the free time, can crack this puzzle together. There are multiple levels. So let's get...

## Electronics & Communications Engineers Thread.

I failed to find a special forum for ECE members. So,i am starting a one. Lets start the THINGS.

## accustic heat pump

acoustic heat pump I just came across some brief information on an acoustic resonating heat pump that uses no oil/cfc but sound wave pressure differential to operate the heat pump....