# Thread: Arrays of user-defined size?

1. ## Arrays of user-defined size?

In my C++ class I've been writing C++ code based on matrix manipulation.

My first program was simple enough, using a class to multiply a 2x2 matrix by a 2d column vector and display the results.

Today I wrote a program using an array and a class to find the determinant of a user-inputed 2x2 matrix.

BUT I want the user to be able to define the size of the matrix - the array.

I tried this:

int a;
int b;
int matrix[a][b];

But, not surprisingly, that doesn't work.

What needs to be done?

Thanks.

2. Oh, just had an idea -

Would it be better to make the array 10x10 or so, and then have the user choose a size and only put in #'s for the size the user wants? So when I'm using a for loop it will use the variables representing the size the user chose and will leave the rest of the values as 0?

3. To define the size of an array at runtime I always end up using malloc ( or one of its equivalents ) to get the size of memory needed to create an array the size a user wants. You could try that

5. Code:
```int x;
int y;

int* Matrix = new int[x * y];

...

delete[] Matrix;```
or...
Code:
```int x;
int y;

int** Matrix = new int*[y]; //<-- Is this true???
for(int i = 0; i < y; i++) Matrix[i] = new int[x];

...

for(int i = 0; i < y; i++) delete[] Matrix[i];
delete[] Matrix;```
I prefer the first one. It is actually a 1D array, but can be handled as a 2D. The last example (not even sure if it's correct) is really easy to do something wrong at...

6. Well since I don't fully understand the proposed code (and I don't like using code unless I completely understand it), I just wrote this and it works fine -

Code:
```// This program will make a matrix of user defined size and add it to another matrix of the same size.
// By Will Herrick
// last updated 9-12-02

#include <iostream.h>
#include <stdlib.h>
class matrix
{
public:
matrix();   // default constructor - fills the matrix with 0's
void ShowMatrix();
int a;
int b;
int x;
int y;
int matrix_ar[8][8]; // 8x8 array
};
matrix::matrix()
{
a = 5;
b = 6;
for(x = 0; x<8 ; x++)
{
for(y = 0; y<8; y++)
{
matrix_ar[x][y]=0;
}
}
}

void matrix::ShowMatrix()
{
for(x = 0; x<a; x++)
{
cout << "| ";
for(y = 0; y<b; y++)
{
cout << matrix_ar[x][y];
}
cout << " |\n";
}
}

int main(int argc, char *argv[])
{
matrix matrix_A;
matrix_A.matrix_ar[0][1] = 5;
matrix_A.ShowMatrix();
system("PAUSE");
return 0;
}```

7. Originally posted by Salem
> int** Matrix = new int*[y]; //<-- Is this true???
> for(int i = 0; i < y; i++) Matrix[i] = new int[x];
Yes, that's the way to do it
But the result is accessed as [y][x] not [x][y]

> int matrix_ar[8][8]; // 8x8 array
Also a good idea providing the max size of the array isn't too large, and you don't have too many matrix objects in your program.
Yea, when I actually go and write the full-fledged program I will determine a maximum size (I don't see any reason why anything bigger than 5x5 or 6x6 would be needed), and put in an if-statement to make sure the user doesn't put in anything too large.

On another note, how do I create objects on-the-fly?

For example:
"Would you like to create a new matrix? Y/N"

would this have to do with the "new" keyword? (which I USED to know how to use, but I stopped programming for 3 months - and I never programmed much anyways )

8. Originally posted by Salem
> would this have to do with the "new" keyword?
Yeah, read the example posted above

Just make the int **arr a member of the class (your data members of your class should be private by the way), and perhaps have a constructor which takes the x,y size parameters
Hmm good idea about the constructor there!

And yea, I know that those should be private - but I havn't had the courage to jump back into pointers just yet. (like I said, I returned from my minimal experience in coding after 3 months!)