2D array

This is a discussion on 2D array within the C++ Programming forums, part of the General Programming Boards category; how can i get a character input in a 2d array?...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Unhappy 2D array

    how can i get a character input in a 2d array?

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Code:
    char d2array[50][50];
    cin>>d2array[x][y]; //where x,y < 50
    Depending on whether you need to input all the elements, you need to run a loop.

    ##I came across a handy *hack* when first learning dynamic allocation months ago, which according to most books..is impossible, but I tested in two compilers and it worked seamlessly.
    Code:
     char d2array[a][b];
    where a and b are variables ...and the array apparently gets dynamically allocated..Why does this work?..is this compiler specific ? ##
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    i need to insert a line in that array!!!
    just like in sngle dimensional arrays we use getline function!!!

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    and you expressed that as character input?
    I'm still not sure what you are trying to say..but the following probably fits....;
    Code:
    std::string m[50];
    std::getline(std::cin,m[x]);  //where x <50
    and if you want each string to be a single element of a 2d array..then just replace the 'char's in first reply with 'std::string'
    and the cin ' line with the getline function..
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    Quote Originally Posted by manasij7479 View Post
    ##I came across a handy *hack* when first learning dynamic allocation months ago, which according to most books..is impossible, but I tested in two compilers and it worked seamlessly.
    Code:
     char d2array[a][b];
    where a and b are variables ...and the array apparently gets dynamically allocated..Why does this work?..is this compiler specific ? ##
    This is a non-standard compiler extension.
    The correct way is to use std::vector.
    Eg:
    std::vector<std::vector<int>> vec(std::vector(b), a);
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    std::vector<std::vector<int>> vec(std::vector(b), a);
    Is there any advantage of initially allocating the size of a std::vector ?.. I have always used std::vector:: push_back(..); when in need of new space..
    Last edited by manasij7479; 04-19-2011 at 06:24 AM. Reason: .. :[b]:p[/b]ush_back(..); was interpreted rather 'nicely'
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    5,886
    Quote Originally Posted by manasij7479 View Post
    Is there any advantage of initially allocating the size of a std::vector ?.. I have always used std::vector:ush_back(..); when in need of new space..
    There is the advantage of only sizing once if the total number of elements needed can be predicted in advance.

    With multiple calls of push_back(), it is necessary to resize the vector. When the new size exceeds the previous size, it is necessary to copy all elements from an old buffer to a new buffer. Apart from multiple calls to dynamically allocate memory, it is necessary to copy all values to a new buffer every time the vector is resized. These involve performance hits, particularly if copying individual elements is expensive, and increased chances of memory fragmentation (in the executing process, not the vector).

    Implementations of vector can reduce the overhead of repeatedly growing the vector using different strategies (allocating more memory than needed in anticipation of growth, and then not resizing actual buffers for the next few elements push_back'd). No such strategy can beat initialisation with a required size, as that involves resizing the buffer exactly once.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    ..how is the above process done in a dynamically allocated array ?...i.e when more space is needed...
    Is it also completely copied over?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  2. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  3. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  4. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM

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