Creating random matrix in constructor - c++

So, i'm making program that will multiply and adds matrices. I created class matrix with constructor:
class Matrix
{
private:
int row;
int column;
public:
int getRows()
{
return row;
}
int getColumns()
{
return column;
}
void print_matrix()
{
for(int i = 0; i < row; i++)
{
cout<<"\n";
for(int j = 0; j < column; j++)
cout<<matrix[i][j]<<"\t";
}
}
Matrix(int row, int column);
};
Matrix::Matrix(int row, int column)
{
this->row = row;
this->column = column;
int** matrix = new int*[row];
for(int i = 0; i < row; i++)
matrix[i] = new int[column];
for(int i = 0; i < row; i++)
for(int j = 0; j < column; j++)
matrix[i][j] = (i+j)*2*3/4;
for(int i = 0; i < row; i++)
delete[] matrix[i];
delete[] matrix;
}
at this point i dont know how to print my matrix, or work with him. In method "print_matrix" my compilator said that "'matrix was not declarated in this scope'".

You are deleting the allocated matrix in the constructor itself. To make it work,
add a member int**matrix in the class. And then do the allocation in constructor and delete it in destructor.
To add it further, even if you didn't delete the matrix then you would end up with the same error. The variable you have declared and used in the constructor - it's lifetime ends when the constructor ends.
Now instead of using pointer and allocating to it, why not use vector?
In that case you will keep std::vector v; as a member variable in the class and then resize it in constructor.
v.resize(row);
for (int i = 0; i < row; ++i)
v[i].resize(col);

Related

How to overload assignment operator and create copy constructor for 2D Dynamic Arrays?

I have a flight class which has bool** property in it.
class Flight {
public:
int flightNumber;
int row;
int seat;
bool** seatReserv;
Flight();
Flight(int fly,int rw,int st);
~Flight();
Flight(const Flight& rightVal);
Flight& operator=(const Flight& rightVal);
};
I tried to write my overloaded assignment operator as below, however i realised that even i change my row and seat property, i am not changing the 2D arrays [row] and [seat] size.
Flight& Flight::operator=(const Flight& rightVal){
if(this != &rightVal){
for(int i = 0; i < row; i++){
delete[] this->seatReserv[i];
}
delete [] this->seatReserv;
flightNumber = rightVal.flightNumber;
row = rightVal.row;
seat = rightVal.seat;
this -> seatReserv = new bool*[row];
for(int i = 0; i < row; i++){
seatReserv[i] = new bool[seat];
}
for(int i = 0; i < row; i++){
for(int j = 0; j < seat; j++){
seatReserv[i][j] = rightVal.seatReserv[i][j];
}
}
}
return *this;
}
Furthemore i encountered the same problem in my copy constructor below, so the question is how can i write copy constructor and overload my assignment operator for my class?
Flight::Flight(const Flight& rightVal){
flightNumber = rightVal.flightNumber;
row = rightVal.row;
seat = rightVal.seat;
for(int i = 0; i < row; i++){
for(int j = 0; j < seat; j++){
seatReserv[i][j] = rightVal.seatReserv[i][j];
}
}
}
EDIT:I can not use vectors this is for a homework assignment so vector using is forbidden.
This would probably work as copy constructor:
Flight::Flight(const Flight &rightVal){
this->flightNumber = rightVal.flightNumber;
this->row = rightVal.row;
this->seat = rightVal.seat;
this->seatReserv = new bool*[row];
for (int i = 0; i < this->row; i++){
this->seatReserv[i] = new bool[seat];
for (int j = 0; j < this->seat; j++){
this->seatReserv[i][j] = rightVal.seatReserv[i][j];
}
}
}
For the = operator you could probably use the same thing as above, but don't forget to delete what was there before using the destructor (this is to avoid memory leaks).
Flight::~Flight(){
if (seatReserv == nullptr) return; //this means that at the beginning of the class seatReserv should be set to nullptr.
for (int i = 0; i < this->row; i++){
delete[] seatReserv[i];
}
delete[] seatReserv;
}
I also suggest that instead of using a "2D array", you use one single sequence of bools, with length row * seat. To access one at a specific place you'd do this:
i * row + j, instead of this array[i][j].
This is faster when allocating and freeing and it's more efficient for your CPU cache.
This
this -> seatReserv = new bool*[row];
for(int i = 0; i < row; i++){
seatReserv[i] = new bool[i];
}
should be this
this -> seatReserv = new bool*[row];
for(int i = 0; i < row; i++){
seatReserv[i] = new bool[seat];
}
The little details matter.
Your copy constructor doesn't allocate any memory for your 2D array, so that's a problem.

How can I dynamically allocate matrix that is made from triple pointer to a class?

So this is my class
class Map {
Field*** f;
int rows;
int columns;
};
How can I make a matrix of pointers to the class Field?
I tried this but it doesnt work.
Map(int rows_, int columns_) : rows(rows_), columns(columns_) {
f = new Field*[][];
*f = new Field*[rows];
for (int i = 0; i < rows; i++) {
*f[i] = new Field[columns];
}
}
You may consider rethinking about what you are doing. A flattened one dimensional array is a better choice.
If you still want to a *** pointer here is how you can make one:
f = new Field**[Row];
for(int i =0; i<Row; i++){
f[i] = new Field*[Col];
for(int j =0; j<Col; j++){
f[i][j] = new Field[H];
for(int k = 0; k<H;k++){
f3D[i][j][k] = 0;
}
}
}
Don't forget to free it.

I don't understand what's wrong

Here is a my own Matrix class:
class Matrix {
private:
int row; // rows
int col; // columns
double** matrix; // pointer to this matrix
public:
Matrix();
Matrix(int row, int col);//creating matrix based on two params
Matrix(double** matx, int row, int col); //Here is a problem
Matrix(Matrix& N);//copy constructor
~Matrix();
//first, second and fourth constructors works good
int getRow();
int getCol();
void changeSize(int row, int col);
void randValues();
void writeValues();
};
Here is a body of constructor, This constructor need take a exists matrix as parameter and create a new one based on the matrix provided (it's NOT a copy constructor)
Matrix::Matrix(double** matx, int row, int col){
//allocated memory for new matrix
matrix = new double*[row];
for (int i = 0; i < row; i++){
matrix[i] = new double[col];
}
// //copy values to new matrix
for(int i=0; i<row; i++)
{
for(int k = 0; k < col; k++)
{
matrix[i][k] = matx[i][k];
}
}
};
int main(){
double** test = new double *[5];
for(int i = 0; i < 5; i++){
test[i] = new double;
}
for(int i = 0; i < 5; i++){
for(int k = 0; k < 5; k++){
test[i][k] = 0.11;
cout << test[i][k] << " ";//just for make me sure if is ok
}
cout << "\n";
}
Matrix *matx = new Matrix(test,5,5);
matx->writeValues();
return 0;
}
And when I run a program they write on console lots of zeros values and few garbage and of the end is: Process returned -1073741819 (0xC0000005) execution time : 2.162 s

Creating an n by n grid, where each element is a pointer to an object

class ClassName {
A* grid[][];
}
I'm getting that "declaration of ‘grid’ as multidimensional array must have bounds for all dimensions except the first" error. The issue is I won't know the dimensions until I run the program, where the size is one of the arguments.
The instructions state that each element of the grid should be an A* -- i.e., a pointer to an object of type A.
How can I do this?
You could opt to define grid this way: A*** grid;. Of course, you'd have to dynamically allocate memory with this setup:
class ClassName {
A*** grid;
int nCols, nRows;
public:
ClassName(int cols, int rows) {
nCols = cols;
nRows = rows;
grid = new A**[nCols];
for (int i = 0; i < nCols; i++) {
grid[i] = new A*[nRows];
for (int j = 0; j < nRows; j++) {
grid[i][j] = nullptr;
}
}
}
~ClassName() {
for (int i = 0; i < nCols; i++) {
delete[] grid[i];
}
delete[] grid;
}
};

How to delete this 2D Array in C++

I've absolutely no idea why my delete codes inside the destructor won't be able to functionally well. I hope u guys can help me for this.
Thank you so much!
class Array2D
{
public:
Array2D();
Array2D(int, int);
~Array2D();
private:
int row;
int col;
int **p;
};
Array2D::Array2D()
{
// Default Constructor
}
Array2D::Array2D(int rows, int cols)
{
this -> row = rows;
this -> col = cols;
p = new int* [row];
for(int i=0; i< row; i++)
p[i] = new int[col];
// Fill the 2D array
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
{
p[i][j] = rand () % 100;
}
}
Array2D::~Array2D()
{
// I'm using this way to delete my 2D array.
// however, it won't work!
for (int i = 0; i < row; i++)
{
delete[]p[i];
}
delete[]p;
}
You are not initializing anything in your default constructor. That means that the destructor will go mad on a default constructed object. You are also not disabling the copy constructor, which is not functioning with your class, because if you have copied an object, it will try to delete the same table twice. Change it as follows, for example
class Array2D
{
public:
Array2D();
Array2D(int, int);
~Array2D();
private:
int row;
int col;
int **p;
void initialize(int rows, int cols);
// disable copy functions (make private so they cannot
// be used from outside).
Array2D(Array2D const&);
Array2D &operator=(Array2D const&);
};
Array2D::Array2D()
{
initialize(0, 0);
}
Array2D::Array2D(int rows, int cols)
{
initialize(rows, cols);
}
void Array2D::initialize(int rows, int cols) {
this -> row = rows;
this -> col = cols;
p = new int* [row];
for(int i=0; i< row; i++)
p[i] = new int[col];
// Fill the 2D array
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
{
p[i][j] = rand () % 100;
}
}