make an 2 dimensional array (like 6 *6) and then split it in 2 vertical slices in c++?

I would like to know how I can make a 2-dimensional array (6*6) and then split it to two vertical slices (and sometimes to change it into 3 vertical slices) and then store each slice into 1d array. The following description shows exactly what I want:
I would like to know how I can make a 2-dimensional array (6*6) and then split it to two vertical slices (and sometimes to change it into 3 vertical slices) and then store each slice into a 1d array.
Thanks for the help.

[code]
#include <iostream>

int main(void)
{
int myArray[6][6] = { {0, 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} };
int width = 6, height = 6;

for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
std::cout << myArray[i][j] << ' ';
}
std::cout << std::endl;
}
}

Last edited on
int s1[3][3];
int s2[3][3];

for(r = 0; r < 3; r++)
for(c = 0; c< 3; c++)
{
s1[r][c] = array[r][c];
}

for(r = 3; r < 6; r++)
for(c = 3; c< 6; c++)
{
s2[r-3][c-3] = array[r][c];
}

as for the 2d 1d stuff.. make them 1d to begin with.
you can index a 1d array like a 2d array by array[desired_row*numcols+desired_col]

there are a lot of reasons 1d arrays are easier to work with, so if you did that up front you would have it both ways.

built in templates are powerful. Vectors are nice here, but valarray's slice might be just the thing as well. Why not use this stuff... rolling your own gets harder and harder as your problem size scales.

Last edited on
@ jonnin
Thanks for your comment.
I recently updated the code and i tried your approach too, but i couldn't find out how to do it.
would you please help me with understanding to how to do it . I will be more than happy if you provide me with the source code. I am a beginner in c++.

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const int c = 6;
const int r = 6;
int m2d[r*c];

  int myArray[6][6] = { {0, 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} }; //your array, just to deal with the data. This isn't necessary,
 but copy and paste was too easy to pass up here and I needed an example for you anyway so copying this is as good as anything else. 

for(j = 0; j < r; j++)
for(k = 0; k<c; k++)
m2d[j*c+k] - myArray[j][k];

you can define and take the slices the same way as above (first post) except use the indexing formula I gave.

m2d is already a 1-d array.   You can use it as such so there is no conversion. 




Last edited on
@jonnin

thanks for reply. i have done applied and run the code the code, but still shows only the 6*6 matrix.
#[code]include <iostream>
#include <iostream>
using namespace std;



test[0][0] = 0
test[0][1] = 1
test[0][2] = 2
test[0][3] = 3
.
.
.
.


thanks
Last edited on
I was trying to get you to do it to learn. But maybe you need to see this before you really absorb it. Wow, the web mangled my indents etc. Not going to fix that here.


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
#include<cstring>
#include <iostream>
#include<cstdlib>

using namespace std; //bad but going with the flow
int main()
{
    const int c = 6;
    const int r = 6;
    int m2d[r*c] = 
	   {0, 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};  //got rid of the example copy            
		
int s1[18]; //slice 1
int s2[18]; // slice 2
int dx = 0;  //inDeX counter.
for(int j = 0; j < r; j++)
{
	for(int k = 0; k<3; k++)
	{
       s1[dx] = m2d[j*c+k];  //it so happens that we can do both slices in 1 loop
	   s2[dx++] = m2d[j*c+k+3];
	}
}
//proof / printout
for(int j = 0; j < 18; j++)
{	
  cout<< s1[j] <<",";
  
  if((j+1)%3==0) cout << endl;
}
cout <<"------------"<<endl;

for(int j = 0; j < 18; j++)
{	
  cout<< s2[j] <<",";
  
  if((j+1)%3==0) cout << endl;
}
}


You could transpose, memcpy, and transpose back. I started to do that, but it was more convoluted than you probably need. That lets you take the columns directly, as much as you want, but its just as expensive as the above until you reach memory access problem sized data.
note that you can index s1 and s2 *as if* 2-d same as m2d. But for this code, it was easier to iterate directly. That is one of the advantages of the 1-d approach !
Last edited on
Thanks @joinnin
Done
Registered users can post here. Sign in or register to post.