I'm having trouble with MALLOC in C. I have the following code :
//This program finds the sum of two matrices using MALLOC
#include<stdio.h>
#include<stdlib.h>
void main()
{
int *a,i,r1,c1;
printf("\nEnter the dimensions of the Matrix :");
scanf("%d %d",&r1,&c1);
if(r1==c1)
{
a=(int*)calloc(r1*r1,sizeof(int));
b=(int*)calloc(r1*r1,sizeof(int));
c=(int*)calloc(r1*r1,sizeof(int));
printf("\nElements of A :");
for(i=0;i<r1*c1;i++)
{
scanf("%d",&a[i]);
} printf("\nElements of A :");
for(i=0;i<r1*c1;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<r1*c1;i++)
{
c[i]=a[i]+b[i];
}
/***I'm not writing the remaining part ..It display the Matrix C ***/
free(a);
free(b);
free(c);
}
else
printf("\nOnly Square Matrices \n");
}
This program find the sum of two matrices . When i run this program
Enter the dimensions of the Matrix :3 3
Enter the elements of Matrix A :1 2 3 4 5 6 7 8 9
10 11 12 1 3 5 45 4 6 6 46 4 6 64 6 64 64 6 6 64 64 6 64
//It doesn't stop till i press Ctrl+C
I don't know why this happens . Help me figure out the exact problem .
Aucun commentaire:
Enregistrer un commentaire