You are on page 1of 4

Chapter 1 Answers

Exercises Page 14

1. X = A * B + C ^ 4
2. X = (C – D) / E + 5) – 5 * D
3. X = 17 * B * C – B ^ (E – A)

Exercises Page 30

Question 1

The following answers are merely suggestions. Any valid answer will be correct.

Description Variable name Variable type Possible value


Number of pencils noPencils numeric integer 50
Average height of person in aveHeightM numeric real 1.72
metres
Symbol (A, G, T or R) symbol character G
Employed? employed Boolean True
Weight of bean in mg. beanWeightMg numeric integer 20
Price of bread breadPrice numeric real 11.50
Gender (M/F) gender character M
Notice of door doorNotice string Exit
Number of door doorNumber integer of string 15 or 3-25
Able to swim? canSwim Boolean False

Question 2

2.1 noMarbles
2.2 amtDue
2.3 registered
2.4 height = 1.75
2.5 personName
2.6 code = “K”
2.7 dogDescr = “Dachshund”
2.8 noSmarties
2.9 weekDays = 7
2.10 notice = “No Entry”
2.11 vat = 0.14

Basic Programming Principles Second Edition Chapter 1 Answers


Page 1 of 4
Question 3

3.1 x = a - bc2
x =a–b*c^2
Substitute values:
x = 84 – 3 * 4 ^ 2
= 84 – 3 * 16
= 84 – 48
= 36

3.2 x = k mod 5 + m \ n – 3n
= k mod 5 + m \ n – 3 * n
Substitute values:
x = 34 mod 5 + 33 \ 7 – 3 * 7
= 34 mod 5 + 33 \ 7 – 21
= 34 mod 5 + 4 – 21
= 4 + 4 – 21
= 8 – 21
= -13

3.3 x = a(bc + 4) – c + a mod b / a


= a * (b * c + 4) – c + a mod b / a
Substitute values:
x = 2 * (10 * 12 + 4) – 12 + 2 mod 10 / 2
= 2 * (120 + 4) – 12 + 2 mod 10 / 2
= 2 * 124 – 12 + 2 mod 10 / 2
= 248 – 12 + 2 mod 10 / 2
= 248 – 12 + 2 mod 5
= 248 – 12 + 2
= 236 + 2
= 238

Question 4

4.1 x = g - 5+14 mod h ^ 2 where g = 12 and h = 1


Substitute values
x = 12 – 5 + 14 mod 1 ^ 2
= 12 – 5 + 14 mod 1
= 12 – 5 + 0
=7+0
=7

4.2 x = 27 * y \ 4 / 2 – z + 2 where y = 24 and z = 4


Substitute values
= 27 * 24 \ 4 / 2 – 4 + 2
= 648 \ 4 / 2 – 4 + 2
= 648 \ 2 – 4 + 2
= 324 – 4 + 2
= 320 + 2
= 322

Basic Programming Principles Second Edition Chapter 1 Answers


Page 2 of 4
4.3 x = a / (b + 2 ^ 2) – 12 + c mod b – 2 where a = 96, b = 2, c = 98
Substitute values
= 96 / (2 + 2 ^ 2) – 12 + 98 mod 2 – 2
= 96 / (2 + 4) – 12 + 98 mod 2 – 2
= 96 / 6 – 12 + 98 mod 2 – 2
= 16 – 12 + 98 mod 2 – 2
= 16 – 12 + 0 – 2
= 4–2
= 2

4.4 x = (y – 2 * z) + y / 6 * w \ 2 where w = 10, y = 12, z = 2


Substitute values
= (12 – 2 * 2) + 12 / 6 * 10 \ 2
= (12 – 4) + 12 / 6 * 10 \ 2
= 8 + 12 / 6 * 10 \ 2
= 8 + 2 * 10 \ 2
= 8 + 20 \ 2
= 8 + 10
= 18

4.5 x = 35 mod y – (z * w + y / 3) + y * w where w = 5, y = 24, z = 2


Substitute values
= 35 mod 24 – (2 * 5 + 24 / 3) + 24 * 5
= 35 mod 24 – (10 + 24 / 3) + 24 * 5
= 35 mod 24 – (10 + 8) + 24 * 5
= 35 mod 24 – 18 + 24 * 5
= 35 mod 24 – 18 + 120
= 11 – 18 + 120
= -7 + 120
= 113

Question 5

5.1
Variables (Integer) noPairsSocks
(Real) amountDue
Constant (Real) price = 3.5
Equation amountDue = noPairsSocks * price

5.2
Variables (Integer) test1, test2
(Real) aveMark
Constant (Real) noTests = 2
Equation aveMark = (test1 + test2) / noTests

5.3
Variables (Integer) numMarbles, numPerChild
Equation numPerChild = numMarbles \ 4
The number 4 could have been declared as a constant in this exercise.

Basic Programming Principles Second Edition Chapter 1 Answers


Page 3 of 4
5.4
Variables (Real) newAmount
Constant (Real) amount = 245.6
discount = 0.05
Equation newAmount = amount – (amount * discount)

5.5
Variables (Integer) noPages
(Real) copyPrice
Constant (Real) coverPrice = 1.5
doublePagePrice = 0.45
Equation copyPrice = coverprice + noPages / 2 * doublePagePrice

5.6
Variables (Integer) noItems
(Real) amtDue
Constant (Real) price = 5.6
discount = 0.05
vat = 0.14
Equation amtDue = noItems * price * (1 – discount) * (1 + vat)
This is only one way of writing the equation.

5.7
Variables (Integer) points1, points2
(Real) avePerc
Constant (Integer) total1 = 34
total2 = 125
Equation avePerc = (points1 / total1 * 100 + points2 / total2 * 100) / 2

5.8
Variables (Integer) length1, length2, length3
(Real) totalLength
Constant (Integer) numLines = 3
cmInM = 100
Equation totalLength = (length1 + length2 + length3) / numLines / cmInM

5.9
Variables (Integer) totalBiscuits, packetsBiscuits, remBiscuits
Constant (Integer) numInPacket = 15
Equation packetsBiscuits = totalBiscuits \ numInPacket
remBiscuits = totalBiscouts mod numInPacket

Basic Programming Principles Second Edition Chapter 1 Answers


Page 4 of 4

You might also like