English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

/*code functions in static case but got to use dynamic array*/
#include
#include
#include
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(&p[j][0],sizeof(p[j][0]),1,fp); */

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

}

j=j-1;
for( i = 1; i+1<=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);

}

2007-12-10 23:21:53 · 4 answers · asked by Ankit 1 in Computers & Internet Programming & Design

4 answers

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.

2007-12-11 01:39:22 · answer #1 · answered by AnalProgrammer 7 · 0 0

confident, yet its in basic terms a play with words. in case you have been to create a String classification. you ought to define the two public and own get entry to operators. the interior maximum get entry to operators might do the actual nitty gritty stuff with dynamic reminiscence allocation and tips, mutually as the everyday public get entry to operators might supply a greater straight forward interface to characteristic(), delete(), length() operators. So the programmer who gets to place in writing the String classification may be no longer able to stay away from the pointer programming. in spite of the undeniable fact that, the programmer who gets to apply the String classification might have a greater straight forward answer. The String classification programmer provides you as many hardship-free public get entry to operations that they see greater healthful. they might have one talked approximately as change_lenght() that provides a public interface to the privately hidden dynamic reminiscence allocators/destructors written with tips and carefully written calls to malloc(), unfastened() and realloc().

2016-11-02 21:15:51 · answer #2 · answered by ? 4 · 0 0

Hey mind one simple thing array means static only you cannot declare it dynamic okay

2007-12-10 23:28:51 · answer #3 · answered by RAJ Kumar 2 · 0 0

have a look at this:

// DMA of 2D array in C++
#include
#include
using namespace std;
int main()
{
int x = 3, y = 3;

int **ptr = new int *[x];

for(int i = 0; i {
ptr[i] = new int[y];
}
srand(time(0));

for(int j = 0; j {
for(int k = 0; k {
int a = rand()%10;
ptr[j][k] = a;
cout< }
cout< }
}

for explaination see :

http://www.programmingtunes.com/dma-of-2d-array-c/

2013-09-30 06:23:37 · answer #4 · answered by Anonymous · 0 0

fedest.com, questions and answers