How to store a small array in a bigger one

Hey guys, I'm new to programming and C++. I have a little problem here. Is there a way to store a small array, say test[2][2], in a bigger one, say field[7][5]? I made something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;
const int r = 7;
const int c = 5;
void display(int ar[][c], int size);
int main()
{
    int field[r][c];
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            field[i][j] = 0;
        }
    }
    
    int test[2][2] = {
        {2, 2},
        {2, 2},
    };
    
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            field[i][j] = test[i][j];
        }
    }
    
    display(field, r);
    return 0;
}

void display(int ar[][c], int size) {
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < c; j++) {
            cout << ar[i][j] << " ";
        }
        cout << endl;
    }
}


But what if I want to store the test array somewhere in the middle of the field array? Is there a way to do so?
I'd be really appreciated for your help guys.
You can copy values however you like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 int test[2][2] = {
        {2, 3},
        {4, 5},
    };

    int field[7][5];

  // Store values from test in field
  field[0][0] = test[0][0];
  field [0][1] = test[0][1];
  field [1][0] = test[1][0];
  field [1][1] = test[1][1];


    // Store values from test in field, in different place
  field[3][3] = test[0][0];
  field [3][4] = test[0][1];
  field [4][3] = test[1][0];
  field [4][4] = test[1][1];


You can take the four values of int test[2][2] and put them into the 35 spaces of int field[7][5] however you like.

A bigger question is why? What are you actually trying to achieve, because this doesn't make much sense. Why are you taking these four values and storing them in the other array?
Last edited on
You may need more flexibility than a C-style array offers; try a vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <iomanip>
#include <vector>
#include <cassert>
using namespace std;

using vec    = vector<int>;       // aliases to avoid long type expressions
using matrix = vector<vec>;

int fieldWidth = 10;              // field width to output matrix elements


//=====================================================================
// Extract a submatrix of m rows and n columns, starting at i0, j0
//=====================================================================
matrix subMatrix( const matrix &M, int i0, int j0, int m, int n )
{
   assert( i0 >= 0 && i0 + m - 1 < M.size() && j0 > 0 && j0 + n - 1 < M[0].size() );   // check validity

   matrix result( m, vec( n ) );
   for ( int i = 0; i < m; i++ )
   {
      for ( int j = 0; j < n; j++ ) result[i][j] = M[i0+i][j0+j];
   }

   return result;
}


//=====================================================================
// Overwrite a matrix with submatrix, starting at i0, j0
//=====================================================================
void addToMatrix( const matrix &S, matrix &M, int i0, int j0 )
{
   int m = S.size(), n = S[0].size();                                                  // get submatrix size
   assert( i0 >= 0 && i0 + m - 1 < M.size() && j0 > 0 && j0 + n - 1 < M[0].size() );   // check validity

   for ( int i = 0; i < m; i++ )
   {
      for ( int j = 0; j < n; j++ ) M[i0+i][j0+j] = S[i][j];
   }
}


//======================================================================
// Overload << operator to output a matrix
//======================================================================
ostream &operator << ( ostream &out, const matrix &M )
{
   for ( auto &row : M )
   {
      for ( auto e : row ) out << setw( fieldWidth ) << e << ' ';
      out << '\n';
   }
   return out;
}


//======================================================================
// Driver program
//======================================================================
int main()
{
   // Original matrix
   matrix M = { { 0, 1, 2, 3, 4 }, { 10, 11, 12, 13, 14 }, { 20, 21, 22, 23, 24 }, { 30, 31, 32, 33, 34 },
                                   { 40, 41, 42, 43, 44 }, { 50, 51, 52, 53, 54 }, { 60, 61, 62, 63, 64 } };
   int r = M.size(), c = M[0].size();

   // Blank matrix of same size
   matrix B = vector<vec>( r, vec( c, 0 ) );

   // Submatrix size and location
   int i0 = 4, j0 = 2, subr = 2, subc = 2;

   // Grab submatrix from M
   matrix S = subMatrix( M, i0, j0, subr, subc );

   // And stick it in the same position in the blank matrix
   addToMatrix( S, B, i0, j0 );


   // Now let's see what we've got
   cout << "Original matrix:\n" << M << '\n';
   cout << "Submatrix:\n" << S << '\n';
   cout << "Put into blank matrix (at same location): \n" << B << '\n';
}


Original matrix:
         0          1          2          3          4 
        10         11         12         13         14 
        20         21         22         23         24 
        30         31         32         33         34 
        40         41         42         43         44 
        50         51         52         53         54 
        60         61         62         63         64 

Submatrix:
        42         43 
        52         53 

Put into blank matrix (at same location): 
         0          0          0          0          0 
         0          0          0          0          0 
         0          0          0          0          0 
         0          0          0          0          0 
         0          0         42         43          0 
         0          0         52         53          0 
         0          0          0          0          0 

Last edited on
Thank you for your reply! I appreciate it. I want to make a simple Tetris clone, so I'm trying to figure out how to move small arrays, tetrominoes, inside a big array, which is going to be the board.
lastchance, only now did I see your post. This is awesome! I made something like this:

1
2
3
4
5
6
    int posY = 2, posX = 1;
    for (int i1 = posY, i2 = 0; i1 < posY+2; i1++, i2++) {
        for (int j1 = posX, j2 = 0; j1 < posX+2; j1++, j2++) {
            field[i1][j1] = test[i2][j2];
        }
    }


It seems to be working. However, your code undoubtedly is waaaaay better than mine. Thank you very much for your help!
Last edited on
Topic archived. No new replies allowed.