Thread: Array problem

  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
    Chappell Hill, Texas
    Posts
    2,332
    Your problem is poorly stated. Ask a better question. Show examples of what you are attempting.
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Chappell Hill, Texas
    Posts
    2,332
    What compiler are you using, and what are the options you are specifying?
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Chappell Hill, Texas
    Posts
    2,332
    Does it have a C99 option?
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Chappell Hill, Texas
    Posts
    2,332
    It's an option that allows you to do what you want to do. Google it.
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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

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, 01: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, 11: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