CS201p Assignments solution spring 2023 👇👇👇👇👇👇
// MAS All Rounder
#include<iostream>
#define ROW 3
#define COL 3
using namespace std;
class AddMatrix{
private:
int matrix[ROW][COL];
public:
AddMatrix(){
for(int i=0;i<ROW;i++){
for(int j=0;j<COL;j++){
matrix[i][j]=0;
}
}
}
AddMatrix(int array[]){
setMatrix(array);
}
void setMatrix(int array []){
for(int i=0;i<ROW;i++){
for(int j=0;j<COL;j++){
matrix[i][j]=array[COL*i+j];
}
}
}
AddMatrix operator + (AddMatrix & x){
AddMatrix y;
for(int i=0;i<ROW;i++){
for(int j=0;j<COL;j++){
y.matrix[i][j]=matrix[i][j]+x.matrix[i][j];
}
}
return y;
}
friend AddMatrix Multiply(AddMatrix & x, int id){
AddMatrix y;
for(int i=0;i<ROW;i++){
for(int j=0; j<COL;j++){
y.matrix[i][j]=id*x.matrix[i][j];
}
}
return y;
}
void display(AddMatrix & result){
for(int i=0;i<ROW;i++){
for(int j=0;j<COL;j++){
cout<<result.matrix[i][j]<<"\t";
}
cout<<endl;
}
}
};
int main(){
int array[]={0,1,2,3,0,3,1,2,0};
AddMatrix matrix(array), multiplied, sum;
multiplied= Multiply(matrix,2);
sum = multiplied + matrix;
cout<<"Matrix:"<<endl;
matrix.display(matrix);
cout<<"Multiplied with first digit 2: "<<endl;
multiplied.display(multiplied);
cout<<"Sum: "<<endl;
sum.display(sum);
return 0;
}
Download solution file 👇👇👇