Starting from:

$30

Lab 2: Decision Making and Repetition

APS105 Page 1 of 4
APS 105 — Computer Fundamentals
Lab 2: Decision Making and Repetition
The goal of this laboratory is to practice the material on decision making and loops. You will be writing
two separate and fun C programs. The first part is to determine what coins (cents, nickels, dimes and
quarters) can be used to make up a given amount of money. The other program requires the “drawing” of
a triangle of various sizes depending on user’s input.
Preparation
This lab has two parts. Read through this entire document carefully and do the work to create the
programs that are described in Part 1 and 2 below. You can do this on your own home computer if you
have succeeded in downloading and getting Codelite / VSC or any other IDE to work, as described in
Lab 0. You can also do this work using the ECF lab image prior to your lab period. Eventually, you
need to ensure that the code also runs properly on Examify.
Notes:
• In the sample output examples that follow:
o The text that would be entered by the program user is displayed using a bold font.
o The text <enter> stands for the user pressing the enter key on the keyboard. On some
keyboards, the enter key may be labeled return.
• When you execute your programs, the user’s text will not be displayed in bold and <enter> is not
going to show.
• Throughout this lab, there is a single space after the colon (:) in each output line.
Part 1— Making Change
In a file called Lab2Part1.c, write a complete C program that makes change for amounts less than one
dollar. That is, you will determine the number of coins that can make up a specific number of cents. Valid
input to the program should be a positive integer value less than 100, which represents an amount of money,
in cents. The program should prompt the user for a positive integer value, and in response to user input,
should print the original amount of cash, together with a set of coins (quarters, dimes, nickels, cents) that
make up that amount. The program should produce change containing the minimum number of coins
required for the given amount. The output should be in a natural form, in that whenever there is one coin,
the coin type should be singular, otherwise, the coin type should be in its plural form. If the number of coins
for a particular coin type is 0, the type should not be shown. For example, if user input is 58 cents, the
output must look like this:
58 cents: 2 quarters, 1 nickel, 3 cents.
The following shows an example of an incorrect output:
58 cents: 2 quarters, 0 dimes, 1 nickels, 3 cents.
The program should repeatedly prompt the user for additional user input, until an invalid input is entered,
after which the program should stop. An invalid input would be a negative number, one that is greater than
99, or less than 1. It can be assumed that the user always enters integer values when prompted.
An example run of the program is shown below. The values 10, 16, 20, . . . are entered by the user in the
example run. Given the same user input, your output should match the following output exactly, including
APS105 Page 2 of 4
all punctuation, and all wording (including the plural form if needed). Any variation from this will result in
a loss of marks.
Please give an amount in cents less than 100: 10<enter>
10 cents: 1 dime.
Please give an amount in cents less than 100: 16<enter>
16 cents: 1 dime, 1 nickel, 1 cent.
Please give an amount in cents less than 100: 20<enter>
20 cents: 2 dimes.
Please give an amount in cents less than 100: 28<enter>
28 cents: 1 quarter, 3 cents.
Please give an amount in cents less than 100: 30<enter>
30 cents: 1 quarter, 1 nickel.
Please give an amount in cents less than 100: 36<enter>
36 cents: 1 quarter, 1 dime, 1 cent.
Please give an amount in cents less than 100: 67<enter>
67 cents: 2 quarters, 1 dime, 1 nickel, 2 cents.
Please give an amount in cents less than 100: 75<enter>
75 cents: 3 quarters.
Please give an amount in cents less than 100: 99<enter>
99 cents: 3 quarters, 2 dimes, 4 cents.
Please give an amount in cents less than 100: 0<enter>
Goodbye.
APS105 Page 3 of 4
Part 2 – Arithmetic Operations
Write    a    program    that    will    perform    the    four    basic    arithmetic    operations    (addition,    subtraction,    multiplication,    
and    division)    on    pairs    of    fractions,    producing    answers    in    standard    fractional    form.    Specifically,    your    program    
should    repeatedly    perform    the    following    actions.    
• Read    a    character    representing    an    operation.    This    will    be    one    of    the    characters    +,    -,    *,    /,    or    E.    The    E
signals    the    end    of    the    data;    reading    it    should    cause    the    program    to    terminate.    
• Read    four    integers representing    the    numerator    and    denominator    of    a    first    fraction    followed    by    the    
numerator    and    denominator    of    a    second    fraction.    
• Perform    the    indicated    operation    and    print    the    results    in    the    appropriate    form    as    illustrated    in    the    
examples    that    follow.    
• Once the results    are    printed,    the    program should    loop    back    and    start    over,    note    that    to    terminate    and    
exit    the    program    ‘E’ should    be    entered    as    illustrated.
• It    is    your responsibility    to    avoid    divide    by    zero.        All    divide    by    zero    cases    should    be    detected    and    
prevented.        In    such    cases,    the    program    should    alert the    user    with    the message:
o Give values: 1 0 3 4
o output: 1/0 * 3/4 Invalid expression!
OR
o Give values: 3 5 0 7
o output: 3/5 / 0/7 Invalid expression
• It    is    also    your responsibility    to    detect    illegal operations. Only    one    of    the    four    (+, -, *, /)
operations or E to    stop,    id    permitted at    the    start,    else    a    message    should    warn the    user    and    then    
the    program should    allow    the    user    to    try    again.
o Give an operation (+, -, *, /) E to stop:9
o output: Invalid operation! Try again.
o Give an operation (+,-,*,/) $ to stop:
• Notice    that    integer    results    should    be    written    as    such,    as    in    (c),    and    negative    results    should    have    only    
a    negative    sign    on    their    numerator,    as    in    (d).
• It    is    not necessary    to    reduce    fractions    to    lowest    terms, as    in    (a).
In    the    examples,    the    input    is    shown    on    one    line.    
(a)
Give an operation (+, -, *, /) E to stop: +<enter>
Give values: 1 3 -19 -8<enter>
output: 1/3 + -19/-8 = 65/24
(b)
Give an operation (+, -, *, /) E to stop: /<enter>
Give values: 2 -5 3 -7<enter>
output: 2/-5 / 3/-7 = 14/15
(c)
Give an operation (+, -, *, /) E to stop: -<enter>
Give values: 5 2 1 2<enter>
APS105 Page 4 of 4
output: 5/2 - 1/2 = 2
(d)
Give an operation (+, -, *, /) E to stop:*<enter>
Give values: 21 4 7 -3<enter>
output: 21/4 * 7/-3 = -147/12
(e)
Give an operation (+,-,*,/) $ to stop:+ <enter>
Give values: 1 0 3 4<enter>
output: 1/0 + 3/4 Invalid expression!
OR
Give an operation (+, -, *, /) E to stop:/<enter>
Give values: 3 5 0 7<enter>
output: 3/5 / 0/7 Invalid expression!
(f)
Give an operation (+, -, *, /) E to stop: E<enter>
Program ends here.
Hints:
• Consider designing your program flow before starting coding. Use flowchart of pseudocode.
• Consider validating the four operations using Boolean logic.
• Consider combining if…else and switch for decision making.
Good Luck!

More products