C++: Simple array manipulating Using 1D & 2D Arrays?

This is a discussion on C++: Simple array manipulating Using 1D & 2D Arrays? within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm writing a program that inserts a 1D array into a specific location in a 2D array. How do ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    9

    Question C++: Simple array manipulating Using 1D & 2D Arrays?

    Hello, I'm writing a program that inserts a 1D array into a specific location in a 2D array.
    How do I insert it:


    Code:
    int array[][MAX_COLS] = {{2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22 },
    {4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44 },
    {5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 },
    {6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66 },
    {7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77 },
    {8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88 }};
    
    int insertarray[] = {3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33 };
    // I need to put this into the 2 row.
    
    for (int nRows = 0; nRows < 6; nRows++ )
      {
       for( int nCols = 0; nCols < MAX_COLS; nCols++ )
          {
           cout << array[nRows][nCols] << " ";
          }
       cout << endl;
      }
    
    

    Thank you in advance;

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    std::array<std::array<T, N>, M> My2DArray;
    // Initialize 2D array...
    std::array<T, N> My1DArray;
    // Initialize 1D array...
    My2DArray[K] = My1DArray;

    T and N of 1D array must obviously agree with T and N in the 2D array.
    Don't forget to include <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.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    Thank you for your answer but I don't understand it. I'm using a loop to solve this problem

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    The reasoning is simple. Treat the array as if it were just a variable.
    If you use C++ arrays, this is possible. No looping is necessary.
    I would recommend some research on std::array and how to use it. Very simple. Very elegant. Very safe. Works very well with the standard library, too.
    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.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    Regardless of using std::array or old style primative arrays, My2DArray[k] is a 1D array represending the kth row of My2DArray.

    You cannot copy primative arrays, directly, nor can you copy from one type of array or other container to another using =. You must use a loop or a standard function. But this just like coppying any 1D sequence of data.

    Using std::array is a good idea though.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    You can copy or assign std::arrays directly to each other:

    Code:
    #include <array>
    
    int main()
    {
    	const int N = 10;
    	const int M = 20;
    	const int K = 5;
    	typedef int T;
    	std::array<std::array<T, N>, M> My2DArray;
    	// Initialize 2D array...
    	std::array<T, N> My1DArray;
    	// Initialize 1D array...
    	My2DArray[K] = My1DArray;
    }
    My1DArray and My2DArray here are uninitialized, so be sure to initialize them properly before use, unlike what I do here.
    This is just to show an example that compiles.
    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.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    Thank you for your answer!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating data in an array...
    By MikeyIckey in forum C Programming
    Replies: 11
    Last Post: 01-03-2009, 02:23 PM
  2. problem in manipulating a maped array
    By Masterx in forum C++ Programming
    Replies: 20
    Last Post: 12-31-2008, 12:06 PM
  3. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  4. pointer manipulating 2-dimensional array
    By John Connor in forum C Programming
    Replies: 2
    Last Post: 01-31-2008, 12:15 PM
  5. Manipulating Array Values
    By benjamin923 in forum C Programming
    Replies: 5
    Last Post: 08-02-2007, 12:43 PM

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