Monday, May 24, 2010

How to declare and use 2d dynamic array in c language?

/*code functions in static case but got to use dynamic array*/


#include%26lt;stdio.h%26gt;


#include%26lt;math.h%26gt;


#include%26lt;conio.h%26gt;


void main(int argc, char *argv[])





{


FILE *fp;


int i,j=0;


float n,x1,y1,x2,y2,cross,result,area=0,p[5][5...


clrscr();


fp=fopen(argv[1],"r");


if(fp==NULL)


{


printf("Cannot open file");


exit(0);


}








while(!feof(fp)) {


/* fread(%26amp;p[j][0],sizeof(p[j][0]),1,fp); */





fscanf(fp, "%f %f", %26amp;p[j][0],%26amp;p[j][1]);


printf("%f\n,%f\n",p[j][0],p[j][1]);


j++;





}





j=j-1;


for( i = 1; i+1%26lt;=j; i++)


{


x1 = p[i][0] - p[0][0];


y1 = p[i][1] - p[0][1];


x2 = p[i+1][0] - p[0][0];


y2 = p[i+1][1] - p[0][1];


printf("x1 is %f\ny1 is %f\nx2 is %f\ny2 is %f\n",x1,y1,x2,y2);


cross = x1*y2 - x2*y1;


printf("cross is %f\n",cross);


area += cross;





}


result = fabs(area/2.0);





printf("The area is %f",result);





}

How to declare and use 2d dynamic array in c language?
Hey mind one simple thing array means static only you cannot declare it dynamic okay
Reply:You do not actually declare a two dimentional dynamic array in C.


What you do is declare some storage. You reference this storage using a 1 dimention array, but you control the subscript in such a way that it acts like a 2, 3 etc. dimension array.





A little bit mind blowing but if you check out the three links you should be able to get it to work.

sd cards

No comments:

Post a Comment