Custom order of a 2d array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class matrix
{
    const int x;
    const int y;
    int value[x][y];
    public:
    matrix(int ,int );
};

matrix::matrix(int a,int b)
{
    x=a;
    y=b;
}

I want to declare the value array with the user inputs of the order of the matrix.What should I do?
use new & delete[] and make value into a pointer, then allocate an int array of size x*y to value
Last edited on
Please elaborate , I am still very much a beginner.
You should read this: http://cplusplus.com/doc/tutorial/dynamic/
But if you havent learnt much about pointers, you shouldnt be doing this i think, because you probably wouldnt understand(pointers are probably the most annoying but useful things youll meet in c++)


Alright, heres an example;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class matrix
{
    const int x;
    const int y;
    int** value;
    public:
    matrix(int ,int );
};

matrix::matrix(int a,int b)
{
    x=a;
    y=b;
    value = new int*[a];
    for(int i = 0; i < a; i++)
    {
        value[i] = new int[b];
    }
}
Last edited on
Topic archived. No new replies allowed.