I'm a little frustrated because I do not understand why this function puts garbage instead of the result. Could someone please tell me what I'm wrong about?
I use the MinGW 5 compiler that comes in Qt. Here I put the headers ...
class matrix
{
int **array, rows, columns;
public:
//constructors that allocates memory and fills the array with 0's
matrix();
matrix(int side);
matrix(int width, int height);
int getRows() { return rows; }
int getColumns() { return columns; }
int getMatrix(); //returns the array atrib
matrix operator+(const matrix &o);
matrix operator-(const matrix &o);
matrix operator*(const matrix &o);
//set matrix methods write input data to the array
void setMatrix();
void setMatrix(int side);
void setMatrix(int width, int height);
void setRows(int num) { rows = num; }
void setColumns(int num) { columns = num; }
void print();
~matrix();
};
I'll put the implementation to clarify everything
#include "matrix.h"
#include <iostream>
//default constructor without parameters.
matrix::matrix()
{
//for allocating memory since our array is a pointer.
rows = columns = 3;
array = new int*[rows];
for (int i = 0; i < rows; i++) {
array[i] = new int[columns];
}
//just for making sure we don't have garbaje.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array[i][j] = 0;
}
}
}
//default constructor for square matrices.
matrix::matrix(int side)
{
//for allocating memory since our array is a pointer.
rows = columns = side;
array = new int*[rows];
for (int i = 0; i < rows; i++) {
array[i] = new int[columns];
}
//just for making sure we don't have garbaje.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array[i][j] = 0;
}
}
}
//default constructor for defining the rows and columns.
matrix::matrix(int width, int height)
{
//for allocating memory since our array is a pointer.
rows = width, columns = height;
array = new int*[rows];
for (int i = 0; i < rows; i++) {
array[i] = new int[columns];
}
//just for making sure we don't have garbaje.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
array[i][j] = 0;
}
}
}
//returns a copy of the array pointer.
int matrix::getMatrix()
{
int **copy = new int*[rows];
for (int i = 0; i < rows; i++) {
copy[i] = new int[columns];
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
copy[i][j] = 0;
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
copy[i][j] = array[i][j];
}
}
return **copy;
}
matrix matrix::operator+(const matrix &o)
{
matrix cpy(rows, columns);
cpy.array = getArray();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cpy.array[i][j] += o.array[i][j];
}
}
return cpy;
}
matrix matrix::operator-(const matrix &o)
{
}
matrix matrix::operator*(const matrix &o)
{
}
//set matrix as a 3 x 3
void matrix::setMatrix()
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
std::cout << "[ " << i+1 << "," << j+1 << " ]: ";
std::cin >> array[i][j];
}
}
system("cls");
}
//set matrix as a n x n
void matrix::setMatrix(int side)
{
rows = columns = side;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
std::cout << "[ " << i+1 << "," << j+1 << " ]: ";
std::cin >> array[i][j];
}
}
system("cls");
}
//set matrix as a n x m
void matrix::setMatrix(int width, int height)
{
rows = width, columns = height;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
std::cout << "[ " << i+1 << "," << j+1 << " ]: ";
std::cin >> array[i][j];
}
}
system("cls");
}
//this will print the matrix without problems.
void matrix::print()
{
std::cout << std::endl;
for (int i = 0; i < rows; i++) {
std::cout << "|";
for (int j = 0; j < columns; j++) {
std::cout << "\t" << array[i][j] ;
}
std::cout << "\t|";
std::cout << std::endl;
}
std::cout << std::endl;
}
//default destructor for deallocating memory.
matrix::~matrix()
{
for (int i = 0; i < columns; i++) {
delete [] array[i];
}
delete [] array;
}
The problem is in these two methods.
//returns a copy of the array pointer.
int matrix::getMatrix()
{
int **copy = new int*[rows];
for (int i = 0; i < rows; i++) {
copy[i] = new int[columns];
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
copy[i][j] = 0;
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
copy[i][j] = array[i][j];
}
}
return **copy;
}
matrix matrix::operator+(const matrix &o)
{
matrix cpy(rows, columns);
cpy.array = getMatrix();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cpy.array[i][j] += o.array[i][j];
}
}
return cpy;
}
and finally the main
#include "matrix.h"
int main()
{
matrix A(2);
matrix B(2);
matrix C(2);
A.setMatrix();
B.setMatrix();
C = A + B;
C.print();
return 0;
}