Exercise
Exercise
Exercise
DM stores
distances in metres and centimetres and DB in feet and inches. Write a program
that can read values for the class objects and add one object DM with another
object of DB. use friend function.
25. Write a program to read in a character, an integer, and a float, and print out the values,
then cast to other types; so that the interaction with the user is something like the
following:
26.
27.
28.
29.
30.
31.
32.
33.
34.
C
s
e
s
b
e
s
s
t s e b s i C i s b e s t
43. Write a program to read three ints and to print them in ascending order.
44. Given the following rules, write a program to read a year (4 digit integer) and tell
whether the given year is/was a leap year.
a. There were no leap years before 1752.
b. If the year divides by 400 then it is a leap year.
c. All other years that divide by 100 are not leap years.
d. All other years that divide by four are leap years.
For example, 1800,1900 were not leap years but 2000 will be; 1904, 1908,...,1996
were/will be leap years.
45. Write a program that, given a date, three ints (for example, 11 27 1997), will print the
number of that day within its year: i.e. Jan 1st is always 1, Dec 31st is either 365 or 366.
The months of the year have lengths according to the following rules:
a. The odd months up to and including month 7 have 31 days.
b. The even months from 8 upwards, have 31 days.
c. Month 2 has 28 days except in a leap year when it has 29 days.
d. The rest of the months have 30 days.
46. Write a program to read a file and count the number of chars, words, and lines, and print
these quantities.
47. Write a program to manage 100 bank accounts. The accounts have numbers ranging from
1001 to 1100 (decimal). There are several types of transactions, which the program will
read. If allowed, the program may modify the account balance. Otherwise a warning
message will be printed. The dialogue for the transactions have the following forms:
In the following, the bold type represents questions the program types to the user. Each
response to the transaction type question is a single character. Any attempted illegal
transaction causes an error message. Amounts are in dollars and can have 0, 1 or 2 digits
after the point: 2765 or 123.4 or 8864.57:
Interaction
Transaction type?: O
Initial deposit?: amount
Transaction type?: B
Account number?: account_number
Transaction type?: D
Account number?: account_number
Amount?: amount
Transaction type?: W
Account number?: account_number
Amount?: amount
Transaction type?: C
Account number?: account_number
Transaction type?: I
Interest rate?: interest_rate
Transaction type?: P
Transaction type?: E
Explanation
Open an account, giving the initial deposit.
Allowed if less than 100 accounts now open.
Prints the new account number.
A Balance inquiry, prints the account number
and the balance, only allowed if the account is open.
A Deposit, prints the account number
and new balance, only allowed if account open.
A Withdrawal, only allowed if account open
and sufficient funds available, prints
account number and new balance.
Close the account. Only allowed if account
is open.
Compute interest at given % rate.
and apply to all accounts
Print all the account numbers and amounts.
Close all accounts and exit program
48. Add pin numbers to the bank accounts and add a special pin number for the bank
manager. Add a transaction type S to open the bank. Only the manager should be allowed
to do transactions S, P, I, and E. For each transaction, the computer must ask for the pin
number. In an Open transaction, the user chooses the pin number for the account.
49. Rewrite your leap year program as a function and write a main() to test it.
50. Rewrite your day_of_the_year program as a function and write a main() to test it.
51. Write a program to read in a date, 3 ints, and print the corresponding day of the week
(Monday, Tuesday, . . ., Sunday). You will be given a range of valid dates and the day of
the week for a certain date.
52. A function is required that checks strings to see if they are palindromes. A palindrome
string that reads the same, backwards, or forwards. For example,
I was able no on elba saw I
The palindrome can be even or odd in length. Give the body to the following function
and write a main() to read in strings from the user and pass them to your function to test
it. It returns TRUE if the string passed in is a palindrome:
#include
void love_me()
{
char c;
cout << endl << "Do you love me, answer y or n?: ";
c = ' ';
while(c!='y' && c!='n') {
cin >> c;
if(c!='n' && c!='y')
cout << endl << "Invalid reply, try again: ";
}
if(c=='n') {
cout << endl << "I hate you ";
love_me();
}
cout << endl << "I love you too!";
}
void main()
{
love_me();
}
78. Here is a (difficult) classic counting problem. In this country the currency has
denominations 1c, 5c, 10c, 25c, 100c, etc. A bank is trying to figure out how many ways
that a teller can give a customer a certain amount of money. For example, how many
ways can you give 10c using the above denominations? The answer is 4:
79.
80.
81.
82.
10*1c,
1*5c + 5*1c
2*5c
1*10c
Now, how many ways can 100c be given? The answer comes in a recursive form:
Call the denominations d[0], d[1], d[2], ... (d[0]=1, d[1]=5, d[2]=10, etc)
87. Modify your program to implement binary search. This will require new versions of all
three interface functions.
88. Modify your program to use a hash table. You will be given a hash function and a rehash
function.
89. Implement an ADT for a queue using a circular list within an array (of size 20). The
queue will hold integers. The interface is enqueue(int), int = dequeue(), int = empty(),
make_empty().
90. Implement an ADT for a queue using pointers. The interface can be as in the queue
problem above.