Sie sind auf Seite 1von 6

create two classes DM and DB which stores the value of distances.

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.

C/C++ PROGRAMMING EXERCISES.


Here are some exercises to help you practice your skills. Some of them will be projects that you
will do for credit. At present, I'm considering the following as projects: 5,6,8,9,12,16,19,20,22. If
you are new to C/C++, get as much practice as you can with problems from the book and these
problems. These are designed to stretch you gently.
1. Type in the following program and run it:
2. #include
3. main()
4. { /* PROGRAM TO PRINT OUT SPACE RESERVED FOR VARIABLES */
5.
char c;
6.
short s;
7.
int i;
8.
unsigned int ui;
9.
unsigned long int ul;
10.
float f;
11.
double d;
12.
long double ld;
13.
cout << endl
14.
<< "The storage space for each variable type is:"
15.
<< endl;
16.
cout << endl << "char: \t\t\t%d bits",sizeof(c)*8; // \t
means tab
17.
cout << endl << "short: \t\t\t%d bits",sizeof(s)*8;
18.
cout << endl << "int: \t\t\t%d bits",sizeof(i)*8;
19.
cout << endl << "unsigned int: \t\t%d bits",sizeof(ui)*8;
20.
cout << endl << "unsigned long int: \t%d bits",sizeof(ul)*8;
21.
cout << endl << "float: \t\t\t%d bits",sizeof(f)*8;
22.
cout << endl << "double: \t\t%d bits",sizeof(d)*8;
23.
cout << endl << "long double: \t\t%d bits",sizeof(ld)*8;
24. }

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.

Input a single character, followed by : h


Input an integer, followed by : 4872
Input a float, followed by : 182.937
The character h when cast to an int gives value ?????
The character h when cast to a float gives value ?????
The integer 4872 when cast to a char gives value ?
The integer 4872 when cast to a float gives value ?????
The float 182.937 when cast to a char gives value ?

34.

The float 182.937 when cast to an int gives value ?????

35. Write a program to print the following pattern:


36.
37.
38.
39.
40.
41.
42.

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:

int is_palindrome(char *s);


Note that strings are terminated with the zero valued character. For the purposes of this
exercise, the terminating zero is not part of the string.
53. Modify your bank program to store the accounts in a file when the program closes
(transaction E) and to restore the accounts from the file when the bank is opened
(transaction S).
54. What will the following program print if you answer "n" 3 times? Decide for yourself,
then run the program.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.

#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)

The number of ways to change amount x using denominations d[0]...d[n]

the number of ways to change x using denominations d[0]...d[n-1]


+ the number of ways to change x-d[n] using denominations d[0]...d[n]
i.e. the number of ways to change 17c using denominations {1c,5c,10c}
= the number of ways to change 17c using {1c, 5c} +
the number of ways to change (17-10)c using {1c, 5c, 10c}.

We apply this result recursively to get the answer.


To set up the solution, we will need an array int denominations[10] to hold the
denominations. The user will be asked how many denominations there are and then will
be asked to type in their values. They go into the array starting at element 0. Then the
user is asked the amount that is to be changed. The amounts are integer numbers of cents.
The program continues to use the same set of denominations and to ask for more amounts
until the user types a zero value, then it halts.
Say the user has typed in 5 denominations, and wants to know the number of ways to
change the amount 90. main() will call ways(90,4), where the 4 gives the index of the
highest denomination in the denom array. Write the function int ways(int, int), making
sure that it returns the correct values when the amount is zero and when the index is
negative. Test your function.
There are 13 ways to change 25c using {1c,5c,10c,25c}
In England the denominations used to be 1/4, 1/2, 1, 2, 3, 6, 12, 30, 120, 240. The were
called farthing, half-penny, penny, two-pence (pronounced tupence), three-pence
(pronounce thrupence), six-pence, shilling (or bob), half-crown, 10-shillings (ten bob),
pound. No wonder we spent 5 years learning how to add money values!
Now add an array oldways[500,10] to keep results as they are calculated. All elements
should start at -1, then when ways(u,v) is called, it should first see if oldways[u,v] > 0. If
it is, the function returns the value from the array, otherwise it computes the value and
inserts it into the array. With the above array the program will only work with values up
to 500 cents.
83. Write a function for Bubble Sort. It should be passed an array and an int giving the array
size, and it should return with the array sorted.
84. Use structures in your bank account program. The main data structure should now be an
array of structures. Each account's details should be kept in a structure.
85. Modify your bank program so that the details of an account are stored in a class. Use
member functions for the transactions on the accounts .
86. Write a program to implement a table for names and addresses. The ADT should have an
interface including insert(), delete(), and search(). In the first version, use linear search,
where the key field is the name. A file of names and addresses will be supplied.

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.

Das könnte Ihnen auch gefallen