1
int array[3][3][8] = {
    {{3, 4, 5}, {3, 5, 7}, {5, 6, 7}},
    {{1, 3, 5}, {0, 1, 2, 3, 4, 5, 6, 7}, {1, 5, 7}},
    {{1, 2, 3}, {1, 3, 7}, {0, 1, 7}}
};

User inputs x,y coordinate and direction, at that location they can only move in 0-7 direction. However, each location can only move towards certain direction. Therefore I am making this array to see if that direction is in that x, y coordinate’s array. After I get the 3rd dimension array (becomes 1d array), I will see if user input direction is in that array. For example:

 {3,4,5} at 1x1 // then check if direction is in this array

I tried:

 int new_array[8] = array[1][1];

Error: array must be initialized with a brace-enclosed initializer

 int new_array = array[1][1][]; // {3,4,5}

Error: expected primary-expression before ']' token
So I know this syntax isn't valid, are there other ways to achieve such operation? To copy the 3rd dimension array into a new array.

3
  • not clear what is the issue. You want copy from your 3d array to some other array? You want to change data from [1,2,3,4,5,6,7,8] to [3,4,5]? What exactly the issue? your first snippet works just fine. Commented Feb 11, 2021 at 17:19
  • I was trying to copy that 3rd dimension array out then do if in array with a user input value Commented Feb 11, 2021 at 17:24
  • Still unclear. So you set user at {0,0,0} and he inputs something like x=0,y=0, coord=5 meaning we want to change z value at [0][0][5]? Or values inside arrays represent some relation to other array indicies and it's data is some graph? We need some example how do you want to use it Commented Feb 11, 2021 at 17:40

2 Answers 2

1

You could use std::copy:

#include <algorithm>
#include <iterator>

//...

    int new_array[8];

    std::copy(std::begin(array[1][1]),
              std::end(array[1][1]),
              new_array);

Done using std::array:

#include <array>

// ...

    std::array<std::array<std::array<int, 8>, 3>, 3> array = {{
        {{
            {3,4,5},
            {3,5,7},
            {5,6,7}
        }},
        {{
            {1,3,5},
            {0,1,2,3,4,5,6,7},
            {1,5,7}
        }},
        {{
            {1,2,3},
            {1,3,7},
            {0,1,7}
        }},
    }};

    std::array<int, 8> new_array = array[1][1];

    // the same result but simpler:

    auto new_array = array[1][1];
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer that’s what I was looking for
@Mcmx385 You're welcome! You could also take a look at using std::array. If often simplifies things like this.
@Mcmx385 I added using std::array as an option. It took me some time to get the braces right I must admit. :-)
0

there is no way you can get the col or row of an array like that... if you define a primitive array then you have to navigate the cells and asign the values

int main() {
    int test[2][3][2] = {
                            {
                                {1, 2},
                                {3, 4},
                                {5, 6}
                            }, 
                            {
                                {7, 8}, 
                                {9, 10}, 
                                {11, 12}
                            }
                        };
    int z[2]; 
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            for (int k = 0; k < 2; ++k) {
                z[k] = test[i][j][k];
            }
        }
    }

    for (int k = 0; k < 2; ++k)
    {
        cout << "z[" << k << "] = " << z[k] << endl;
    }
    return 0;

1 Comment

Is there another way to define the 3D?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.