|
CS201P ASSIGNMENT NO. 2 SPRING 2022 |
CS201P ASSIGNMENT NO. 2 SPRING 2022 | 100% RIGHT | INTRODUCTION TO PROGRAMMING(PRACTICAL | BY VuTech
Solution
#include<iostream>
#define row 3
#define col 3
//visit: www.vutechofficial.blogspot.com for more updates
//For solution file, Link is available in description.
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 by first digit 2: "<<endl; //Enter first digit of your VUID
multiplied.display(multiplied);
cout<<"Sum: "<<endl;
sum.display(sum);
return 0;
}