Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab12 15

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

ASSIGNMENT - 12

Program: Write a C++ Program to design a class to represent a matrix. The class
should have the functionality to insert and retrieve the elements of the matrix.

Code:

#include<iostream>
using namespace std;
class matrix{
public:
int row, col;
int arr[100][100]; //max size;
//parameterised constructor.
matrix(int r, int c){
row = r;
col = c;
}
//getter
void setvalue(){
for(int i = 0; i < row; i++){
for(int j = 0 ; j < col ; j++){
cout<<"Enter value for row "<< i <<" column "<< j <<": ";
cin>>arr[i][j];
}
}
}
//print function
printmatrix(){
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
cout << arr[i][j] << " ";
}
cout << endl;
}
}
};
int main(){
int r, c;
cout<<"Enter number of rows: ";
cin>>r;
cout<<"Enter number of columns: ";
cin>>c;
output:
matrix m(r,c);
m.setvalue();
m.printmatrix();
return 0;
}

Gurshaan Singh Dult 23124035


Output:

Gurshaan Singh Dult 23124035


ASSIGNMENT - 13
Program: Write a C++ program to design a class representing complex numbers
and having the functionality of performing addition & multiplication of two
complex numbers using operator overloading.

Code:

#include <iostream>
using namespace std;
class Complex {
int real, imag;
public:
// Parameterized constructor
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
// Overload + operator for complex number addition
Complex operator+(Complex const& obj) {
Complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}
// Overload * operator for complex number multiplication
Complex operator*(Complex const& obj) {
Complex temp;
temp.real = real * obj.real - imag * obj.imag;
temp.imag = real * obj.imag + imag * obj.real;
return temp;
}
// Function to display the complex number
void print() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(2, 3); // First complex number
Complex c2(4, 5); // Second complex number
Complex c3 = c1 + c2; // Add the two complex numbers
cout << "Sum: ";
c3.print(); // Print the sum
Complex c4 = c1 * c2; // Multiply the two complex numbers
cout << "Product: ";
c4.print(); // Print the product
return 0;
}

Gurshaan Singh Dult 23124035


ASSIGNMENT - 14
Program: :Write a C++ Program to overload operators like *, <<, >> using
friend function. The following overloaded operators should work for a class
vector.

Code:

#include<iostream>
using namespace std;
class matrix{
public:
int row, col;
int arr[100][100]; //max size;
matrix(int r, int c){
row = r;
col = c;
}
//getter
void setvalue(){
for(int i = 0; i < row; i++){
for(int j = 0 ; j < col ; j++){
cout<<"Enter value for row "<< i <<" column "<< j <<": ";
cin>>arr[i][j];
}
}
}
//print function
void printmatrix(){
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
cout << arr[i][j] << " ";
}
cout << endl;
}
}
//+ operator overloading
matrix operator+(const matrix& other) {
matrix result(row, col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result.arr[i][j] = arr[i][j] + other.arr[i][j];
}
}
return result;
}
//* operator overloading
matrix operator*(const matrix& other) {
matrix result(row, other.col);
for (int i = 0; i < row; i++) {
Gurshaan Singh Dult 23124035
for (int j = 0; j < other.col; j++) {
result.arr[i][j] = 0;
for (int k = 0; k < col; k++) {
result.arr[i][j] += arr[i][k] * other.arr[k][j];
}
}
return result;
}
//comparison operator overloading
bool operator==(const matrix& other) {
if (row != other.row || col != other.col) {
return false;
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (arr[i][j] != other.arr[i][j]) {
return false;
}
}
}
return true;
}
};
int main(){
// rst matrix
int r, c;
cin>>r;
cin>>c;
cout<<endl;
cout<<"For addition of matrices their order must be the same"<<endl;
cout<<"Enter number of rows for the matrices: ";
cout<<"Enter number of columns for the matrices: ";
matrix m1(r,c);
cout<<"Enter elements of the rst matrix: "<<endl;
m1.setvalue();
cout<<endl;
matrix m2(r,c);
cout<<"Enter elements of the second matrix: "<<endl;
m2.setvalue();
cout<<endl;
cout<<"The rst matrix is: "<<endl;
m1.printmatrix(); //matrix1
cout<<endl;
cout<<"The second matrix is: "<<endl;
m2.printmatrix(); //matrix2
cout<<endl;
//addition of two matrices
matrix sum = m1 + m2;
cout<<"The sum of the two matrices is: "<<endl;
sum.printmatrix();
cout<<endl;
//multiplication of two matrices
matrix product = m1 * m2;
cout<<"The product of the two matrices is: "<<endl;
product.printmatrix();
cout<<endl;
Gurshaan Singh Dult 23124035
fi
fi
fi
//comparison of two matrices
if (m1 == m2) {
cout << "The two matrices are EQUAL" << endl;
}else{
cout << "The two matrices are NOT EQUAL" << endl;
}
return 0;
}

Output:

Gurshaan Singh Dult 23124035


ASSIGNMENT - 15
Program: Write a C++ program for developing a matrix class which can handle
integer matrices of different dimensions. Also overload the operator for
addition, multiplication & comparison of matrices.

Code:

#include <iostream>
using namespace std;
class Vector {
private:
int x, y, z;
public:
Vector(int a = 0, int b = 0, int c = 0) : x(a), y(b), z(c) {}
// Overload * operator
friend Vector operator*( Vector& v, int scalar) {
return Vector(v.x * scalar, v.y * scalar, v.z * scalar);
}
// Overload << operator
friend ostream& operator<<(ostream& os, Vector& v) {
os << "(" << v.x << ", " << v.y << ", " << v.z << ")";
return os;
}
// Overload >> operator
friend istream& operator>>(istream& is, Vector& v) {
is >> v.x >> v.y >> v.z;
return is;
}
};
int main() {
Vector v1(1, 2, 3);
Vector v2 = v1 * 2;
cout << "v2 = " << v2 << endl;
Vector v3;
cout << "Enter vector v3: ";
cin >> v3;
cout << "v3 = " << v3 << endl;
return 0;
}

Output:

Gurshaan Singh Dult 23124035

You might also like