how to modify 2d array in function c 2b 2b

Solutions on MaxInterview for how to modify 2d array in function c 2b 2b by the best coders in the world

showing results for - "how to modify 2d array in function c 2b 2b"
Lucas
24 Apr 2020
1void Insert_into_2D_Array(int** foo, int x_pos, int y_pos, int x_size, int y_size)
2{
3  int insert_value = 10; 
4
5  if (x_pos < x_size && y_pos < y_size) {
6    foo[x_pos][y_pos] = insert_value;    // insert_value lost post func exit?
7  }
8}
9
10void Init_2D_Array(int** foo, int x_size, int y_size)
11{
12
13  foo = new int*[x_size];    // new alloc mem lost post func exit ?
14  for (int i=0;i<x_size;i++)
15  {
16      foo[i] = new int[y_size];     // new alloc mem lost post func exit
17  }
18}
19
20int main(int agc, char** argv)
21{
22
23  int** foo; 
24  int x_size=10, y_size=10;   
25  Init_2D_Array(foo, x_size, y_size); 
26  Insert_into_2D_Array(foo, 3,3, x_size, y_size); 
27
28}
29
Eric
01 Feb 2016
1class Array2D
2{
3private:
4    int* m_array;
5    int m_sizeX;
6    int m_sizeY;
7
8public:
9    Array2D(int sizeX, int sizeY) : m_sizeX(sizeX), m_sizeY(sizeY)
10    {
11        m_array = new int[sizeX*sizeY];
12    }
13
14    ~Array2D()
15    {
16        delete[] m_array;
17    }
18
19    int & at(int x, int y)
20    {
21        return m_array[y*sizeX + x];
22    }
23};
24
Stefano
11 Jan 2021
1void Insert_into_2D_Array(int** foo, int x_pos, int y_pos, int x_size, int y_size)
2{
3    int insert_value = 10;
4
5    if (x_pos < x_size && y_pos < y_size) {
6        foo[x_pos][y_pos] = insert_value;    // insert_value lost post func exit
7    }
8}
9
10int** Init_2D_Array(int x_size, int y_size)
11{
12
13    int** foo = new int*[x_size];    // new alloc mem lost post func exit  
14    for (int i = 0; i<x_size; i++)
15    {
16        foo[i] = new int[y_size];     // new alloc mem lost post func exit
17    }
18
19    return foo;
20}
21
22int main()
23{
24
25    int** foo;
26    int x_size = 10, y_size = 10;
27    foo = Init_2D_Array(x_size, y_size);
28    Insert_into_2D_Array(foo, 3, 3, x_size, y_size);
29
30    return 0;
31}
32
Jessica
22 Aug 2017
1void Insert_into_2D_Array(int** foo, int x_pos, int y_pos, int x_size, int y_size)
2{
3  int insert_value = 10000;
4
5  if (x_pos < x_size && y_pos < y_size) {
6    (foo)[x_pos][y_pos] = insert_value;    // insert_value lost post func exit
7  }
8}
9
10void Init_2D_Array(int*** foo, int x_size, int y_size)
11{
12
13  *foo = new int*[x_size];    // new alloc mem lost post func exit
14  for (int i=0;i<x_size;i++)
15  {
16      (*foo)[i] = new int[y_size];     // new alloc mem lost post func exit
17  }
18}
19
20void main(){
21
22      int** foo = NULL;
23      int x_size=10, y_size=10;
24      Init_2D_Array(&foo, x_size, y_size);
25      Insert_into_2D_Array(foo, 3,3, x_size, y_size);
26
27      cout<<"#############  "<<foo[3][3]<<endl;
28}
29