Thursday, December 18, 2008

Memory Allocation for a two dimensional array dynamically


#include
#include

int allocate(float ***p, int m,int n)
{
int i;
*p=(float**)malloc(m*sizeof(float*));
for (i = 0; i < m ; i++)
{
(*p)[i]=(float*)malloc(n*sizeof(float));
}
if (*p==NULL)
{
return 0;
}
else
{
return 1;
}
}

int main()
{
int Row,Col,i,j;
float **Array;
printf("Enter the number od rows and volomns respectively\n");
scanf("%d %d",&Row,&Col);

if (allocate(&Array,Row,Col))
{
printf("array created. start filling it");
}
else
{
printf("memory not allocated. exiting program");
sleep(5);
exit(1);
}

for (i = 0; i < Row ; i++)
{
for (j = 0; j < Col ; j++)
{
printf("\na[%d][%d]=",i,j);
 scanf("%2f",&Array[i][j]);
}

}

for (i = 0; i < Row ; i++)
{
for (j = 0; j < Col ; j++)
{
printf("\nArray[%d][%d]=%f",i,j,Array[i][j]);
}

}

 getch();
 return 0;
}

0 comments:

Hit Counter


Statistics

Followers

 

Programmer's Guide. Copyright 2008 All Rights Reserved