Google Search

Wednesday, October 27, 2010

Write a C program using dynamic memory allocation to copy one matrix to another matrix

Write a C program using dynamic memory allocation to copy one matrix to another matrix

#include
void main()
{

int **a,**b,**c;

int a_r,a_c,b_r,b_c;
int i,j,k;
clrscr();
again:
printf("\nenter rows and columns for matrix one:");
scanf("%d%d",&a_r,&a_c);
/* allocate memory for matrix one */
a=(int **) malloc(sizeof(int *),a_r);
for( i=0;i {
a[i]=(int *) malloc(sizeof(int*)*a_c);
}

/* allocate memory for transpose matrix */
c=(int **) malloc(sizeof(int *)*a_r);
for( i=0;i {
c[i]=(int *) malloc(sizeof(int)*a_c);
}


printf("\n enter matrix one %d by %d\n",a_r,a_c);
for(i=0;i {
for(j=0;j {
scanf("%d",&a[i][j]);
}
}


/*initialize product matrix */
for(i=0;i {
for(j=0;j {
c[i][j]=a[i][j];
}
}

printf("\n Transpose of matrix one and two is\n");
for(i=0;i {
for(j=0;j {
printf("%d\t",c[i][j]);

}
printf("\n");
}

/*free memory*/
for(i=0;i {
free(a[i]);
}
free(a);
for(i=0;i {
free(c[i]);
}
free(c);
printf("\npress any key");
getch();
}

0 comments:

Post a Comment