In this lab, you will:
Using US quarters, dimes, nickels and pennies make change for an amount
of
money that's less than 1.00 .
The user input is in the form .98 or .07 or any two digit number
between 0 and 1.00 . You may assume that
negative inputs aren't allowed
and that quantites greater than
1.00 are not allowed (although this program will work for larger
amounts).
The output for a user input of .89 would be:
3 quarters
1 dimes
4 pennies
Note that since there are no nickels used in making change we wouldn't
write 0
nickels.
We will first try to use as many quarters in making change, then use as
many
dimes, then use as many nickels and finally use pennies.
For example: If the user's input is .89 then multply
this by 100 to convert to an integer. We can then use the / and %
operators and
they work in the following manner:
89 / 25 = 3 quarters
89 % 25 = 14
14 / 10 = 1 dimes
14 % 10 = 4
4 /
5 = 0
nickels
4 % 5 = 4
pennies
Our algorithm in pseudo-code is: (remember that pseudo-code isn't
quite
legal C)
float amount;
int
cents;
int
quarters,
dimes, nickels, pennies;
printf
"Enter the amount:"
scanf
%f &amount
amount = amount + .005 /* correct
for any
round-off errors */
cents = 100 * amount
quarters = cents / 25;
if (quarters > 0)
printf %i
quarters
cents = cents % 25;
dimes = cents / 10
if ( dimes > 0)
printf %i
dimes
Complete the remainder of the algorithm on the
answer
sheet to answer question #1
1. Complete the algorithm
for the
change program.
Open Eclipse and
start a
project named “lab7” (as you learned in lab 6). Create a new source
file for the project and name it “change.c”.
Now you can start writing your program. Don't forget that to include
the code
below.
#include <stdio.h>
void main(void)
{
/* your code goes between the pair of curly braces */
}
Locate the “Run” button on the toolbar and run your code. Check
your code with the following inputs:
.89 .99
, 1 , .05.
Obtain a signature
from your
lab TA to answer question #2 on the Answer sheet.
We will write a C
program that
prompts the user to enter the date in the format mm/dd/yyyy.
Our program will read the values for month, day and year into separate
variables of datatype int. Next, our
program will
then check to see if the date is valid and if so prints the date the
user
entered. If the date entered by the user is not valid then print a
message
corresponding to the error before the program terminates.
The user's input is
in the
format:
2/26/2006
and the output would be
02/26/2006
that is all single digit numbers would be prefixed with 0's.
Also our program would catch (some) bad dates such as the input
2/-5/2006
would produce the output
"Invalid date, all entries must be positive."
and the bad input
13/2/2006
would produce the outptut
"Invalid month!"
And finally the bad input
2/31/2006
would produce the output
Invalid number of days in month!
Again using
pseudo-code our
algorithm is:
int month, day, year;
int maxdays;
printf Enter a date in the format mm/dd/yyyy:
scanf %i/%i/%i
&month, &day ,&year
/* check for non-negative month day and year */
if ( month <= 0 OR day <= 0 OR year
<= 0
)
{
printf("Invalid date, all
entries
must be positive.\n");
return; /* finish the program’s execution */
}
/* determine the maximum number of days in month and check that month
is
between 1 and 12 inclusive. */
switch(month){
case 1: maxdays = 31;
break;
case 2: maxdays = 28;
break;
case 3: maxdays = 31;
break;
case 4: maxdays = 30;
break;
case 5: maxdays = 31;
break;
case 6: maxdays = 30;
break;
case 7: maxdays = 31;
break;
case 8: maxdays = 31;
break;
case 9: maxdays = 30;
break;
case 10: maxdays = 31;
break;
case 11: maxdays = 30;
break;
case 12: maxdays = 31;
break;
default: printf("Invalid month!\n");
return;
}
/* now verify that the day is less than maxdays
and
if so print the date */
if day <= maxdays
printf(" The date
entered is
%02i/%02i/%04i \n", month , day , year);
else
printf("Invalid number of days in
month!\n");
Start a new project named “lab7_part_2”. Create
a new source file for the project and name it “date.c”.
Type (or copy and paste) the following statements
into your
file. (To copy and paste, select the text
and then
middle click where you want to paste it.)
#include <stdio.h>
void main(void){
/* your code goes between the curly braces */
}
Note: The "/" symbols in the scanf
function,
scanf ( "%i/%i/%i",
&month, &day ,&year);
are delimiters and scanf will skip these
characters
and not try to convert these into month, day, year integer variables.
There is
nothing special about the / symbol , if we
asked the
user to enter a date in the format mm?dd?yyyy
then we
would use a ? as our delimiter in the scanf above.
Highlight the file “date.c” by clicking on it with the left button
of the
mouse. Now, use the “Run” button on the toolbar to run the code. If
you get an error message, note the line number and fix the error. In
the
Eclipse console, you should see the statement:
Enter a date in the format mm/dd/yyyy:
Try the following dates,
11/-11/2006
1/1/6
2/35/2006
13/2/2006
Obtain your TA's signature on the answer sheet for question #3 on the
Answer
sheet.