You are on page 1of 32

Lab report 08

Title: Chapter 7 Exercises

Course title: Object Oriented Programming


Course code: CSE-160
1 Year 2nd Semester Examination 2021
st

Date of Submission: 12/12/2022

Submitted to-

Dr. MD. Ezharul Islam


Professor
Department of Computer Science and Engineering
Jahangirnagar University
Savar, Dhaka-1342

Sl Class Roll Exam Roll Name

01 381 Shahed Annam

Department of Computer Science and Engineering


Jahangirnagar University
Savar, Dhaka, Bangladesh
Page |1

Problem Number: 1

#include<bits/stdc++.h>
using namespace std;
void reversit(char s[],int n)
{
int i;
for(i=0;i<n/2;i++)
{
swap(s[i],s[n-i-1]);
}
}
int main()
{
int i,j,k,n;
char s[1000];
cin.get(s,1000);
reversit(s,strlen(s));
cout<<s;

return 0;
}
Page |2

Problem Number: 2

#include<bits/stdc++.h>

using namespace std;

class employee

private:

string name;

long number;

public:

getdata()

cin>>name;

cin>>number;

putdata()

cout<<name<<" "<<number<<endl;

};

int main()

int i,j,k,n;

employee arr[100];

cout<<"How many employee do you want ? ";

cin>>n;

for(i=0;i<n;i++)

{
Page |3

arr[i].getdata();

for(i=0;i<n;i++)

arr[i].putdata();

return 0;

}
Page |4

Problem Number: 3

#include<bits/stdc++.h>

using namespace std;

class Distance

private:

int feet;

float inches;

public:

input()

cin>>feet;

cin>>inches;

display()

cout<<feet<<" feet "<<inches<<" inches"<<endl;

void add_dist(Distance);

void div_dist(int n)

float fltfeet = feet + inches/12;

fltfeet/=n;

feet = (int)fltfeet;

inches = (fltfeet-feet)*12.0;

}
Page |5

};

void Distance::add_dist(Distance d)

feet = feet + d.feet;

inches = inches + d.inches;

int main()

int i,j,k,n;

Distance arr[100];

cout<<"How many data do you want ? ";

cin>>n;

for(i=0;i<n;i++)

arr[i].input();

for(i=1;i<n;i++)

arr[0].add_dist(arr[i]);

arr[0].div_dist(n);

arr[0].display();

return 0;

}
Page |6
Page |7

Problem Number: 4

#include<bits/stdc++.h>

using namespace std;

int maxint(int a[],int n)

int i,maxi=a[0],index=0;

for(i=1;i<n;i++)

if(a[i]>maxi)

maxi = a[i];

index = i;

return index;

int main()

int i,j,k,n;

cout<<"How many data do you want ? ";

cin>>n;

int a[n];

for(i=0;i<n;i++)

cin>>a[i];

i = maxint(a,n);
Page |8

cout<<"Maximum is "<<a[i]<<endl;

return 0;

}
Page |9

Problem Number: 5

#include<bits/stdc++.h>

using namespace std;

class fraction

private:

int lob,hor;

public:

input()

cin>>lob>>hor;

display()

cout<<lob<<"/"<<hor<<endl;

sum(fraction x)

lob = lob*x.hor+x.lob*hor;

hor = hor*x.hor;

average(int n)

hor*=n;

};

int main()

{
P a g e | 10

int i,j,k,n;

cout<<"How many data do you want ? ";

cin>>n;

fraction a[n];

for(i=0;i<n;i++)

a[i].input();

for(i=1;i<n;i++)

a[0].sum(a[i]);

a[0].average(n);

a[0].display();

return 0;

}
P a g e | 11
P a g e | 12

Problem Number: 6

#include<bits/stdc++.h>

using namespace std;

enum Suit {clubs, diamonds, hearts, spades};

const int jack = 11;

const int queen = 12;

const int king = 13;

const int ace = 14;

class card{

private:

int number;

Suit suit;

public:

card() {};

void set(int n, Suit s);

void display(void);

};

int main(){

card deck[52];

int j;

for(int j = 0; j < 52; j++){

int num = (j % 13) + 2;

Suit su = Suit(j / 13);

deck[j].set(num, su);

srand(time(NULL));
P a g e | 13

for(int j = 0; j < 52; j++){

int k = rand() % 52;

card temp = deck[j];

deck[j] = deck[k];

deck[k] = temp;

const int PLAYERS = 4;

const int CARDS_OF_PLAYER = 52 / 4;

card bridge[PLAYERS][CARDS_OF_PLAYER];

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

for(int j = 0; j < CARDS_OF_PLAYER; j++)

bridge[i][j] = deck[i * CARDS_OF_PLAYER + j];

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

cout << "Player No." << i + 1 << ": ";

for(int j = 0; j < CARDS_OF_PLAYER; j++)

bridge[i][j].display();

cout << endl;

return 0;

void card::set(int n, Suit s){

suit = s;

number = n;

void card::display(void){

cout << setw(4);

if(number >= 2 && number <= 10)


P a g e | 14

cout << number;

else{

switch(number){

case jack: cout << 'J'; break;

case queen: cout << 'Q'; break;

case king: cout << 'K'; break;

case ace: cout << 'A'; break;

switch(suit){

case clubs: cout << "♣"; break;

case diamonds: cout << "♦"; break;

case hearts: cout << "♥"; break;

case spades: cout << "♠"; break;

}
P a g e | 15

Problem Number: 7

#include<bits/stdc++.h>

using namespace std;

long double mstold(char s[],int n)

char ld[100];

int i,j=0;

for(i=0;i<n;i++)

if(s[i]>=48 && s[i]<=57)

ld[j]=s[i];

j++;

ld[j]='\0';

long double x = stold(ld);

return x;

int main()

int n,i;

char s[100];

while(1)

cin>>s;

cout<<mstold(s,strlen(s))<<endl;

}
P a g e | 16

return 0;

}
P a g e | 17

Problem Number: 12

#include <bits/stdc++.h>

using namespace std;

class safearay

private:

static const int LIMIT = 100;

int a[LIMIT];

public:

void putel(int index, int value);

int getel(int index);

};

int main()

safearay m;

char ch;

do

cout << "Enter \"p\" to enter an array element and \"g\" for conclusion: ";

char sym;

cin >> sym;

switch(sym)

case 'p':

int i, v;

cout << "Enter index and value: ";


P a g e | 18

cin >> i >> v;

m.putel(i, v);

break;

case 'g':

int j, temp;

cout << "Enter an array index: ";

cin >> j;

temp = m.getel(j);

break;

default:

cout << "Input Error!\n";

cout << "Continue? (y/n): ";

cin >> ch;

cout << endl;

while(ch != 'n');

return 0;

void safearay::putel(int index, int value)

if(0 <= index && index <= LIMIT - 1)

a[index] = value;

else

cout << "The index is outOfBounds!\n";

int safearay::getel(int index)


P a g e | 19

int temp;

if(0 <= index && index <= LIMIT - 1)

temp = a[index];

cout << "Item No." << index << " is equal to " << temp << endl;

else

cout << "The index is outOfBounds!\n";

return temp;

}
P a g e | 20

Problem Number: 9

#include<bits/stdc++.h>

using namespace std;

class Queue

private:

enum {m=10};

int q[m];

int head,tail;

public:

Queue()

head=0;

tail=0;

void put(int x)

q[tail++]=x;

cout<<endl;

void get()

cout<<q[head++];

cout<<endl;

};

int main()

{
P a g e | 21

int n,i,op,x;

Queue a;

while(1)

cout<<"1.put \n2.get \n3.exit \n";

cin>>op;

if(op==1)

cin>>x;

a.put(x);

if(op==2)

a.get();

if(op==0)

break;

return 0;

}
P a g e | 22
P a g e | 23

Problem Number: 10

#include <bits/stdc++.h>

using namespace std;

class matrix

private:

static const int LIMIT = 10;

int a[LIMIT][LIMIT];

public:

void putel(int index1, int index2, int value);

int getel(int index1, int index2);

};

int main()

matrix m;

char ch;

do

cout << "Enter \"p\" to enter an array element and \"g\" for conclusion: ";

char sym;

cin >> sym;

switch(sym)

case 'p':

int i1, i2, v;

cout << "Enter index and value: ";

cin >> i1 >> i2 >> v;


P a g e | 24

m.putel(i1, i2, v);

break;

case 'g':

int j1, j2, temp;

cout << "Enter an array index: ";

cin >> j1 >> j2;

temp = m.getel(j1, j2);

break;

default:

cout << "Input Error!\n";

cout << "Continue? (y/n): ";

cin >> ch;

cout << endl;

while(ch != 'n');

return 0;

void matrix::putel(int index1, int index2, int value)

if(0 <= index1 && index1 <= LIMIT - 1 && 0 <= index2 && index2 <= LIMIT - 1)

a[index1][index2] = value;

else

cout << "The index is outOfBounds!\n";

int matrix::getel(int index1, int index2)

{
P a g e | 25

int temp;

if(0 <= index1 && index1 <= LIMIT - 1 && 0 <= index2 && index2 <= LIMIT - 1)

temp = a[index1][index2];

cout << "[" << index1 << ", " << index2 << "] = " << temp << endl;

else

cout << "The index is outOfBounds!\n";

return temp;

}
P a g e | 26

Problem Number: 11

#include<bits/stdc++.h>

using namespace std;

string ldtoms(long double sum);

int main()

long double sum;

char ch;

do

cout << "Enter the amount of money: ";

cin >> sum;

string mss = ldtoms(sum);

cout << "Amount equal to " << mss << endl;

cout << "Continue? (y/n): ";

cin >> ch;

cout << endl;

} while(ch != 'n');

return 0;

string ldtoms(long double sum)

stringstream ss (stringstream::in | stringstream::out);

ss.setf(ios::fixed);

ss << setprecision(2) << sum;

string s = ss.str();

s.insert(0, "$");
P a g e | 27

int p = s.find('.') - 1, n = 0;

for(int i = p; i > 0; i--)

if(++n % 3 == 0)

s.insert(i, ",");

return s;

}
P a g e | 28

Problem Number: 12

#include<bits/stdc++.h>

using namespace std;

long double mstold(string s);

string ldtoms(long double sum);

class bMoney{

private:

long double money;

public:

bMoney() {money = 0;}

bMoney(long double m) {money = m;};

void madd(bMoney b1, bMoney b2);

void getmoney(void);

void putmoney(void);

};

int main(){

bMoney b, sum;

char ch;

do{

cout << "Enter the amount of money: ";

b.getmoney();

sum.madd(sum, b);

cout << "Continue? (y/n): ";

cin >> ch;

cout << endl;

} while(ch != 'n');
P a g e | 29

sum.putmoney();

return 0;

void bMoney::madd(bMoney b1, bMoney b2){

money = b1.money + b2.money;

void bMoney::getmoney(void){

string s;

cin >> s;

money = mstold(s);

void bMoney::putmoney(void){

string s = ldtoms(money);

cout << "Amount is equal " << s << endl;

long double mstold(string s){

long double sum = 0;

int p;

for(int i = 1; i < s.size(); i++){

if(48 <= static_cast<int>(s[i]) && static_cast<int>(s[i]) <= 57)

sum = sum * 10 + static_cast<int>(s[i]) - 48;

else if(s[i] == '.')

p = i;

long double frac_part = pow(10, s.size() - 1 - p);


P a g e | 30

sum /= frac_part;

return sum;

string ldtoms(long double sum){

stringstream ss (stringstream::in | stringstream::out);

ss.setf(ios::fixed);

ss << setprecision(2) << sum;

string s = ss.str();

s.insert(0, "$");

int p = s.find('.') - 1, n = 0;

for(int i = p; i > 0; i--)

if(++n % 3 == 0)

s.insert(i, ",");

return s;

}
P a g e | 31

You might also like