resizeable 2-dimen array

i'm trying to make a matrix class and i want to use 2 dimensional arrays for them. i know you can use vectors(easy way) but is there a way to re-size the array?? with out accidentally over writing something else?

i know it has something to do with new and delete but i'm not familiar with them

i can do it with one dimensional arrays but not 2
Last edited on
Well, you could create another array with the "new" size, then copy all the data over, then set that array as the "real" array.
Theres no way to change the size of the current array but firedraco's way will work
I may be wrong in saying this - but is this possible at all?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Include Class Header and this files header here...
MatrixClass* CreateMatrix(int &arraySizeOne, int &arraySizeTwo)
{
	std::cin >> arraySizeOne, arraySizeTwo;

	MatrixClass* myCreatedMatrix = new MatrixClass[arraySizeOne][arraySizeTwo];

	return myCreatedMatrix;
}
//New File for delete function
void DeleteMatrix(MatrixClass* myCreatedmatrix, int arraySizeOne, int arraySizeTwo)
{
		delete[][] myCreatedMatrix;	
}


Probably not correct syntax - but it's kind of possible like this isn't it?

For the top bit using the class like method - in another file or somewhere else on the page. You would have another pointer thats pointing to the real class and make it edit that?
Last edited on
So in another file it might be something like...
1
2
3
4
5
6
7
8
9
#include "class.h"
#include "create_matrix.h"

void startup()
{
	Matrix *pMatrix;
	int sizeOne, sizeTwo;
	pMatrix = CreateMatrix(sizeOne, sizeTwo);
}

Let me know if you manage to get what you want working :)
If you wanna go back to the old C style, you can use realloc() :)
Good old realloc() - that is another way of doing it which i still think is pretty good.
i still cant do it

declaration
 
float* mat;



this works:
1
2
3
 r = numRow();
 c = numCol();
 mat = new float[r][c]; 

this does but not what i need
1
2
3
 r = numRow();
 c = numCol();
 mat = new float[r*c]; 


also i'm getting an
invalid types float[int] for array subscript
error in this:
 
 mat[r][c] = num;



Last edited on
Hmm, now i'm a little confused - mat = new float[r][c]; is a 2d array which i gather is what you want. But your other one is a single array its just 1 long line. As lets say your r = 10 and c = 10 - then all thats doing is making a long single array with an x-axis value of 100. Doing it the way you have at the top using "new float[r][c]" would be making a y-axis value of 10 and an x-axis value of 10 as well.

And as for mat[r][c] = num; what exactly do you want to happen here?
to understand better how to use new float[r][c] and new float[r*c] read this: http://www.cplusplus.com/doc/tutorial/arrays.html#multidimensional_arrays
I know how multidimensional arrays work, i was just saying that works, but not what i want.

and i do want an 2 dimensional array. when i do mat[r][c] = num; i mean to change that specific spot([r][c]) in the array to be = to "num" sent in.

i also looked here for info:
http://www.cplusplus.com/doc/tutorial/dynamic.html
mat[r][c] = num;

Should work fine...(assuming you aren't accessing outside of the bounds of the array). What is the value of "num"?
num is a float and a paramater in member function changeEntery(int r, int c, float num)
Hmmm...then it should work fine...are you getting an error or something.
i am getting an error
'c' cannot appear in a constant-expression
for:
mat = new float[r][c];

and
invalid types 'float[int]' for array subscript
for:
1
2
 if (r <= numRow() && c <= numCol())
    mat[r][c] = num; 


and
invalid types 'float[int]' for array subscript
for:
cout << mat[i][j];

Last edited on
what does you for loop look like for your - displaying of "cout << mat[i][j]; ?
Dynamic 2d array.

1
2
3
4
5
6
7
8
9
10
11
12
13
int **array = new int*[rows];
for (int i = 0; i < rows; ++i) 
 array[i] = new int[cols];

array[row][col] = num;

cout << "Value = " << array[row][col] << endl;

// Free memory
for (int i = 0; i < rows; ++i) {
 delete [] array[i];
}
 delete [] array;

Beautiful - that has even helped me heh
ohh i get. smart. thanks


There are other ways to do it. But they either involve overloading operators, or using a vector of vectors.

The way I've illustrated is for people who don't mind working with pointers and are confident in their memory management abilities.
Topic archived. No new replies allowed.