You are on page 1of 8

DS Lab 2

Abdul Rafiu
22k-4162
BCS-3B
Task 1:
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char const *argv[])


{
int row,col;
int **matrix;

cout<<"Enter number of rows in of the matrix: "; cin>>row;


cout<<"Enter Number of colums in the matrix: "; cin>>col;

if(row==col){
matrix= new int*[row];
for (int i = 0; i < row; i++)
{
matrix[i] = new int[col];
for (int j = 0; j < col; j++)
{
cout<<"Enter element "<<i+1<<","<<j+1<<": "; cin>>matrix[i]
[j];
}
}
bool checking = true;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if(i!=j && matrix[i][j]==0){
checking = true;
continue;
}
else if(i==j)
continue;
else{
checking = false;
break;
}
}
if (checking)
{
continue;
}
else
{
break;
}
}
if (checking)
{
cout<<"Given matrix is diagonal."<<endl;
}
else{
cout<<"Given matrix is not diagonal."<<endl;
}
}

else{
cout<<"For the diagonal matrix, given matrix should be square
matrix."<<endl;
}

for (int i = 0; i < row; i++)


{
for (int j = 0; j < col; j++)
{
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}

for (int i = 0; i < row; i++)


{
delete[] matrix[i];
}

return 0;
}
Task 3:
#include <iostream>
using namespace std;

class ProductStockManager
{
int **product;

public:
ProductStockManager(){};

ProductStockManager(int x)
{
product = new int *[3];

for (int i = 0; i < 3; i++)


{
product[i] = new int[2];
}
}

ProductStockManager(const ProductStockManager &obj2)


{
product = obj2.product;
}

~ProductStockManager()
{
for (int i = 0; i < 3; i++)
{
delete[] product[i];
}
}

ProductStockManager operator=(ProductStockManager &obj2)


{
if (this != &obj2)
{
int **product = obj2.product;
}

return *this;
}

void getdata();
void displaydata();
void updateinfo();
};

void ProductStockManager ::getdata()


{

for (int i = 0; i < 3; i++)


{
cout << "Enter the stock of product no." << i + 1 << " :";
cin >> product[i][0];
}
}

void ProductStockManager ::displaydata()


{

cout << "Product no.\tStock" << endl;


for (int i = 0; i < 3; i++)
{

cout << "Product " << i + 1 << "\t";


cout << product[i][0] << endl;
}
}

void ProductStockManager ::updateinfo()


{
int number, stock;
cout << "\nTo update info:\nEnter the product number to update it's stock:
";
cin >> number;
cout << "Now, enter the stock number: ";
cin >> stock;

if ((number > 3) || (number < 0))


{
cout << "Boundary Error!";
}

for (int i = 0; i < 3; i++)


{
if (i == number - 1)
{
product[number - 1][0] = stock;
}
}
}

main()
{
int choice;
ProductStockManager obj(1);
obj.getdata();

cout << "\nBefore modifications:" << endl;


obj.displaydata();

cout << "\nTo modify the data!" << endl;


obj.updateinfo();

cout << "After Modification: " << endl;


obj.displaydata();
}
Task 4:
#include <iostream>
#include <cstring>

using namespace std;

class FruitShopInventory{
int **fruitinv;
int size;
public:
FruitShopInventory(int s):size(s),fruitinv(new int*[size]){
for(int i = 0; i<size; i++){
fruitinv[i]=new int[2];
fruitinv[i][0]=5;
fruitinv[i][1]=200;
}
}

FruitShopInventory(FruitShopInventory& obj):
size(obj.size),fruitinv(new int*[obj.size]){
for(int i = 0; i<size; i++){
fruitinv[i]=new int[2];
for(int j=0;j<2;j++){
fruitinv[i][j]=obj.fruitinv[i][j];
}
}
}

FruitShopInventory operator =(FruitShopInventory &obj){


if (this != &obj)
{
fruitinv = obj.fruitinv;
size = obj.size;
}
return *this;
}

~FruitShopInventory(){
for(int i=0;i<size;i++)
delete[] fruitinv[i];
}

void set(int row,int col,int val){


if(row<0||row>=size||col<0||col>=2)
cout<<"Boundary Error\n";
else{
fruitinv[row][col]=val;
}
}

void print(){
for(int i = 0; i<size; i++){
cout<<"Quantity: "<<fruitinv[i][0]<<", Price: "<<fruitinv[i]
[1]<<endl;
}
}
};

int main(){
FruitShopInventory obj1(3);
FruitShopInventory obj2 = obj1;

cout<<"Before Modification: "<<endl;


cout<<"Object 1: "<<endl;
obj1.print();
cout<<"Object 2: "<<endl;
obj2.print();

obj1.set(2,0,4);
obj1.set(2,1,300);

cout<<"After Modification: "<<endl;


cout<<"Object 1: "<<endl;
obj1.print();
cout<<"Object 2: "<<endl;
obj2.print();

return 0;
}

You might also like