Workesshet
Workesshet
#include
main()
{ /* PROGRAM TO PRINT OUT SPACE RESERVED FOR VARIABLES */
char c;
short s;
int i;
unsigned int ui;
unsigned long int ul;
float f;
double d;
long double ld;
cout << endl
<< "The storage space for each variable type is:"
<< endl;
cout << endl << "char: \t\t\t%d bits",sizeof(c)*8; // \t means tab
cout << endl << "short: \t\t\t%d bits",sizeof(s)*8;
cout << endl << "int: \t\t\t%d bits",sizeof(i)*8;
cout << endl << "unsigned int: \t\t%d bits",sizeof(ui)*8;
cout << endl << "unsigned long int: \t%d bits",sizeof(ul)*8;
cout << endl << "float: \t\t\t%d bits",sizeof(f)*8;
cout << endl << "double: \t\t%d bits",sizeof(d)*8;
cout << endl << "long double: \t\t%d bits",sizeof(ld)*8;
}
2. 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:
C
i I
s s
b b
e e
s s
t s e b s i C i s b e s t
4. Write a program to read three integer nnumbers and to print them in ascending order.
5. 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 those divides 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.
6. 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:
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 Explanation
Open an account, giving the initial deposit.
Transaction type?: O
Allowed if less than 100 accounts now open.
Initial deposit?: amount
Prints the new account number.
Transaction type?: B A Balance inquiry, prints the account number
Account number?: account_number and the balance, only allowed if the account is open.
Transaction type?: D
Account number?: account_number A Deposit, prints the account number
and new balance, only allowed if account open.
Amount?: amount
Transaction type?: W
A Withdrawal, only allowed if account open
Account number?: account_number
and sufficient funds available, prints
account number and new balance.
Amount?: amount
Transaction type?: C Close the account. Only allowed if account
Account number?: account_number is open.
Transaction type?: I Compute interest at given % rate.
Interest rate?: interest_rate and apply to all accounts
Transaction type?: P Print all the account numbers and amounts.
Transaction type?: E Close all accounts and exit program
9. 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.
10. Rewrite your leap year program as a function and write a main() to test it.
11. Rewrite your day_of_the_year program as a function and write a main() to test it.
12. 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.
13. 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,
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:
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.
14. 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).
15. What will the following program print if you answer "n" 3 times? Decide for yourself,
then run the program.
#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();
}
16. 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:
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)
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.
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.
17. 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.
18. 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.
19. 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 .
20. 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.
21. Modify your program to implement binary search. This will require new versions of all
three interface functions.
22. Modify your program to use a hash table. You will be given a hash function and a rehash
function.
23. 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().
24. Implement an ADT for a queue using pointers. The interface can be as in the queue
problem above.