# matrix multiplication using array of pointers

Page 1 of 1

## 4 Replies - 55398 Views - Last Post: 23 November 2009 - 11:35 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=63630&amp;s=7b9950b754cc72345aac279b5919fc89&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 arnabraha1989

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

# matrix multiplication using array of pointers

Posted 13 September 2008 - 03:34 AM

I have written a program for multiplication of two matrices using array of pointers like this
```#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void main()
{
int row_size1,col_size1,row_size2,col_size2;
int **A,**B,**C;
int i,j,e;
printf("Enter the number of rows and columns in the first matrix:");
scanf("%d %d",&row_size1,&col_size1);
printf("Enter the number of rows and columns in the second matrix:");
scanf("%d %d",&row_size2,&col_size2);
A=(int**)malloc(row_size1*sizeof(int*));
for(i=0;i<col_size1;i++)
{
A[i]=(int*)malloc(col_size1*sizeof(int));
}
B=(int**)malloc(row_size2*sizeof(int*));
for(i=0;i<col_size2;i++)
{
B[i]=(int*)malloc(col_size2*sizeof(int));
}
C=(int**)malloc(row_size1*sizeof(int*));
for(i=0;i<col_size2;i++)
{
C[i]=(int*)malloc(col_size2*sizeof(int));
}
if(col_size1!=row_size2)
{
printf("The matrices are not conformable ");
}
else
{
printf("Enter the elements of the first matrix:");
for(i=0;i<row_size1;i++)
for(j=0;j<col_size1;j++)
scanf("%d",&A[i][j]);
printf("Enter the elements of the second matrix:");
for(i=0;i<row_size2;i++)
for(j=0;j<col_size2;j++)
scanf("%d",&B[i][j]);
for(i=0;i<row_size1;i++)
{
for(j=0;i<col_size2;j++)
{
C[i][j]=0;
for(e=0;e<row_size2;e++)
{
C[i][j]=C[i][j]+(A[i][e]*B[e][j]);
}
}
}
printf("The required matrix is");
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size2;j++)
printf("%d ",C[i][j]);
printf("\n");
}
}

```

Its giving a error message that THERE IS A PROCESSOR FAULT. what is the reason?

Is This A Good Question/Topic? 1

## Replies To: matrix multiplication using array of pointers

Reputation: 1
• Posts: 8
• Joined: 06-September 08

## Re: matrix multiplication using array of pointers

Posted 13 September 2008 - 04:42 AM

arnabraha1989, on 13 Sep, 2008 - 03:34 AM, said:

I have written a program for multiplication of two matrices using array of pointers like this
```#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void main()
{
int row_size1,col_size1,row_size2,col_size2;
int **A,**B,**C;
int i,j,e;
printf("Enter the number of rows and columns in the first matrix:");
scanf("%d %d",&row_size1,&col_size1);
printf("Enter the number of rows and columns in the second matrix:");
scanf("%d %d",&row_size2,&col_size2);
A=(int**)malloc(row_size1*sizeof(int*));
for(i=0;i<col_size1;i++)
{
A[i]=(int*)malloc(col_size1*sizeof(int));
}
B=(int**)malloc(row_size2*sizeof(int*));
for(i=0;i<col_size2;i++)
{
B[i]=(int*)malloc(col_size2*sizeof(int));
}
C=(int**)malloc(row_size1*sizeof(int*));
for(i=0;i<col_size2;i++)
{
C[i]=(int*)malloc(col_size2*sizeof(int));
}
if(col_size1!=row_size2)
{
printf("The matrices are not conformable ");
}
else
{
printf("Enter the elements of the first matrix:");
for(i=0;i<row_size1;i++)
for(j=0;j<col_size1;j++)
scanf("%d",&A[i][j]);
printf("Enter the elements of the second matrix:");
for(i=0;i<row_size2;i++)
for(j=0;j<col_size2;j++)
scanf("%d",&B[i][j]);
for(i=0;i<row_size1;i++)
{
for(j=0;i<col_size2;j++)
{
C[i][j]=0;
for(e=0;e<row_size2;e++)
{
C[i][j]=C[i][j]+(A[i][e]*B[e][j]);
}
}
}
printf("The required matrix is");
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size2;j++)
printf("%d ",C[i][j]);
printf("\n");
}
}

```

Its giving a error message that THERE IS A PROCESSOR FAULT. what is the reason?

HI
free the memory using FREE function of A,B and C Matrixs

This post has been edited by rambabuy: 13 September 2008 - 04:44 AM

### #3 perfectly.insane

Reputation: 70
• Posts: 644
• Joined: 22-March 08

## Re: matrix multiplication using array of pointers

Posted 13 September 2008 - 10:11 PM

Not freeing the memory will cause a memory leak, but it shouldn't cause the program to crash. I think the real problem is with the for loops that allocate the memory.

```A=(int**)malloc(row_size1*sizeof(int*));
for(i=0;i<col_size1;i++)
{
A[i]=(int*)malloc(col_size1*sizeof(int));
}

```

The loop should terminate when i is greater than row_size1, not col_size1. The other loops below that suffer from the same problem.

### #4 ifi

Reputation: 1
• Posts: 3
• Joined: 23-November 09

## Re: matrix multiplication using array of pointers

Posted 23 November 2009 - 11:30 PM

i also run this program just now and it also gives error but i removed this error,so plz you put a parathesis at the end of program and then execute it,i will hope that it must work

### #5 ifi

Reputation: 1
• Posts: 3
• Joined: 23-November 09

## Re: matrix multiplication using array of pointers

Posted 23 November 2009 - 11:35 PM

rambabuy, on 13 Sep, 2008 - 03:42 AM, said:

arnabraha1989, on 13 Sep, 2008 - 03:34 AM, said:

I have written a program for multiplication of two matrices using array of pointers like this
```#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void main()
{
int row_size1,col_size1,row_size2,col_size2;
int **A,**B,**C;
int i,j,e;
printf("Enter the number of rows and columns in the first matrix:");
scanf("%d %d",&row_size1,&col_size1);
printf("Enter the number of rows and columns in the second matrix:");
scanf("%d %d",&row_size2,&col_size2);
A=(int**)malloc(row_size1*sizeof(int*));
for(i=0;i<col_size1;i++)
{
A[i]=(int*)malloc(col_size1*sizeof(int));
}
B=(int**)malloc(row_size2*sizeof(int*));
for(i=0;i<col_size2;i++)
{
B[i]=(int*)malloc(col_size2*sizeof(int));
}
C=(int**)malloc(row_size1*sizeof(int*));
for(i=0;i<col_size2;i++)
{
C[i]=(int*)malloc(col_size2*sizeof(int));
}
if(col_size1!=row_size2)
{
printf("The matrices are not conformable ");
}
else
{
printf("Enter the elements of the first matrix:");
for(i=0;i<row_size1;i++)
for(j=0;j<col_size1;j++)
scanf("%d",&A[i][j]);
printf("Enter the elements of the second matrix:");
for(i=0;i<row_size2;i++)
for(j=0;j<col_size2;j++)
scanf("%d",&B[i][j]);
for(i=0;i<row_size1;i++)
{
for(j=0;i<col_size2;j++)
{
C[i][j]=0;
for(e=0;e<row_size2;e++)
{
C[i][j]=C[i][j]+(A[i][e]*B[e][j]);
}
}
}
printf("The required matrix is");
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size2;j++)
printf("%d ",C[i][j]);
printf("\n");
}
}

```

Its giving a error message that THERE IS A PROCESSOR FAULT. what is the reason?

HI
free the memory using FREE function of A,B and C Matrixs