Object Oriented Programming using c++
Question 01:
Q1-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
#include
using namespace std;
struct time
{
int hours, minutes, seconds;
};
int main()
{
system("cls");
struct time t1;
cout > t1.hours;
cout > t1.minutes;
cout > t1.seconds;
cout
Q1-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.
#include
using namespace std;
struct time
{
int hours, minutes, seconds;
};
int main()
{
system("cls");
struct time t1, t2, s;
// input first time for t1
cout > t1.hours;
cout > t1.minutes;
cout > t1.seconds;
// print t1
cout > t2.hours;
cout > t2.minutes;
cout > t2.seconds;
// print t2
cout
Q1-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.
#include
using namespace std;
struct time
{
int hours, minutes, seconds;
};
// funtion time_to_secs
unsigned long time_to_secs(struct time a)
{
return ((a.hours * 3600) + (a.minutes * 60) + (a.seconds));
}
// function sec_to_time
struct time secs_to_time(unsigned long x)
{
struct time b;
b.hours = x / 3600;
b.minutes = (x % 3600) / 60;
b.seconds = x % 60;
return b;
}
// main
int main()
{
system("cls");
struct time t1, t2, s;
int total_secs;
cout > t1.hours;
cout > t1.minutes;
cout > t1.seconds;
// print t1
cout > t2.hours;
cout > t2.minutes;
cout > t2.seconds;
// print t2
cout
Question 02:
Q2-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.
#include
using namespace std;
struct fraction
{
int numerator, denumerator;
int gcf(int x, int y)
{
if (y == 0)
{
return x;
}
else
{
return gcf(y, x % y);
}
}
void reducer(fraction &p)
{
int gc = gcf(p.numerator, p.denumerator);
p.numerator /= gc;
p.denumerator /= gc;
}
};
int main()
{
fraction f1, f2;
cout q: ";
cin >> f1.numerator >> f1.denumerator;
cout q: ";
cin >> f2.numerator >> f2.denumerator;
fraction s;
// finding sum
s.numerator = (f1.numerator * f2.denumerator) + (f1.denumerator * f2.numerator);
s.denumerator = f1.denumerator * f2.denumerator;
// reducing result
s.reducer(s);
// print
cout
Q2-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.
#include
using namespace std;
struct fraction
{
int nume, denume;
int gcf(int x, int y)
{
if (y == 0)
{
return x;
}
else
{
return gcf(y, x % y);
}
}
void reducer(fraction &p)
{
int gc = gcf(p.nume, p.denume);
p.nume /= gc;
p.denume /= gc;
}
// fadd
struct fraction fadd(fraction a, fraction b)
{
fraction r;
// finding sum
r.nume = (a.nume * b.denume) + (a.denume * b.nume);
r.denume = a.denume * b.denume;
// reducing
reducer(r);
return r;
}
// fsub
struct fraction fsub(fraction a, fraction b)
{
fraction r;
// finding sub
r.nume = (a.nume * b.denume) - (a.denume * b.nume);
r.denume = a.denume * b.denume;
// reducing
reducer(r);
return r;
}
// fmul
struct fraction fmul(fraction a, fraction b)
{
fraction r;
r.nume = (a.nume * b.nume);
r.denume = a.denume * b.denume;
reducer(r);
return r;
}
// fdiv
struct fraction fdiv(fraction a, fraction b)
{
fraction r;
r.nume = (a.nume * b.denume);
r.denume = (a.denume * b.nume);
reducer(r);
return r;
}
};
int main()
{
fraction f1, f2, result;
cout q: ";
cin >> f1.nume >> f1.denume;
cout q: ";
cin >> f2.nume >> f2.denume;
cout
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 |
#include
#include //for tabular table
using namespace std;
class car_parking
{
private:
float hours, charges;
public:
int car;
static int c;
car_parking()
{
c++;
car = c;
}
void calculateCharges()
{
charges = 2.0;
if (hours > 3)
{
int h = hours;
while (h > 3)
{
h--;
charges = charges + 0.5;
if (charges > 10)
{
charges = 10.0;
break;
}
}
}
}
void get_hours()
{
cout > hours;
}
friend void show_list(car_parking c1, car_parking c2, car_parking c3);
};
int car_parking ::c;
void show_list(car_parking c1, car_parking c2, car_parking c3)
{
cout
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.
#include
using namespace std;
void isPerfect(int n)
{
int i, j, sum;
for (i = 1; i > num;
isPerfect(num);
return 0;
}
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.
#include
using namespace std;
int fib(int n)
{
if (n == 0 || n == 1)
{
return n;
}
else
{
return fib(n - 1) + fib(n - 2);
}
}
int main()
{
int n;
cout > n;
cout
The post University of Gujrat appeared first on International Visas.