# Matrix33 in C++

• C/C++
• mathmari
In summary, the code asks for a class named Matrix33 that has its dimensions fixed at 3x3. The class is created and memset is used to initialise it to 0. Next, the program asks for an input of 5 elements and each element is read and inserted into the matrix. Finally the matrix is returned.f

#### mathmari

Gold Member
MHB
Hey! :giggle:

I am looking the following exercise about C++.

(a) Create a class Matrix33 that describes a 2-dimensional $3\times3$ array of integers. At the construction of an element of the class allpositions of the array are initially zero (zero matrix). Implement methods to read and write values at the positions of tha array according to the line and column of each position.
(b) Implement a method IsDiagonal that checks if the array is diagonal.
(c) Support adding arrays by overloading the + operator. With data elements Matrix33 m1, m2; the array Matrix33 m3 = m1 + m2; will contain the sum of the arrays m1 and m2.
(d) Implement methods for reading array data from input and printing array data at output. Write a program that creates 2 elements of class Matrix33, reads their data from the input, adds them and prints the result of the addition to the output.

I searched online for some notes. Is the code as follows? :unsure:

Code:
#include <iostream>
#include <cstdlib>
using namespace std;

class Matrix
{
public:
Matrix(); //Default constructor
Matrix(int m, int n); //Main constructor
void setVal(int m, int n); //Method to set the val of [i,j]th-entry

private:
int m, n;
int **p;
void allocArray()
{
p = new int*[m];
for(int i = 0; i < m; i++)
{
p[i] = new int[n];
}
}
};

Matrix::Matrix() : Matrix(1,1) {}
Matrix::Matrix(int m, int n)
{
allocArray();
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
p[i][j] = 0;
}
}
}

Hey mathmari!

That works more or less.
However, the question asks for a class named Matrix33, which has its dimensions fixed to 3x3.
And the method setVal is missing a parameter to set the actual value, and it's also missing an implementation.
We're also missing a method to read a value in the matrix.
Furthermore, if we do new int[n](), then everything gets initialized to 0, so we don't have to do that explicitly then.
Or alternatively, we could just have an int a as data member, although we need to memset it to 0 then. Furthermore, if we do new int[n](), then everything gets initialized to 0, so we don't have to do that explicitly then.
Or alternatively, we could just have an int a as data member, although we need to memset it to 0 then. So for the part "Create a class Matrix33 that describes a 2-dimensional 3×3 array of integers. At the construction of an element of the class allpositions of the array are initially zero (zero matrix). " do we do the following?

Code:
#include <iostream>

class Matrix33
{
int m;
memset(m, 0, sizeof m);
}

Or did you mean something else? :unsure:

So for the part "Create a class Matrix33 that describes a 2-dimensional 3×3 array of integers. At the construction of an element of the class allpositions of the array are initially zero (zero matrix). " do we do the following?
That wouldn't compile.
Code:
#include <cstring>

class Matrix33
{
public:
Matrix33() {
std::memset(m, 0, sizeof m);
}
private:
int m;
};
And actually, we can do:
Code:
class Matrix33
{
public:
Matrix33() : m() {}
private:
int m;
};
for the same effect. That wouldn't compile.
Code:
#include <cstring>

class Matrix33
{
public:
Matrix33() {
std::memset(m, 0, sizeof m);
}
private:
int m;
};
And actually, we can do:
Code:
class Matrix33
{
public:
Matrix33() : m() {}
private:
int m;
};
for the same effect. Ok!

So do we have at question (a) the following?

Code:
#include <cstring>
using namespace std;

class Matrix33
{
public:
Matrix33() : m() {}
private:
int m;
};

int main()
{
int m, i, j, elem;
cout<<"Enter the Array Elements: ";
for(i=0; i<5; i++){
for(j=0; j<5; j++){
cin>>m[i][j];
cout<<"\nEnter Element to Insert: ";
cin>>elem;
m[i][j] = elem;
}
}
cout<<"\nThe New Array is:\n";
for(i=0; i<6; i++)
cout<<m[i][j]<<"  ";
cout<<endl;
return 0;
}

:unsure:

So do we have at question (a) the following?

Did you try to compile it?
There are a couple of problems with it.
We can try for instance with https://wandbox.org. Furthermore, the code you have in main() is not actually using the class Matrix33 is it? Did you try to compile it?
There are a couple of problems with it.
We can try for instance with https://wandbox.org. Furthermore, the code you have in main() is not actually using the class Matrix33 is it? I made some changes but I still get errors.

Code:
#include <iostream>
#include <cstring>
using namespace std;

class Matrix33
{
public:
Matrix33() : m() {}
private:
int m;
};

int main()
{
Matrix33 m;
int i, j, elem;
cout<<"Enter the Array Elements: ";
for(i=0; i<4; i++){
for(j=0; j<4; j++){
cin>>m[i][j];
cout<<"\nEnter Element to Insert: ";
cin>>elem;
m[i][j] = elem;
}
}
cout<<"\nThe New Array is:\n";
for(i=0; i<4; i++){
for(j=0; j<4; j++){
cout<<m[i][j]<<"  ";
}
}
return 0;
}

What else do we have to change? The main is not correct. Normally from the exercise we have to write this in a functionand not in the main, write? :unsure:

I made some changes but I still get errors.

What else do we have to change? The main is not correct. Normally from the exercise we have to write this in a function and not in the main, write?

Matrix33 is already a 3x3 matrix, so we shouldn't write Matrix33 m but just Matrix33 matrix. We need functions in the class to get and set values.
Currently we only have the so called special constructor function Matrix33().
We need for instance int get(int i, int j) { return m[i][j]; } inside the class.
Then we can get the value at position (i, j) with matrix.get(i, j).
Note that we cannot use matrix.m[i][j] since m is private. The code to read a matrix from stdin is actually part of (d).
We can keep the code in main() for now to verify that we properly implemented (a).
But it should be replaced when we get to (d). Matrix33 is already a 3x3 matrix, so we shouldn't write Matrix33 m but just Matrix33 matrix. We need functions in the class to get and set values.
Currently we only have the so called special constructor function Matrix33().
We need for instance int get(int i, int j) { return m[i][j]; } inside the class.
Then we can get the value at position (i, j) with matrix.get(i, j).
Note that we cannot use matrix.m[i][j] since m is private. The code to read a matrix from stdin is actually part of (d).
We can keep the code in main() for now to verify that we properly implemented (a).
But it should be replaced when we get to (d). So do we not insert the elements into the matrix with:

Code:
    for(i=0; i<4; i++){
for(j=0; j<4; j++){
cin>>matrix[i][j];
cout<<"\nEnter Element to Insert: ";
cin>>elem;
matrix[i][j] = elem;
}
}

:unsure"

So do we not insert the elements into the matrix with:
No. The object matrix does not support indexing with matrix[i][j]. (Shake)

Btw, currently we are first trying to get matrix[i][j], which is not possible.
And then we get elem, which we subsequently try to assign to matrix[i][j].
So we're trying to read every element twice. Instead we should just read elem and use for instance matrix.set(i, j, elem) to put it into the matrix. Instead we should just read elem and use for instance matrix.set(i, j, elem) to put it into the matrix. Let's suppose for now that "elem = i+j" then dowe have the following?

Code:
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class Matrix33
{
private:
int m;

public:
Matrix33(){
memset(m, 0, sizeof m);
}

int get(int i, int j) { return m[i][j]; }

void SetCell(int i, int j, int val)
{
m[i][j] = val;
}

void print()
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
cout << m[i][j] << "  ";
}
cout << endl;
}
}

};

int main()
{
int i,j, elem;
Matrix33 matrix;
for (i=0; i<3; i++){
for (j=0; j<3; j++){
elem=i+j;
matrix.SetCell(i,j,elem);
}
}
matrix.print();
return 0;
}

If this is correct then we have to define the correct "elem" and write the main part inside the Class for question (a), or not?

:unsure:

Last edited by a moderator:
Let's suppose for now that "elem = i+j" then dowe have the following?

If this is correct then we have to define the correct "elem" and write the main part inside the Class for question (a), or not?
It is fine for (a) as it is now. (Nod)

Just some nitpicks.
The method get() should have a name that matches SetCell().
For (a) we need that "getter" method, so we should call it from main() as well to verify that it works as intended. The method get() should have a name that matches SetCell().

What do you mean? You mean that they should have a similar name? :unsure:

For (a) we need that "getter" method, so we should call it from main() as well to verify that it works as intended. Do you mean that what I have written in the main should be in a separate function? :unsure:

What do you mean? You mean that they should have a similar name?

Do you mean that what I have written in the main should be in a separate function?
Yes. If we have SetCell() to set the value of a cell, then we should have GetCell() to get the value of a cell. No. The main() function is fine. It's just that there should be a call to matrix.GetCell() somewhere to verify that it's there and that it works. (a) asks for such a method after all. Yes. If we have SetCell() to set the value of a cell, then we should have GetCell() to get the value of a cell. No. The main() function is fine. It's just that there should be a call to matrix.GetCell() somewhere to verify that it's there and that it works. (a) asks for such a method after all. So do we use matrix.GetCell() to print the matrix instead of the function I defined above?

Sodowe do it as follows?

Code:
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class Matrix33
{
private:
int m;

public:
Matrix33(){
memset(m, 0, sizeof m);
}

int GetCell(int i, int j) { return m[i][j]; }

void SetCell(int i, int j, int val)
{
m[i][j] = val;
}

};

int main()
{
int i,j, elem;
Matrix33 matrix;
for (i=0; i<3; i++){
for (j=0; j<3; j++){
elem=i+j;
matrix.SetCell(i,j,elem);
cout << matrix.GetCell(i,j) << "  ";
}
cout << endl;
}
return 0;
}

:unsure:

So do we use matrix.GetCell() to print the matrix instead of the function I defined above?
Yep. That works. (Nod)

Yep. That works. (Nod)

So all these methods to read and fill the matrix are inside the class Matrix33, right?
How exactly are the elements with which we fill the matrix? Does it mean that that in line $i$ and column $j$ the element that is filled is $ij$ ? :unsure:

The method of part (b) is also inside class Matrix33 ? Or is this a separate method? :unsure:

So all these methods to read and fill the matrix are inside the class Matrix33, right?
How exactly are the elements with which we fill the matrix? Does it mean that that in line $i$ and column $j$ the element that is filled is $ij$ ?

The method of part (b) is also inside class Matrix33 ? Or is this a separate method?
The element in line $i$ and column $j$ is filled with $i+j$.
When we run the program that should be visible. The word "method" means that it is a function inside a class.
So yes, the method of (b) should be inside class Matrix33. The element in line $i$ and column $j$ is filled with $i+j$.
When we run the program that should be visible. Yes, I wrote in the program to fill the (i,j)-cell with $i+j$, but is this supposed to be like this by the exercise statement? It says "at the positions of tha array according to the line and column of each position.". :unsure:

Yes, I wrote in the program to fill the (i,j)-cell with $i+j$, but is this supposed to be like this by the exercise statement? It says "at the positions of tha array according to the line and column of each position.".
It means that we need to have a method that allows us to set the value of a cell at a certain line and column.
The method SetCell() does that.
How it is implemented inside the class is actually arbritrary, as long as we can get the same element back with GetCell() with the same line and column.
And we can. It means that we need to have a method that allows us to set the value of a cell at a certain line and column.
The method SetCell() does that.
How it is implemented inside the class is actually arbritrary, as long as we can get the same element back with GetCell() with the same line and column.
And we can. Do we not havw to ask the user for the enrties of the matrix in (a) ? :unsure:

If yes, is the code as follows?

Code:
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

class Matrix33
{
private:
int m;

public:
Matrix33(){
memset(m, 0, sizeof m);
}

int GetCell(int i, int j) { return m[i][j]; }

void SetCell(int i, int j, int val)
{
m[i][j] = val;
}

};

int main()
{
int i,j, elem;
Matrix33 matrix;
for (i=0; i<3; i++){
for (j=0; j<3; j++){
cout<<"Give the (i,j)-element";
cin>>elem;
matrix.SetCell(i,j,elem);
cout << matrix.GetCell(i,j) << "  ";
}
cout << endl;
}
return 0;
}

:unsure:

Question (a) does not ask for that, does it? Question (a) does not ask for that, does it? Ah part (d) asks for it. (Malthe)

I tried for part (a) till (c) the following:

Code:
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

//Question (a)
class Matrix33
{
private:
int m;

public:
Matrix33(){
memset(m, 0, sizeof m);
}

int GetCell(int i, int j) { return m[i][j]; }

void SetCell(int i, int j, int val)
{
m[i][j] = val;
}

//Question (b)
bool isDiagonal()
{
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if ((i != j) && (m[i][j] != 0)){
return false;
}
}
}
return true;
}

//Question (c)
{
int i,j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
m[i][j] = m1[i][j] + m2[i][j];
cout << "Sum of matrices\n";
for (i = 0; i < 3; i++)
{    for (j = 0; j < 3; j++)
cout << m[i][j] << "  ";
cout << "\n";
}
}
};

Do we have define m1 and m2 at the beginning at "private" ? :unsure:

//Question (c)
Do we have define m1 and m2 at the beginning at "private" ?

Nope. (Shake)

We should define Matrix33 m1, m2 in main().
And we should make sure that we can do Matrix33 m3 = m1 + m2 in main() as well.
We can do so by implementing a function named operator+(). We should define Matrix33 m1, m2 in main().
And we should make sure that we can do Matrix33 m3 = m1 + m2 in main() as well.
We can do so by implementing a function named operator+(). I have done the following for the whole exercise:

Code:
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

//Question (a)
class Matrix33
{
private:
int m;

public:
Matrix33(){
memset(m, 0, sizeof m);
}

int GetCell(int i, int j) { return m[i][j]; }

void SetCell(int i, int j, int val)
{
m[i][j] = val;
}

//Question (b)
bool isDiagonal()
{
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if ((i != j) && (m[i][j] != 0)){
return false;
}
}
}
return true;
}

//Question (c)
int OperatorPlus(int x, int y)
{
int z = x + y;
return z;
}

};

//Question (d)
int main()
{
int i,j, elem,res;
Matrix33 m1,m2,m;
cout << "Enter the elements of matrix m1: ";
for (i=0; i<3; i++){
for (j=0; j<3; j++){
cin>>elem;
m1.SetCell(i,j,elem);
cout << m1.GetCell(i,j) << "  ";
}
cout << endl;
}

cout << "Enter the elements of matrix m2: ";
for (i=0; i<3; i++){
for (j=0; j<3; j++){
cin>>elem;
m2.SetCell(i,j,elem);
cout << m2.GetCell(i,j) << "  ";
}
cout << endl;
}

for (i = 0; i < 3; i++){
for (j = 0; j < 3; j++){
res = OperatorPlus(m1.GetCell(i,j), m2.GetCell(i,j));
m.SetCell(i,j,res);
}
}
cout << "Sum of matrices\n";
for (i = 0; i < 3; i++){
for (j = 0; j < 3; j++){
cout << m[i][j] << "  ";
}
cout << "\n";
}
return 0;
}

I still get some errors. I get:

Code:
prog.cc: In function 'int main()':
prog.cc:75:19: error: 'OperatorPlus' was not declared in this scope
75 |             res = OperatorPlus(m1.GetCell(i,j), m2.GetCell(i,j));
|                   ^~~~~~~~~~~~
prog.cc:82:22: error: no match for 'operator[]' (operand types are 'Matrix33' and 'int')
82 |             cout << m[i][j] << "  ";
|                      ^

What does these errors mean? Is the rest correct at the code that I wrote? :unsure:

Is the rest correct at the code that I wrote?
I don't see the code mentioned in (c). (Shake)

And (d) asks for methods to read and write the matrix. I don't see such methods. (Sweating)

What does these errors mean?

Code:
prog.cc: In function 'int main()':
prog.cc:75:19: error: 'OperatorPlus' was not declared in this scope
75 |             res = OperatorPlus(m1.GetCell(i,j), m2.GetCell(i,j));
|                   ^~~~~~~~~~~~
prog.cc:82:22: error: no match for 'operator[]' (operand types are 'Matrix33' and 'int')
82 |             cout << m[i][j] << "  ";
|                      ^
There is no function named 'OperatorPlus'.
Instead Matrix33 has a method with that name.
To call it, we would need for instance int result = matrix.OperatorPlus(2, 3). 'm' is of type Matrix33. It means that we cannot index it with [i][j]. 