- #1
mathmari
Gold Member
MHB
- 5,049
- 7
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:
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;
}
}
}