Home > COMP20005, education, programming > COMP20005 workshop 2

## COMP20005 workshop 2

The basic idea of workshop 2 is for you to practice writing programs that use conditional. By “conditional” I mean the `if``else` construct. The assigned exercises for this workshop are:

• Exercise 3.2, pages 40–41
• Excerise 3.6, page 43
• Exericse 3.7, page 43

You should be very careful with exercises 3.2(d) and 3.2(e). Here are some hints. The formatting in exercise 3.2(d) is misleading and should serve to remind you to properly indent your code and use curly braces. In exercise 3.2(e), the semicolon that follows a conditional can make the following block of code not part of the `if` construct. In other words, something like

```if (<condition or guard>) ;
```

or

```if (<condition or guard>)
;
```

is valid C code. In this case, the whole `if` block is comprised of the guard and the semicolon. Everything after the semicolon do not belong to the `if` block.

Exercise 3.6 asks you to determine the change for a given number of cents that is at most 99. You are only allowed to use the denominations of 50, 20, 10, 5, 2, and 1 cents. You should solve this exercise by using only conditionals. So concepts like loops and recursion should not be used to solve this exercise. One way to solve the exercise via conditionals only is to take each denomination from largest to smallest. Think about the maximum number of coins of a given denomination that can go into the number of cents you have. Then write a number of `if` statements to get the required change. Another way to solve the exericse is to use the quotient and remainder. One of these two methods require less code than the other. Try out both methods and see for yourself.