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:
    	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:
    	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 ?
    	The float 182.937 when cast to an int gives value ?????

  3. Write a program to print the following pattern:
                          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 ints 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.
    1. There were no leap years before 1752.
    2. If the year divides by 400 then it is a leap year.
    3. All other years that divide by 100 are not leap years.
    4. 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:

    1. The odd months up to and including month 7 have 31 days.
    2. The even months from 8 upwards, have 31 days.
    3. Month 2 has 28 days except in a leap year when it has 29 days.
    4. The rest of the months have 30 days.

  7. Write a program to read a file and count the number of chars, words, and lines, and print these quantities.

  8. 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:

    Transaction type?: O
    Initial deposit?: amount
    Open an account, giving the initial deposit.
    Allowed if less than 100 accounts now open.
    Prints the new account number.
    Transaction type?: B
    Account number?: account_number
    A Balance inquiry, prints the account number
    and the balance, only allowed if the account is open.
    Transaction type?: D
    Account number?: account_number
    Amount?: amount
    A Deposit, prints the account number
    and new balance, only allowed if account open.
    Transaction type?: W
    Account number?: account_number
    Amount?: amount
    A Withdrawal, only allowed if account open
    and sufficient funds available, prints
    account number and new balance.
    Transaction type?: C
    Account number?: account_number
    Close the account. Only allowed if account
    is open.
    Transaction type?: I
    Interest rate?: interest_rate
    Compute interest at given % 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,
    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.

  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.
    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 ";
    	cout << endl << "I love you too!";
    void main()

  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:
      1*5c + 5*1c
    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.

  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.