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.
#includemain() { /* 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; }
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 ?????
C i I s s b b e e s s t s e b s i C i s b e s t
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 |
---|---|
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 |
#includevoid 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(); }
10*1c, 1*5c + 5*1c 2*5c 1*10cNow, how many ways can 100c be given? The answer comes in a recursive form:
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.