Thread: dynamic arrays

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    that means to put a for loop on the 2nd pointer and another for loop for the 3rd pointer and another for loop for the nth pointer yes?
    Yes. Note that you could also use a one dimensional array to represent a multi-dimensional array, upon which you would calculate the index offsets yourself.

    I would have thought C would be a pre-requisite for a C++ course?
    No, at least not for the courses taught by Andrew Koenig and Barbara Moo, but then they teach vectors before dynamically allocated arrays.

    EDIT:
    Oh, and I was taught C++ without a C pre-requisite, though they taught me the C legacy of C++ (but with I/O streams) before the C++ specific stuff, somewhat like sokermaniac's professor's approach.
    Last edited by laserlight; 05-12-2008 at 11:12 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    May 2008
    Posts
    13
    ok for a 2d here is what i have:

    int **board;

    int rows;
    int columns;

    cout << "Enter number of rows and columns: ";
    cin >> rows >> columns;

    board = int int* [rows];

    for (int row = 0; row < rows; row++)
    board[row] = new int[columns];

    return 0;


    something from an example i had.

  3. #18
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by cpjust View Post
    Strange... I would have thought C would be a pre-requisite for a C++ course?
    Sadly, it not only seems to be pre-req, but "C with Classes" seems to be what most college grads in the US and abroad are bringing to the workplace. I'd be hard pressed to find a beginner that doesn't make the same mistakes that have been made for years, instead of simply doing things the "C++ way".

  4. #19
    Registered User
    Join Date
    May 2008
    Posts
    13
    yeah for my class you can dive right into C++ without C. I'm assuming i may have to learn C sometime eh?

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by sokermaniac View Post
    ok for a 2d here is what i have:

    int **board;

    int rows;
    int columns;

    cout << "Enter number of rows and columns: ";
    cin >> rows >> columns;

    board = int int* [rows];

    for (int row = 0; row < rows; row++)
    board[row] = new int[columns];

    return 0;


    something from an example i had.
    Please use code tags, but yes, you're on the right track. You just need to add another dimension there.
    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.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    ok for a 2d here is what i have:
    Looks okay, though you might want the array index to be an unsigned int instead of an int. Also, remember to delete[] what you new[].
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by medievalelks View Post
    Sadly, it not only seems to be pre-req, but "C with Classes" seems to be what most college grads in the US and abroad are bringing to the workplace.
    Or as I call it -- C+ programming.

    I learned C by myself, followed by C++, so I'm not sure how they structure the courses. But I guess there are pros & cons to learning C first.
    Pro: You can learn the language basics without being overwhelmed by classes, STL, templates...
    Con: When you do learn C++ it might take longer to unlearn some of the ugly C stuff.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would like to say that everyone drops C and starts learning C++ and C++/C (my own abbreviation for C code compiled with a C++ compiler ).
    Obviously, working with pointers and all that might be called C, but it is definitely good knowledge! :O
    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.

  9. #24
    Registered User
    Join Date
    May 2008
    Posts
    13
    ok sorry about not using code tags :P not used to forums haha. 2nd my prof was going to start delete but toward the end of the semester he decided not to teach that part but im sure its not difficult to use or learn. then again im a slow learner. :P 3rd how come you would want it to be an unsigned int? is it because the dimensions or the data may be negative?

    Code:
    int **board;
    
    int rows;
    int columns;
    
    cout << "Enter number of rows and columns: ";
    cin >> rows >> columns;
    
    board = int int* [rows];
    
    for (int row = 0; row < rows; row++)
    board[row] = new int[columns];
    
    return 0;
    for a 3d

    Code:
    int ***board;
    
    int rows;
    int columns;
    int thirddim;
    
    cout << "Enter number of rows and columns and some number for the 3rd dimeninsion: ";
    cin >> rows >> columns >> thirddim;
    
    board = int int* [rows];
    
    for (int row = 0; row < rows; row++)
    board[row] = new int[columns];
    for (int  column = 0; column < columns; column++)
    board[colum] = new int[thirddim];
    
    return 0;

  10. #25
    Registered User
    Join Date
    May 2008
    Posts
    13
    by the way, i really appreciate everybody helping me out here! i like programming but im no stud compared to you all! its a constant struggle with me

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Now that you are using code tags, you can actually indent the code and the board won't strip away the spaces! You should try doing that.
    Secondly, delete is very important and I can't believe someone would not teach it >_<
    Anything allocates with new will never go away unless you call it with delete.
    Anything allocates with new must be deallocated with delete.
    Anything allocated with new [] (such as your array), must be deallocated with delete[]. Otherwise you're going to leak memory (memory will still stay as used, but you can't access it).

    And as for your code... well, you seem to have a small problem.
    An array (pointer) to hold a pointer to another array (pointer) to hold more pointers to more arrays.
    Try writing it down on paper, maybe. You'll see what's wrong. Hopefully anyway.
    Remember that only a pointer can contain an address, and to contain a pointer to a pointer, you need a pointer to pointer (type**). This goes up.
    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. #27
    Registered User
    Join Date
    May 2008
    Posts
    13
    yeah my prof is a little unorthodox. but i had no choice but to get him this semester. i've been reading ahead and im understanding the delete concept just not how to use it haha.

    and the small problem. did i need to initalize board again for the 3rd d for the 2nd for loop?

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    3rd array holds normal ints.
    2nd arrays must hold pointers to that array of int (thus int*).
    1st array must hold pointers to the 2nd array which holds int* (the type then becomes...?)
    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.

  14. #29
    Registered User
    Join Date
    May 2008
    Posts
    13
    something like this?
    Code:
    int ***board;
    
    int rows;
    int columns;
    int thirddim;
    
    cout << "Enter number of rows and columns and some number for the 3rd dimeninsion: ";
    cin >> rows >> columns >> thirddim;
    
    board = int int* [rows];
    
    for (int row = 0; row < rows; row++)
         board[row] = new int[columns];
    for (int  column = 0; column < columns; column++)
         board[row][column] = new int[thirddim];
    
    return 0;
    Last edited by sokermaniac; 05-12-2008 at 11:48 AM. Reason: added row before column

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not quite. Have you tried compiling that? It probably won't work, will it?
    But it might give you some helpful diagnostic messages.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 11:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM
  4. dynamic arrays and structures
    By godofbabel in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2002, 03:45 PM