4 Replies - 50001 Views - Last Post: 23 November 2009 - 11:35 PM Rate Topic: -----

#1 arnabraha1989  Icon User is offline

  • New D.I.C Head

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

matrix multiplication using array of pointers

Post icon  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

#2 rambabuy  Icon User is offline

  • New D.I.C Head

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

Re: matrix multiplication using array of pointers

Posted 13 September 2008 - 04:42 AM

View Postarnabraha1989, 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

Was This Post Helpful? 0
  • +
  • -

#3 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 ifi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 ifi  Icon User is offline

  • New D.I.C Head

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

Re: matrix multiplication using array of pointers

Posted 23 November 2009 - 11:35 PM

View Postrambabuy, on 13 Sep, 2008 - 03:42 AM, said:

View Postarnabraha1989, 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

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1