0% found this document useful (0 votes)
95 views

OOP Assignment 1, FA20-BEE-3C-146

This document is an assignment submission for an Object Oriented Programming course. It includes solutions to exercises from Chapter 7 of the textbook "Object Oriented Programming" by Robert Lafore. The student, Muhammad Souban Javaid, submitted solutions to questions 1-4 and 12 from the chapter exercises. The solutions include code for reversing a string, defining an Employee class, calculating the average of English distances input by the user, finding the maximum value in an integer array, and defining a Money class.

Uploaded by

Souban Javed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

OOP Assignment 1, FA20-BEE-3C-146

This document is an assignment submission for an Object Oriented Programming course. It includes solutions to exercises from Chapter 7 of the textbook "Object Oriented Programming" by Robert Lafore. The student, Muhammad Souban Javaid, submitted solutions to questions 1-4 and 12 from the chapter exercises. The solutions include code for reversing a string, defining an Employee class, calculating the average of English distances input by the user, finding the maximum value in an integer array, and defining a Money class.

Uploaded by

Souban Javed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CSC241 Object Oriented Programming

Fall 2021
Assignment 1-Objects and Classes (Maps to CLO1)
Chapter 7, Exercise Questions 1 to 4 and Question 12.
Book: Object Oriented Programming by Robert Lafore (Fourth Edition)

Name: Muhammad Souban Javaid


Roll No: FA20-BEE-146
Section: 3C
Submitted to: Madam Amber Madeeha Zeb
Date: 07-10-2021

1. Write a function called reversit() that reverses a C-string (an array of char). Use a for
loop that swaps the first and last characters, then the second and next-to-last characters,
and so on. The string should be passed to reversit() as an argument. Write a program to
exercise reversit(). The program should get a string from the user, call reversit(), and print
out the result. Use an input method that allows embedded blanks. Test the program with
Napoleon’s famous phrase, “Able was I ere I saw Elba.”
#include<bits/stdc++.h>
using namespace std;
void reverseString(char s[]);

int main(){
char input[100];
cout << "Insert a string: ";
cin.get(input, 100);

reverseString(input);
cout << "Reversed string: " << input << endl;
return 0;
}

void reverseString(char s[]){


int len = strlen(s);
for(int j = 0; j < len/2; j++){
char temp = s[j];
s[j] = s[len-j-1];
s[len-j-1] = temp;
}
}

2. Create a class called employee that contains a name (an object of class string) and an
employee number (type long). Include a member function called getdata() to get data from
the user for insertion into the object, and another function called putdata() to display the
data. Assume the name has no embedded blanks. Write a main() program to exercise this
class. It should create an array of type employee, and then invite the user to input data for
up to 100 employees. Finally, it should print out the data for all the employees.
#include<bits/stdc++.h>
using namespace std;

class employee{
string name;
long number;
public:
void getdata(){
cout << "Insert name: ";
cin >> name;
cout << "Insert number: ";
cin >> number;
}
void putdata(){
cout << "\nName: " << name;
cout << "\nNumber: " << number;
}
};

int main(){
employee empArray[100];
int n = 0;
char ch;
do{
cout << "Insert data for employee " << n + 1;
empArray[n++].getdata();
cout << "Insert another employee details (y/n)?: ";
cin >> ch;
}while(ch != 'n');
for(int i = 0; i < n; i++){
cout << "Employee " << i + 1 << " details";
empArray[i].putdata();
}
cout << endl;
return 0;
}
3. Write a program that calculates the average of up to 100 English distances input by the
user. Create an array of objects of the Distance class, as in the ENGLARAY example in
this chapter. To calculate the average, you can borrow the add_dist() member function
from the ENGLCON example in Chapter 6. You’ll also need a member function that
divides a Distance value by an integer.
Here’s one possibility: void Distance::div_dist(Distance d2, int divisor)
{
float fltfeet = d2.feet + d2.inches/12.0;
fltfeet /= divisor;
feet = int(fltfeet);
inches = (fltfeet-feet) * 12.0;
}
#include <bits/stdc++.h>
using namespace std;

class Distance{
int feet;
float inches;
public:
Distance(): feet(0), inches(0) { };
Distance(int f, int i): feet(f), inches(i) { };
void getdist();
void showdist() const;
void add_dist(Distance d);
void div_dist(int k);
};

int main(){
const int MAX = 100;
Distance d[MAX], sum;
int n = 0;
char ch;
do{
d[n].getdist();
sum.add_dist(d[n++]);
cout << "Do you want to continue (y/n)?: ";
cin >> ch;
cout << endl;
} while(ch != 'n');
sum.div_dist(n);
sum.showdist();
return 0;
}

void Distance::getdist(){
cout << "Insert feet: ";
cin >> feet;
cout << "\nInsert inches: ";
cin >> inches;
}
void Distance::showdist() const{
cout << feet << "\'-" << inches << '\"' << endl;
}

void Distance::add_dist(Distance d){


feet += d.feet;
inches += d.inches;
if(inches > 11){
inches -= 12;
feet++;
}
}

void Distance::div_dist(int k){


int total = feet * 12 + inches;
total /= k;
feet = total / 12;
inches = total % 12;
}

4. Start with a program that allows the user to input a number of integers, and then stores
them in an int array. Write a function called maxint() that goes through the array, element
by element, looking for the largest one. The function should take as arguments the address
of the array and the number of elements in it, and return the index number of the largest
element. The program should call this function and then display the largest element and its
index number. (See the SALES program in this chapter.)
#include<bits/stdc++.h>
using namespace std;

int maxint(int *a, int n){


int max = 0, index;
for(int i = 0; i < n; i++){
if(a[i] > max)
{
max = a[i];
index = i;
}
}
return index;
}

int main(){
int n;
cin >> n;
int a[n];
for(int i = 0; i < n; i++){
cin >> a[i];
}
int index = maxint(a, n);
cout << a[index] << " at " << index;
return 0;
}
12. Create a class called bMoney. It should store money amounts as long doubles. Use the
function mstold() to convert a money string entered as input into a long double, and the
function ldtoms() to convert the long double to a money string for display. (See Exercises 6
and 10.) You can call the input and output member functions getmoney() and putmoney().
Write another member function that adds two bMoney amounts; you can call it madd().
Adding bMoney objects is easy: Just add the long double member data amounts in two
bMoney objects. Write a main() program that repeatedly asks the user to enter two money
strings, and then displays the sum as a money string.
Here’s how the class specifier might look:
class bMoney
{
private:
long double money;
public:
bMoney();
bMoney(char s[]);
void madd(bMoney m1, bMoney m2);
void getmoney();
void putmoney();
};
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>

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');
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);
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;
}

----------------------------------------------------------------------------------------------------------------------------

You might also like