# Array Size

This is a discussion on Array Size within the C Programming forums, part of the General Programming Boards category; I'm working with a professor on research at the college i attend. My job currently is to convert his code ...

1. ## Array Size

I'm working with a professor on research at the college i attend. My job currently is to convert his code from matlab to C. My problem is i have an array called:

double gp[ x] [ 4 ];

The thing is I don't know X because we will be reading in a array of numbers to GP. Basically I need a way to find the size of X from the array he reads in. I'm completely stumped and would like to know if anyone could help me with this problem.

2. Just use std::vector:

Code:
```typedef vector< double > row;
typedef vector< row > table;

int number_of_rows; // to be filled in
//...
table data( number_of_rows, row( 4, double( ) ) );```

3. Originally Posted by rhouli67
The thing is I don't know X because we will be reading in a array of numbers to GP. Basically I need a way to find the size of X from the array he reads in. I'm completely stumped and would like to know if anyone could help me with this problem.
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).

Originally Posted by Sebastiani
Just use std::vector:
That will require a switch of programming language to C++.

4. 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.

5. >> That will require a switch of programming language to C++.

>> 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)

I'm not sure I understand your question. Are you asking how to calculate the number of rows?

As far as allocation goes, you have two basic options. The 'optimized' method requires less memory, and is easier to manage, but can't be indexed as you normally would a two-dimensional array, whereas the 'standard' way uses more memory, and requires more diligent book-keeping.

(optimized)
Code:
```double* data = malloc( number_of_rows * 4 * sizeof( double ) );
data[ row * 4 + column ] = z;
free( data );```
(standard)
Code:
```double** data = malloc( number_of_rows * sizeof( double* ) );
for( int i = 0; i < number_of_rows; ++i )
{
data[ i ] = malloc( 4 * sizeof( double ) );
}
data[ row ][ column ] = z;
for( int i = 0; i < number_of_rows; ++i )
{
free( data[ i ] );
}
free( data );```

6. There is another way, i.e., typedef the inner array type, then allocate the number of inner arrays you need, e.g.,
Code:
```#include <stdlib.h>

typedef double GpElement[4];

int main(void)
{
size_t number_of_rows = 10;
GpElement* data = malloc(number_of_rows * sizeof(*data));
data[0][0] = 1.23;
free(data);
return 0;
}```

7. What's the reason you're using C instead of C++? Unless it's for an embedded system or something, there should be a C++ compiler for your platform.

8. Thanks for your help laserlight and Sebastiani i should be able to figure the rest out.

Originally Posted by cpjust
What's the reason you're using C instead of C++? Unless it's for an embedded system or something, there should be a C++ compiler for your platform.
My Professor wants the program to be written in C. Why C instead of C++ i don't know. But its part of a program we are writing for a particular type of autonomous robot.

9. This *is* the C Board. Or did you overlook that just as I did?

10. Originally Posted by Sebastiani
This *is* the C Board. Or did you overlook that just as I did?
No, I'm just wondering if there is a valid reason for using C over C++ in this case. If it were me, I'd ask the professor if C++ is fine or if he has a specific reason why he wants it in C.

11. You could also use an array of structures, and copy lasterlight's use of malloc. You could also use C99, and prompt for n.
Code:
```#include <stdio.h>
int main( void )
{
int n = 0;
printf( "Enter n: " );
fflush( stdout );
scanf( "%d", &n );

double c99ick[ n ][ 4 ];

/* ... do stuff here ... */

return 0;
}```