Ass1 OOP
Ass1 OOP
Submitted To : Department :
Mr. Zulfiqar Ali IT24A(Evening)
Information Technology
Question 01:
a. Create a structure called time. Its three members, all type int, should be called hours, minutes, and
seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds.
This can be in 12:59:59 format, or each number can be entered at a separate prompt (“Enter hours:”, and
so forth). The program should then store the time in a variable of type struct time, and finally print out the
total number of seconds represented by this time value: long totalsecs = t1.hours*3600 + t1.minutes*60
+ t1.seconds.
CODE:
#include<iostream>
using namespace std;
struct Time{
int hours,minutes,seconds;
};
void input(Time &t){
cout<<"Enter number of Hours : ";
cin>>t.hours;
cout<<"Enter number of Minutes : ";
cin>>t.minutes;
cout<<"Enter number of Seconds : ";
cin>>t.seconds;
}
void printSec(Time t1){
long totalsecs = t1.hours*3600 + t1.minutes*60 + t1.seconds;
cout<<"Total number of Seconds are : "<<totalsecs;
}
int main(){
Time t1;
input(t1);
printSec(t1);
}
b. Use the time structure from Part a, and write a program that obtains two time values from the user,
stores them in struct time variables, converts each one to seconds (type int), adds these quantities,
converts the result back to hours-minutes-seconds, stores the result in a time structure, and finally
displays the result in 12:59:59 format
CODE:
#include<iostream>
using namespace std;
struct Time{
int hours,minutes,seconds;
};
void input(Time &t){
cout<<"Enter number of Hours : ";
cin>>t.hours;
cout<<"Enter number of Minutes : ";
cin>>t.minutes;
cout<<"Enter number of Seconds : ";
cin>>t.seconds;
}
void printSec(Time t1){
int totalsecs = t1.hours*3600 + t1.minutes*60 + t1.seconds;
cout<<"Total number of Seconds are : "<<totalsecs;
}
int toSec(Time t){
return t.hours*3600 + t.minutes*60 + t.seconds;
}
Time format(int totalSec){
Time t;
t.hours =totalSec /3600;
totalSec %= 3600;
t.minutes =totalSec /60;
t.seconds =totalSec %60;
return t;
}
void print(Time t){
cout<<t.hours<<":"<<t.minutes<<":"<<t.seconds<<endl;
}
int main(){
Time t1,t2,t3;
cout<<"Enter First Time Format "<<endl;
input(t1);
cout<<"Enter Second Time Format "<<endl;
input(t2);
int totalSec= toSec(t1) + toSec(t2);
t3= format(totalSec);
print(t3);
}
c. Start with the program from Part b, “Structures,” which adds two struct time values. Keep the same functionality, but
modify the program so that it uses two functions. The first, time_to_secs(), takes as its only argument a structure of type
time, and returns the equivalent in seconds (type long). The second function, secs_to_time(), takes as its only argument a
time in seconds (type long), and returns a structure of type time.
CODE:
#include <iostream>
using namespace std;
struct Time {
int hours, minutes, seconds;
};
void input(Time &t) {
cout << "Enter number of Hours : ";
cin >> t.hours;
cout << "Enter number of Minutes : ";
cin >> t.minutes;
cout << "Enter number of Seconds : ";
cin >> t.seconds;
}
long time_to_secs(Time t) {
return t.hours * 3600 + t.minutes * 60 + t.seconds;
}
int main() {
Time t1, t2, t3;
input(t1);
input(t2);
Question 02:
a. Create a structure called fraction. It has two members, both type int, called numerator and
denominator. Write a program that input values in two variables of type fraction. Add both fractions and
print the result in fraction format.
CODE:
#include<iostream>
using namespace std;
struct Fraction{
int numerator,denominater;
};
void input(Fraction &f){
cout<<"Enter Numerator :";
cin>>f.numerator;
cout<<"Enter Denominator :";
cin>>f.denominater;
}
Fraction add(Fraction f1,Fraction f2){
Fraction result;
result.numerater=f1.numerator*f2.denominater+f2.numerator*f1.denominater;
result.denominater=f1.denominater * f2.denominater;
return result;
}
void display(Fraction f) {
cout << "Sum: " << f.numerator << "/" << f.denominator << endl;
}
int main(){
Fraction f1,f2,f3;
cout<<"Enter first fraction "<<endl;
input(f1);
cout<<"Enter second fraction "<<endl;
input(f2);
f3=add(f1,f2);
display(f3);
return 0;
}
b. Use the fraction structure from Part a, implement four-function fraction calculator. It uses functions
for each of the four arithmetic operations. They can be called fadd(), fsub(), fmul(), and fdiv(). Each of
these functions should take two arguments of type struct fraction, and return an argument of the same
type.
CODE:
#include <iostream>
using namespace std;
struct Fraction {
int numerator, denominator;
};
void input(Fraction &f) {
cout << "Enter numerator: ";
cin >> f.numerator;
cout << "Enter denominator: ";
cin >> f.denominator;
}
Fraction fadd(Fraction f1, Fraction f2) {
return {f1.numerator * f2.denominator + f2.numerator * f1.denominator, f1.denominator * f2.denominator};
}
Fraction fsub(Fraction f1, Fraction f2) {
return {f1.numerator * f2.denominator - f2.numerator * f1.denominator, f1.denominator * f2.denominator};
}
Fraction fmul(Fraction f1, Fraction f2) {
return {f1.numerator * f2.numerator, f1.denominator * f2.denominator};
}
Fraction fdiv(Fraction f1, Fraction f2) {
return {f1.numerator * f2.denominator, f1.denominator * f2.numerator};
}
void display(Fraction f) {
cout << f.numerator << "/" << f.denominator << endl;
}
int main() {
Fraction f1, f2, result;
char op;
cout << "Fraction Calculator (+, -, *, /)\n";
input(f1);
input(f2);
cout << "Enter operation (+, -, *, /): ";
cin >> op;
switch (op) {
case '+': result = fadd(f1, f2); break;
case '-': result = fsub(f1, f2); break;
case '*': result = fmul(f1, f2); break;
case '/': result = fdiv(f1, f2); break;
default: cout << "Invalid operation!\n"; return 1;
}
return 0;
}
Question 03:
A parking garage charges a $2.00 minimum fee to park for up to three hours. The garage charges an
additional $0.50 per hour for each hour or part thereof in excess of three hours. The maximum charge for any given 24-
hour period is $10.00. Assume that no car parks for longer than 24 hours at a time. Write a program that calculates and
prints the parking charges for each of three customers who parked their cars in this garage yesterday. You should enter
the hours parked for each customer. Your program should print the results in a neat tabular format and should calculate
and print the total of yesterday’s receipts. The program should use the function calculateCharges() to determine the
charge for each customer.
Your outputs should appear in the following format:
Car Hours Charge
1 1.5 2.00
2 4.0 2.50
3 24.0 10.00
TOTAL 29.5 14.50
CODE:
#include <iostream>
struct Car {
double hours;
double charge;
};
if (hours <= 3)
return 2.00;
return 10.00;
else
int main() {
Car cars[3];
cout << "Enter hours parked for car " << i + 1 << ": ";
cars[i].charge = calculateCharges(cars[i].hours);
totalHours += cars[i].hours;
totalCharge += cars[i].charge;
cout << i + 1 << " " << cars[i].hours << " " << cars[i].charge << endl;
cout << "TOTAL " << totalHours << " " << totalCharge << endl;
return 0;
Question 04:
An integer is said to be a perfect number if the sum of its divisors, including 1 (but not the number itself), is
equal to the number. For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write a function isPerfect() that determines
whether parameter number is a perfect number. Use this function in a program that determines and prints all the perfect
numbers between 1 and 1000. Print the divisors of each perfect number to confirm that the number is indeed perfect.
Challenge the power of your computer by testing numbers much larger than 1000.
CODE:
#include<iostream>
using namespace std;
bool isPerfect(int num){
int sum= 0;
for (int i =1;i<num;i++){
if(num%i==0){
sum+=i;
}
}
return sum==num;
}
void printDivisor(int num){
for(int i=1;i<num;i++){
if(num %i==0){
cout<<i<<" ";
}
}
cout<<endl;
}
int main(){
cout<<"Print all the perfect numbers between 1 and 1000 "<<endl;
for (int i =1;i<= 1000;i++){
if(isPerfect(i)){
cout<<i<<endl;
cout << "Divisors: ";
printDivisor(i);
}
}
}
Question 05: Consider a situation that a user wants to print first n (any value, e.g. first 10) numbers of
Fibonacci series. You need to implement a program that gets a number from user and prints first n Fibonacci
series numbers. Write a function for this purpose that prints Fibonacci series recursively. This function should
accept a number in parameter and then print n number of elements of Fibonacci series.
CODE:
#include<iostream>
using namespace std;
int fibonacci(int n){
for(int i=0;i<n;i++){
if(n<=1){
return n;
}
else
return fibonacci(n-1) +fibonacci(n-2);
}
}
void printfibonacci(int n){
for(int i=0;i<n;i++){
cout<<fibonacci(i)<<" ";
}
}
int main(){
int n;
cout<<"Enter number :";
cin>>n;
cout<<"Print First "<<n<<" fibonacci series number "<<endl;
printfibonacci(n);
}