0
void createArray(int a, int b, int c, int d, int array[3][3]){

    int state[3][3];

    for(int x=0;x<3;x++){
        for(int y=0;y<3;y++){

            if(x == a && y == b){
                state[x][y] = array[c][d];
            }
            else if(x == c && y == d){
                state[x][y] = array[a][b];
            }
            else{
                state[x][y] = array[x][y];
            }
        }
    }

    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            cout << state[i][j] << " ";
        }
        cout << endl;
    }

}

I have basically got this function which clones the multidimensional array that is inputed but swaps the values of the two co-ordinates (a,b) and (c,d) around. This is then outputted out to the console.

However what I would really like is for this to be returned as a multidimensional array, but I don't think this can be done?

I have looked at vectors and pointers but don't really understand them and if I use them, I will then have to change all the previous code I have written.

10
  • MCVE please. Commented Dec 7, 2014 at 20:00
  • 2
    @πάνταῥεῖ I don't think there is a need for MCVE here. The question is simple: returning a 2D array from a function. Commented Dec 7, 2014 at 20:01
  • @bolov Where's the return actually? What's the actual inputs/outputs/errors? Commented Dec 7, 2014 at 20:02
  • @bolov Thats correct! Is it possible? Commented Dec 7, 2014 at 20:03
  • @πάνταῥεῖ array is the input 2D array. state is the 2D array he wants to return. Commented Dec 7, 2014 at 20:04

3 Answers 3

3

There are multiple options.

Option 1 - pass the array to the function

void createArray(int a, int b, int c, int d, const int array[3][3], int outArray[3][3]){
...
}

Option 2 - return a reference to an array - just make sure the array does not live on the stack of the function it's returned from.

typedef int My3Times3Array[3][3];
const My3Times3Array& createArray(int a, int b, int c, int d, const int array[3][3]){
...
}

Option 3 - return a class that contains an array

struct Array
{
    int array[3][3];    
};

...

Array createArray(int a, int b, int c, int d, const int array[3][3]){
...
}

There's also std::vector, std::array, or boost::matrix, but since you mentioned you aren't comfortable with pointers yet, I'd save template classes for later.

Sign up to request clarification or add additional context in comments.

Comments

3

When you want to return a non conventional data type (int, char etc), the best way of doing it is by making your very own one.

struct mat3
{
    int myArray[3][3];    
};


mat3 createArray(int a, int b, int c, int d, int array[3][3]){

mat3 state;

for(int x=0;x<3;x++){
    for(int y=0;y<3;y++){

        if(x == a && y == b){
            state.myArray[x][y] = array[c][d];
        }
        else if(x == c && y == d){
            state.myArray[x][y] = array[a][b];
        }
        else{
            state.myArray[x][y] = array[x][y];
        }
    }
}

for(int i=0;i<3;i++){
    for(int j=0;j<3;j++){
        cout << state.myArray[i][j] << " ";
    }
    cout << endl;
    return state;
}

}

I have looked at vectors and pointers but don't really understand them and if I use them, I will then have to change all the previous code I have written

I suggest you study pointers further, they are so essential that you are already using them without knowing it.

2 Comments

"by making your very own" - looks like a typo. What does this mean?
Make your own data type. In this case you are making a type called 'mat3'.
0

You can dynamically allocate a two-dimensional array and return pointer to it. For example

typedef int ( *STATE )[3];

STATE createArray(int a, int b, int c, int d, int array[3][3]){

    STATE state = new int[3][3];

    //...

    return state;
}

Or

int ( *createArray(int a, int b, int c, int d, int array[3][3] ) )[3]{

    int ( * state )[3] = new int[3][3];

    //...

    return state;
}

In fact the same result you can get if you will use vectors. For example

#include <vector>
#include <array>

//...
std::vector<std::array<int, 3>> createArray(int a, int b, int c, int d, int array[3][3]){

    std::vector<std::array<int, 3>> state( 3 );

    //...

    return state;
}

4 Comments

In c++, using pointers is usually not advised. Also, std::vector<std::array<int, 3>> is sufficiently different from int[3][3] to make some additional explanation necessary.
Which is stupid and non-sense waste of program time, as the array size is already known.
@Jako I showed the general approach how arrays are allocated and how to work with pointers. It is not a stupid approach as you think. It is a flexible approach that will work with arrays of any size.
@VladfromMoscow Would it be better to use typedef int STATE[3] and have the function return STATE*? I guess it's functionally equivalent (not sure) but more readable.

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.