Originally Posted by

**laserlight**
If you have no way to determine x other than by reading in the input, then you should use malloc() to dynamically allocate an array of some size, then read in as much as possible. If you find there is not enough space, expand with realloc(), and repeat as necessary. When you are done with this dynamically allocated array, free() the memory. Note that you should keep track of both the size (as in number of elements in use) and capacity (number of elements that can be used before reallocation is required).

Seeing I'm not familiar with malloc() I have two questions. Ill try to explain them as best as possible although it will be hard.

Code:

#include <stdio.h>
#include <math.h>
#define SIGMA 0.5
int main()
{
double gp[2][4] = { 2, 2, 4, 4,
4, 4, 6, 6};
double c;
int x, n, counter, count_two;
/* This is where I need to find N which is gp[X] */
n = 2;
double ds[n][2], spl[10][4], l[n];
for(x = 0; x < n; x++)
{
ds[x][0] = gp[x][2]-gp[x][0];
ds[x][1] = gp[x][3]-gp[x][1];
}
for(x = 0; x < n; x++)
l[x] = sqrt((pow(ds[x][0], 2))+ (pow(ds[x][1], 2)));
counter = 0;
for(x = 0; x < n; x++)
{
int i = 1;
c = l[x];
while(c > (2*SIGMA))
{
c = c/2;
i *=2;
}
ds[x][0] /= i;
ds[x][1] /= i;
spl[counter][0] = gp[x][0];
spl[counter][1] = gp[x][1];
spl[counter][2] = gp[x][0] + ds[x][0];
spl[counter][3] = gp[x][1] + ds[x][1];
counter++;
for(count_two = 1; count_two < i; count_two++)
{
spl[counter][0] = spl[counter -1][2];
spl[counter][1] = spl[counter -1][3];
spl[counter][2] = spl[counter][0] + ds[x][0];
spl[counter][3] = spl[counter][1] + ds[x][1];
counter++;
}
}
for(x = 0; x < counter; x++)
{
for(b = 0; b < 4; b++)
printf("%f, ", spl[x][b]);
putchar('\n');
}
}

This is a standalone working version of the program. When its implemented as a function as part of a larger code its supposed to take in input of an array of number [n] [4] and replace GP with that array. The array is a series of x and y points in order of:

{ x y x y

x y x y

x y x y

ETC....

}; (this will go on for upwards of 10,000 rows)

Each row of the array we read in is a line with positon x, y too x, y. If the line is greater then a size of 1 we need to cut it down till the line is in numerous segments all less then 1(This is the entire purpose of this function) . Once we have these new lines we input the x y positions back into array spl[ x ][ 4 ].

Now that i gave a little bit of background i have two questions. If i use malloc( ) for gb[ n ][4] will i be able to find out how many rows there are (i need this in order to declare array ds[ n ] [ 2 ]? Also can i use malloc() for spl[x] [ 4 ] seeing that the number of rows, n, is also unknown (but it will certainly be a couple thousand more the GB)

Thank you for your help and time.