1d to 2d arrays

What's an efficient way of turning a 1D array to a 2D array but keeping the [] for a 1D size.

example:
 
int a[9] = {0,1,2,3,4,5,6,7,8};//how can this be treated as a 3x3 instead? while keeping the a[9] declaration 



EDIT: also what's an example code for implementing the equation for "width * row + col" for such an array above and whatever else needed for that implementation
Last edited on
@Kalcor

1
2
3
4
5
6
7
8
9
10
11
int a[9] = {0,1,2,3,4,5,6,7,8};

for(int x=1;x<10;x++)
{
	cout << a[x-1];
	if(x%3 == 0)
	{
		cout << " - A["<<(x/3)<<"][0] to A["<<(x/3)<<"][2] = ";
		cout << 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
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <iomanip>
#include <numeric>
using namespace std;

const int NUMROWS = 3;
const int NUMCOLS = 3;
const int NUM = NUMROWS * NUMCOLS;

//======================================================================

void toRowCol( int n, int &row, int &col )      // convert from 1d index [n] to 2d index [row][col]
{
   col = n % NUMCOLS;
   row = n / NUMCOLS;
}

//======================================================================

int fromRowCol( int row, int col )              // returns 1d index [n] from 2d index [row][col]
{
   return row * NUMCOLS + col;
}

//======================================================================

int main()
{
   int row, col, n;
   int a[NUM];
   iota( a, a + NUM, 0 );

   #define SP << ' ' << setw( 4 ) <<
   #define NL << '\n'

   cout SP "row" SP "col" SP "a[n]" NL;
   for ( n = 0; n < NUM; n++ ) 
   {
      toRowCol( n, row, col );
      cout SP row SP col SP a[n] NL;
   }

   cout NL NL;

   cout << "2d array:\n";
   for ( row = 0; row < NUMROWS; row++ )
   {
      for ( col = 0; col < NUMCOLS; col++ )
      {
         n = fromRowCol( row, col );
         cout SP a[n];
      }
      cout NL;
   }
}


  row  col a[n]
    0    0    0
    0    1    1
    0    2    2
    1    0    3
    1    1    4
    1    2    5
    2    0    6
    2    1    7
    2    2    8


2d array:
    0    1    2
    3    4    5
    6    7    8
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
#include <iostream>

template < typename T > struct view_as_2d
{
    T* arr ;
    std::size_t sz ;
    std::size_t rows ;
    std::size_t cols ;

    // range checks and const overloads are elided for brevity.

    view_as_2d( T* arr, std::size_t sz, std::size_t rows ) // invariannt: rows != 0
        : arr(arr), sz(sz), rows(rows), cols(sz/rows) {}

    template < std::size_t N > view_as_2d( T(&arr)[N], std::size_t rows ) // invariannt: rows != 0
        : view_as_2d( arr, N, rows ) {}

    struct row_wrapper
    { T* begin ; T& operator[] ( std::size_t col ) { return begin[col] ; } };

    row_wrapper operator[] ( std::size_t row ) { return { arr + row*cols } ; }
};

int main()
{
    int a[24] {};
    for( int i = 0 ; i < 24 ; ++i ) a[i] = i+10 ;

    for( std::size_t i = 0 ; i < 24 ; ++i ) std::cout << a[i] << ' ' ;
    std::cout << "\n------------------------------\n" ;

    view_as_2d<int> v46( a, 4 ) ; // 4x6
    for( std::size_t row = 0 ; row < v46.rows ; ++row )
    {
        for( std::size_t col = 0 ; col < v46.cols ; ++col ) std::cout << v46[row][col] << ' ' ;
        std::cout << '\n' ;
    }
    std::cout << "------------------------------\n" ;

    view_as_2d<int> v38( a, 3 ) ; // 3x8
    for( std::size_t row = 0 ; row < v38.rows ; ++row )
    {
        for( std::size_t col = 0 ; col < v38.cols ; ++col ) std::cout << v38[row][col] << ' ' ;
        std::cout << '\n' ;
    }
    std::cout << "------------------------------\n" ;

    view_as_2d<int> v54( a, 5 ) ; // view the sub-array (first 20 elements) as 5x4
    for( std::size_t row = 0 ; row < v54.rows ; ++row )
    {
        for( std::size_t col = 0 ; col < v54.cols ; ++col ) std::cout << v54[row][col] << ' ' ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/6841d91f71f42b5b
Topic archived. No new replies allowed.