0

I need to make this function. The parameter suppose to be a random N*N array. And it should returns a (N-1)*(N-1) array. How can I do this?

int** SubMatrix(int** matrix){
    int** submatrix=new int*[size-1];
        ................................
        ................................
        return submatrix;
}

Is this code correct?

Besides, say in the main function, I already have this array

x={{1,2,3},{1,2,3},{1,2,3}};

How do I call the function?

Calling:

int y[2][2]=SubMatrix[x]

is wrong and

int** y=SubMatrix[x] 

is also wrong...

Massimiliano
  • 7,842
  • 2
  • 47
  • 62
user1819047
  • 667
  • 9
  • 18
  • 2
    Is N a known constant, or can change at runtime? In other words, does it have to handle any size input array? – davec Nov 12 '12 at 19:21
  • 2
    You should have a look at c++ basic syntax. A function should be called like `int** y = SubMatrix(x)` – kosklain Nov 12 '12 at 19:21
  • Is that function signature fixed? If you can change that signature, there are much better ways to do this. – Yakk - Adam Nevraumont Nov 12 '12 at 19:34
  • ideally the size should be variable. But I don't know how to define it even if the size is fixed. You cannot declare like int f[][]() right? – user1819047 Nov 13 '12 at 02:02

1 Answers1

0

First, there is no way to keep track of an array's size in C++ (only the amount of memory allocated). So, it is necessary that the size of the array be passed as a parameter or be global. I make this assumption in the following declaration:

int* SubMatrix(int** matrix, int size){
    int* submatrix = new int[(size - 1) * (size - 1)];
    // Do stuff
    return submatrix;
}

Second, if your matrix is square it may be better to use the declaration described above where the size of the array allocated is (N-1)*(N-1) (See Kevin Loney's answer How do I declare a 2d array in C++ using new? regarding matrix declaration on a heap); thus, the array is accessible as a matrix. Accessing cells will, however, require a bit of arithmetic:

for (int i = 0; i < size - 1; ++i)
    for (int j = 0; j < size - 1; ++j)
        submatrix[i * (size -1) + j] // Element at [i][j]

Finally, the array returned by the function is now a matrix pointer:

int* matrix = SubMatrix(matrix, size);

The dimensions of the matrix returned are (size-1)*(size-1)

With respect to your example now:

x = {{1,2,3},{1,2,3},{1,2,3}};
int* y = SubMatrix(matrix, 3);
// y is now a matrix of size (size-1)*(size-1)

If you really (really) want a two-dimensional array:

int** SubMatrix(int** matrix, int size){
    int** submatrix = new int*[size - 1];
    for (int i = 0; i < size - 1; ++i)
        submatrix[i] = new int[size - 1]
    // Do stuff
    return submatrix;
}
Community
  • 1
  • 1
Gio Borje
  • 20,314
  • 7
  • 36
  • 50
  • but this way the return is a one dimensional array right? Is there any way I can make it 2 dimensional? – user1819047 Nov 13 '12 at 02:03
  • Sure, but it isn't necessarily "2 dimensional"; rather, it's called "jagged" because you have an array of array pointers which may be of arbitrary size. I've appended the code to my answer. – Gio Borje Nov 13 '12 at 04:03