Assign all values ​​in a dynamically allocated 2d array at one time

So, I have a dynamically allocated matrix. How do I avoid having to assign the values ​​one by one? Is there a way to assign all values ​​at once?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main (int argc, char * argv[]) 
{
	
	int **values = new int*[3];
	for (int i=0;i<3;i++)
		values[i] = new int [3];

        // How to avoid doing this...
	values[0][0] = 1;
	values[0][1] = 2;
	values[0][2] = 3;
	values[1][0] = 4;
	values[1][1] = 5;
	values[1][2] = 6;
	values[2][0] = 7;
	values[2][1] = 8;
	values[2][2] = 9;
}
it depends on what the values are, and what you have.
For 1-9, yes, you can just do a for loop:

n = 1;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
values[i][j] = n++;

@jonnin

But this only works for values ​​that follow some pattern. What I wanted to know is if I had any way of assigning all the values ​​in a single line of code.
no, there isn't. You can do patterns, or random values, but if you want to load it up with specific data, you have to assign each one, somehow, whether from a text file or user input or hard coded values... files and user input make it a loop, but you still have to type it in (file or live).

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main() 
{
   int **values = new int*[3];
   for ( int i = 0; i < 3; i++ ) values[i] = new int[3];

   // Moderately arbitrary set of initialisers
   int n = 0;   for ( int x : { 1, 2, 3, 40, 50, 60, 700, 800, 900 } ) { values[n/3][n%3] = x;   n++; };


   cout << "Just to check:\n";
   for ( int i = 0; i < 3; i++ )
   {
      for ( int j = 0; j < 3; j++ ) cout << i << " " << j << " | " << values[i][j] << endl;
   }
}
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
#include <iostream>

const std::size_t N = 3 ;
template < typename ARR_NxN > void print( const ARR_NxN& array ) ;

int main()
{
    {
        // this is the sane thing to do; we don't need dynamic storage duration
       int values[N][N] = { {1,2,3}, {4,5,6}, {7,8,9} } ;
       print(values) ;
    }

    {
        //  dynamically allocated 2d array. ideally, use std::unique_ptr<int[]>
        auto values = new int[N][N] { {1,2,3}, {4,5,6}, {7,8,9} } ;
        print(values) ;
        
        delete[] values ;
    }

    {
        // dynamically allocated array of pointers (as in the code in the earlier posts),
        // with each pointer pointing to the first element of a dynamically allocated array
        // bad: not exception safe (also as in the code in the earlier posts)
        auto values = new int* [N] { new int[N]{1,2,3}, new int[N]{4,5,6}, new int[N]{7,8,9} } ;
        print(values) ;

        for( std::size_t i = 0 ; i < N ; ++i ) delete[] values[i] ;
        delete[] values ;
    }
}

template < typename ARR_NxN > void print( const ARR_NxN& array )
{
    for( std::size_t row = 0 ; row < N ; ++row )
    {
        for( std::size_t col = 0 ; col < N ; ++col ) std::cout << array[row][col] << ' ' ;
        std::cout << '\n' ;
    }
    std::cout << "------\n" ;
}

http://coliru.stacked-crooked.com/a/208e008b27458036
closed account (48T7M4Gy)
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
#include <iostream>

int main (int argc, char * argv[])
{
    int row_count = 3;
    int col_count = 3;
    
    // INITIALIZE ARRAY
    int** values = new int*[row_count];
    for(int i = 0; i < col_count; ++i)
        values[i] = new int[i];
    
    
    // DATA SOURCE
    int array[] = {1,2,3,4,5,6,7,8,9};
    
    
    // READ DATA INTO MATRIX
    for(int row = 0; row < row_count; row++){
        for(int col = 0; col < col_count; col++)
            values[row][col] = array[row*3+col];
    }
    
    
    // DISPLAY MATRIX
    for(int row = 0; row < row_count; ++row){
        for(int col = 0; col < col_count; ++col){
            std::cout << values[row][col] << ' ';
        }
        std::cout << '\n';
    }
    
    // CLEANUP GOES HERE
    
    return 0;
}


1 2 3 
4 5 6 
7 8 9 
Program ended with exit code: 0


http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new
Last edited on
Topic archived. No new replies allowed.