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. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    20

    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. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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( ) ) );
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,739
    Quote 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).

    Quote Originally Posted by Sebastiani
    Just use std::vector:
    That will require a switch of programming language to C++.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Quote Originally Posted by laserlight View Post
    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.
    Last edited by rhouli67; 03-30-2009 at 11:19 PM.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 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 );
    Last edited by Sebastiani; 03-30-2009 at 11:55 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,739
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Thanks for your help laserlight and Sebastiani i should be able to figure the rest out.

    Quote Originally Posted by cpjust View Post
    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. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    This *is* the C Board. Or did you overlook that just as I did?
    Last edited by Sebastiani; 03-31-2009 at 12:24 PM. Reason: typo
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Sebastiani View Post
    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.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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;
    }
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of array
    By goran00 in forum C Programming
    Replies: 38
    Last Post: 04-02-2008, 09:57 AM
  2. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 10:27 AM
  3. Replies: 42
    Last Post: 12-19-2004, 07:59 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21