Array problem

This is a discussion on Array problem within the C++ Programming forums, part of the General Programming Boards category; I'm having a problem making a multidimensional array. I want the size of the array to correspond to some variables. ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    6

    Question Array problem

    I'm having a problem making a multidimensional array. I want the size of the array to correspond to some variables. I'm having trouble making this work.
    Edit: I've tried using vectors.
    Last edited by mrme44; 08-03-2010 at 06:05 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Your problem is poorly stated. Ask a better question. Show examples of what you are attempting.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    6
    This is what I'm trying to do
    Code:
    int room_width = 100;
    int room_height = 100;
    double grid[room_width][room_height];
    It gives me the error
    expression must have a constant value
    I want to use variables in the array but it won't let me. How would I get around this?
    Last edited by mrme44; 08-04-2010 at 08:29 AM.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    What compiler are you using, and what are the options you are specifying?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    6
    I'm using VS and I'm creating a dll

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Does it have a C99 option?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    6
    What's a C99 option?

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    It's an option that allows you to do what you want to do. Google it.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,767
    This is the C++ board. Make the doubles const and ints. Arrays must be a fixed size.
    And VS doesn't support C99.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,427
    C99 refers to the C standard as released in 1999. That standard supports what you want - if you have a compiler that complies with that standard, which VS does not. Your dimensions need to be of integral type, not double though.

    However, neither C++ nor C89 (the preceding C standard of 1989) do, unless the array dimensions are fixed at compile time (and are integers with a const attribute). If you want the dimensions to be determined at run time (eg based on user input) you will need to use dynamic memory allocation in C++ or C89.
    Right 98% of the time, and don't care about the other 3%.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,767
    Quote Originally Posted by grumpy View Post
    ...If you want the dimensions to be determined at run time (eg based on user input) you will need to use dynamic memory allocation in C++ or C89.
    Or just std::vector. Or boost::multi_array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Aug 2010
    Posts
    6
    Thanks for the help. I'll look into using dynamic memory allocation or boost::multi_array.
    Last edited by mrme44; 08-04-2010 at 08:40 AM.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,427
    Quote Originally Posted by Elysia View Post
    Or just std::vector. Or boost::multi_array.
    Both of which do dynamic memory management for you .....
    Right 98% of the time, and don't care about the other 3%.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,767
    Which is the point. You don't have to do it yourself. It's easier, safer and faster.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    Mrme,

    We always had fun with this problem when I was working in the lab in college, because it is such a great example of the memory handling skills that so often hamper a great implementation. It is a little bit of a pain, and it does look like we're re-inventing the wheel. But the point of the exercise was to avoid complex class interfaces, and of course hone the memory "bookkeeping" skills. I threw this together using your specifications:

    Code:
    #include<iostream>
    #include<iomanip>
    
    using namespace std;
    
    double **getMatrix( int width, int height );
    void releaseMatrix( double **grid, int width, int height );
    void printMatrix( double **grid, int width, int height );
    
    int main(int argc, char *argv[])
    {
        double **grid;
        int j;
        int room_width = 10;
        int room_height = 10;
        
        grid = getMatrix( room_width, room_height );
        
        // perform operations on your grid...
        for( j = 0; j < room_width; j++ )
        {
            grid[ j ][ j ] = j*2.0 + 1.0;
        }
        
        printMatrix( grid, room_width, room_height );
        
        releaseMatrix( grid, room_width, room_height );
    
        system("pause");
    	return 0;
    }
    
    double **getMatrix( int width, int height )
    {
        double **allocate;
        int j;
        int k;
        
        allocate = new double*[width];
        
        if( NULL == allocate )
        {
            cerr << "Memory allocation problem...exiting." << endl;
            
            exit( 1 );
        }
        
        for( k = 0; k < width; k++ )
        {
            allocate[k] = new double[height];
            
            if( NULL == allocate[k] )
            {
                    cerr << "Memory allocation problem...exiting." << endl;
                    exit( 1 );
            }
    
            for( j = 0; j < height; j++ )
            {
                    allocate[k][j] = 0.0;
            }
        }
        
        return allocate;
    }
    
    void releaseMatrix( double **grid, int width, int height )
    {
        int k;
        
        if( NULL != grid )
        {
            for( k = 0; k < width; k++ )
            {
                    delete [] grid[k];
            }
            delete [] grid;
        }
        
        return;
    }
    
    void printMatrix( double **grid, int width, int height )
    {
        int j;
        int k;
        
        for( j = 0; j < height; j++ )
        {
            for( k = 0; k < width; k++ )
            {
                    cout << setw(4) << grid[k][j];
            }
            cout << endl;
        }
        
        return;
    }
    The two dimensional array, which you called grid, can be allocated and de-allocated as necessary. I must admit that it is a little stilted, but it is very readable and can be applied to aggregate data types too. The output of this code is:

    Code:
       1   0   0   0   0   0   0   0   0   0
       0   3   0   0   0   0   0   0   0   0
       0   0   5   0   0   0   0   0   0   0
       0   0   0   7   0   0   0   0   0   0
       0   0   0   0   9   0   0   0   0   0
       0   0   0   0   0  11   0   0   0   0
       0   0   0   0   0   0  13   0   0   0
       0   0   0   0   0   0   0  15   0   0
       0   0   0   0   0   0   0   0  17   0
       0   0   0   0   0   0   0   0   0  19
    Press any key to continue . . .
    I think that the syntax I used accounts for the update to the standard (C99), where the delete operator required the braces...I think that arrays got an extra atom associated with them for the heap management, but I am only speculating. The text I am using is Victor Shtern's CORE C++: A Software Engineering Approach. It was printed in 2000, and has been accurate in every application I have identified. Is this what you had in mind, Mrme?

    Best Regards,

    New Ink -- Henry

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array copy problem
    By Witch in forum C Programming
    Replies: 3
    Last Post: 03-22-2010, 07:00 PM
  2. Sorting array problem :)
    By BEST in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2009, 12:57 PM
  3. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  4. Replies: 6
    Last Post: 02-15-2005, 10:20 PM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM

Tags for this Thread


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