Professional Documents
Culture Documents
Computer Maths by Buumba, p.
Computer Maths by Buumba, p.
Enter 𝑥
𝑦 =7∗𝑥−3
Display 𝑦
Stop
2nd Edition
COMPUTERS
A computer is simply a device that can be programmed to carry out arithmetic or logical operations.
Basic elements of a computer include among others input and output devices. Input devices send data and
instructions to computers. The most common input devices are keyboard, mouse, scanner, joystick,
barcode reader etc. On the other hand output devices display or output work done by the computer in a
readable format to users. Output devices include the following monitor (screen), printer, speakers etc.
BASIC MATHEMATICAL OPERATIONS
There are four basic operations that are carried out with numbers. These are addition, subtraction,
multiplication and division. When dealing with programming, there is a special way of writing these
operation symbols and it is called computer language. The table below shows the expression of these
operation symbols in both mathematical and computer languages as well as some examples.
MATHEMATICAL LANGUAGE COMPUTER LANGUAGE
Addition (+) +
Subtraction ( − ) −
Multiplication ( × ) ∗
Division (÷ ) /
𝑥+𝑦 𝑥+𝑦
𝑥−𝑦 𝑥−𝑦
𝑥×𝑦 𝑥∗𝑦
𝑥÷𝑦 𝑥/𝑦
𝑥2 𝑥∗𝑥
(𝑥 + 𝑦)(𝑥 − 𝑦) (𝑥 + 𝑦) ∗ (𝑥 − 𝑦)
𝑎 + 𝑏 − 2𝑎𝑏𝑐 𝑎+𝑏−2∗𝑎∗𝑏∗𝑐
Note that addition and subtraction symbols remain the same, only multiplication and division symbols
change.
ALGORITHMS
An algorithm can simply be defined as a step by step procedure to solve a given problem.
WRITING AN ALGORITHM
We can write an algorithm by using either a flowchart or a pseudo code.
FLOWCHARTS
FLOWCHART SYMBOLS
There are six basic symbols that are commonly used in flowchart. These are:
SYMBOL USE
1. Terminal (for start/stop)
Indicates the starting or ending of the program. Sometimes we use the oval
shape
Used for input or output operations i.e. taking input and showing output
Have you ever wondered what happens when you punch say 99 + 798 or any other input on the calculator
before it gives you the answer?
Whatever happens before the answer is displayed is called the internal process or internal operation.
Indicates the selection or decision. It is used for asking questions that can
have either YES or NO (TRUE or FALSE) as an answer.
5. Connector
Used for connecting breaks in the in the flowchart.
FLOWCHART RULES
Flowchart is generally drawn from top to bottom.
All boxes (symbols) of the flowchart must be connected with an arrow.
All flowcharts start with a Terminal. Flowcharts always have starting and ending points indicated by
the symbol (Terminal or oval).
Decision symbol has two exit points, one for YES (TRUE) and another one for NO (FALSE).
Example 1
Given that the base of a triangle is 𝑏 and its perpendicular height ℎ. Draw a flow chart for a program
that calculates the area of a right angled triangle.
Solution.
1
The formula for the area of a triangle is given by: 𝐴 = 𝑏ℎ
2
Start
TIPS.
A flowchart always starts with a terminal or oval
shape. Enter b, h
The second step is to make entries. Here you only
enter variables which should be separated by a
comma always. 𝐴=1 2∗𝑏∗ℎ
After entering, we expect the program to do the
processing. This is shown by the formula written
using computer language in a rectangle.
After processing, the results are displayed. Display Area
Then stop.
Stop
SOLUTION
Start
Start
Enter r, h
𝐴 =𝜋∗𝑟∗𝑟∗ℎ
Display Volume
Stop
Stop
EXERCISE 2
1. Name and draw the symbol which represents each of the following in the flowchart.
(a) Process (b) input (c) decision
2. Draw flowcharts for the programs that calculate the following: (tip: make sure you first identify
variables and constants in the formula.)
1 𝜃
(a) P = 2(𝑙 + 𝑤) (b) A = 2 (𝑎 + 𝑏)ℎ (c) A = 360 × 𝜋𝑟 2 (d) V = 𝜋𝑟 2 ℎ (e) V = 𝑙𝑏ℎ
1 𝜃
(f) V = 3 𝜋𝑟 2 ℎ (g) A = 𝜋𝑟𝑙 + 𝜋𝑟 2 (h) 𝑙 = 360
× 2𝜋𝑟 (i) C = 2𝜋𝑟 (j) A = 4𝜋𝑟 2
1 𝑎
(k) A= 2 𝑎𝑏𝑠𝑖𝑛𝐶 (l) 𝑆∞ = 1−𝑟 (m) 𝐴 = 𝑙𝑏 (n) 𝐴 = 2𝜋𝑟ℎ + 2𝜋𝑟 2
PSEUDO CODE
Example 4.
1
Write a pseudo code for a program that calculates and outputs the area of a triangle. [𝐴 = 2 𝑎𝑏𝑠𝑖𝑛𝐶]
Solution.
1
The formula is 𝐴 = 𝑎𝑏𝑠𝑖𝑛𝐶
2
Note that in the formula, the variables are 𝑎, 𝑏 𝑎𝑛𝑑 𝐶. 𝑇ℎ𝑖𝑠 is what we will enter.
Start
Enter 𝒂, 𝒃, 𝑪
𝑨 = 𝟏⁄𝟐 ∗ 𝒂 ∗ 𝒃 ∗ 𝒔𝒊𝒏 𝑪
Display Area
Stop
Example 5.
Study the flowchart below and write a pseudo code corresponding to it.
Start
Enter x Solution.
Start
𝑦 =7∗𝑥−3 Enter x
𝒚 = 𝟕∗𝒙−𝟑
Display y
Display y Stop
Stop
2. Study the following flowcharts and write the corresponding pseudo codes for each one of them.
(a) The IF – THEN statement is used when only one option is available. Its structure is as shown
below.
Indicates the
start of the
IF < 𝒄𝒐𝒏𝒅𝒊𝒕𝒊𝒐𝒏 > THEN YES
selection Condition Program statement
structure
Statement (s)
(Decision)
NO
END IF
Indicates the end/termination of the selection.
Example 6.
The program below is given in form of a pseudo code which calculates a discount of 15% for
customers buying goods worth more than K500.
Start
Enter amount
IF amount > K500 THEN
Discount = 𝐴𝑚𝑜𝑢𝑛𝑡 ∗ 0.15
END IF
Display Discount
Stop.
Write a flowchart corresponding to the pseudo code above.
Solution.
Clearly from the program above, there is only one option when it comes to calculation of discount
which is buying goods worth more than K500.
Start
Enter Amount
Is
Amount > 𝐾500? NO
YES
Display Discount
Stop
Note that the word “IF” becomes “IS” in the decision box.
Example 7
The program below is given in form of a pseudo code which calculates a discount of 15% for customers
buying goods worth more than K500 and 10% for those buying goods worth K500 and less.
(a) Write a flowchart that corresponds to the program below.
Solution
Start
Enter Amount
Start
IF Amount > K500 THEN
Discount = 𝐴𝑚𝑜𝑢𝑛𝑡 ∗ 0.15
ELSE
Enter Amount
Discount = 𝐴𝑚𝑜𝑢𝑛𝑡 ∗ 0.10
END IF
Display Discount
Stop Is
YES
Amount > K500? Disc= 𝐴𝑚𝑜𝑢𝑛𝑡 ∗ 0.15
NO
Display Discount
Stop
(b) How much discount will a customer buying goods worth K800 get?
SOLUTION
Since K800 is greater than K500, then:
Discount = 𝐴𝑚𝑜𝑢𝑛𝑡 × 0.15
= K800 × 0.15
= 𝐊𝟏𝟐𝟎
Start
Enter a, b, c
𝐷 = 𝑏∗𝑏−4∗𝑎∗𝑐
𝑥1 = (−𝑏 + √𝐷)/2 ∗ 𝑎
𝑥2 = (−𝑏 − √𝐷)/2 ∗ 𝑎
Print 𝑥1 , 𝑥2
Stop
SOLUTION
Start
Enter a, b, c
D=b∗b−4∗a∗c
IF D < 0 THEN
Print “No real solution”
ELSE
𝑥1 = (−𝑏 + √𝐷)/2 ∗ 𝑎
𝑥2 = (−𝑏 − √𝐷)/2 ∗ 𝑎
END IF
Print 𝑥1 , 𝑥2
Stop
3. Given three numbers x, y and z, complete the flow chart below to calculate the mean (M) of the
numbers.
START
STOP
4. Given the distance (D) covered by a car and the time (T) the car takes to cover this distance,
complete the flow chart below for calculating and displaying its average speed (S).
Stop
5. Given that the base radius of a cylinder is r and its height is h, complete the flow chart below for
calculating and displaying its Volume V.
Start
Stop
6. Given the density (D) of a stone and its mass (M), complete the flow chart for calculating and
displaying the volume (V).
Start
Stop
Stop
8. Construct a flow chart for a program that can determine whether a given number is positive or
negative.
9. Given the mass (M), and the volume (V) of an object, complete the flow chart below for
𝑀
calculating and displaying the density (D). 𝐷 =
𝑉
Start
Stop
10. Given the radius (r) and height (h), complete the flow chart below for calculating and displaying
the curved surface area (A) of a cylinder. [𝐴 = 2𝜋𝑟ℎ]
Stop
11. Study the flow chart below to answer the question that follows.
Start
Enter x
𝑦 =7∗𝑥−3
Display y
Stop
𝑥 𝑦
2
32
12. Given the height (h) of a triangle and its base (b), complete the steps below for calculating and
1
displaying its area (A). [𝐴 = 2 𝑏ℎ]
13. Given the length (l) of a rectangle and its area (A), complete the steps below for calculating and
displaying the breadth (b).
Start
Enter …………………………..
b = ……………………………..
Display ………………………
Stop
Start
Enter radius
If radius < 0
Then display “error message” and re-enter positive radius
Else enter height
If height < 0
Then display “error message” and re-enter positive height
1
Else Volume = 3 ∗ 𝜋 ∗ square radius ∗ height
End if
Display volume
Stop
2. The program below is given in the form of a flow chart. Write a pseudo code corresponding to
the program.
Enter a, r
Is
𝑟 < 0? No
Yes
𝑎
𝑆∞ =
1−𝑟
Stop
Start
Enter r
Is Yes
r < 0? Error “r must be positive”
No
1
A = 2 ∗ 𝑟 ∗ 𝑟 ∗ sin 𝜃
Display Area
Write a pseudo code corresponding to the flow chart
program alongside.
Stop
Start Start
Enter a, r, n Enter x, y
R=1−𝑟 Let M = square root (x squared + y squared)
If R = 0 THEN IF M < 0
Print “the value of r is not valid” THEN display error message “M must be positive”
𝑛)
Else 𝑆𝑛 = 𝑎(1 − 𝑟 𝑅 ELSE
End if END IF
Print 𝑆𝑛 Display M
Stop Stop
Construct a flow chart corresponding to Draw the corresponding flow chart for the information
the pseudo code above. given above.
6. The flow chart below shows the steps in calculating the volume of a solid given the base area
(A) and the height (h). Write a pseudo code corresponding to the program.
Start
Enter A
No
Enter h
No
V = A*h
Display V
Stop
Begin
Enter length
If length < 0
Then display “error message and re-enter positive length
Else enter height
If height < 0
Then display “error message” and re-enter positive height
1
Else volume = ∗ 𝑙 ∗ 𝑙 ∗ ℎ
3
End if
Display volume
End
Start
Enter radius
If radius < 0
Then display “error message” and re-enter positive radius
Else enter slant height
If slant height < 0
Then display “error message” and re-enter positive slant height
Else Area = 𝜋 ∗ 𝑟 ∗ (𝑟 + 𝑠𝑙𝑎𝑛𝑡 ℎ𝑒𝑖𝑔ℎ𝑡)
End if
Display Area
Stop
Draw the corresponding flow chart for the information given above.
9. Study the flow chart below and write a pseudo code corresponding to it.
Enter l
Is Yes
l < 0? Error” l must be positive”
No
Enter h
Is Yes
h < 0? Error” h must be positive”
No
Volume =1/3*l^2*h
Display volume
Stop
Start
Enter length
If length < 0 THEN
Display error message “length must be positive”
Else enter height
If height < 0 THEN
Display error message “height must be positive”
1
Else volume = 3 ∗ 𝑙𝑒𝑛𝑔𝑡ℎ ^3 ∗ ℎ𝑒𝑖𝑔ℎ𝑡
End if
Display volume
Stop
Start
Enter 𝑥
Is
Yes Print error “𝑥 must
𝑥 < 0?
be positive”
No
𝑟 = 𝑅 ∗ cos 𝑥
Display r
Stop
Start
Enter R, r, l
IF r > R THEN
Print “Error the value of r is not valid”
ELSE S = 𝜋 ∗ (𝑅 + 𝑟) ∗ 𝑙
END IF
Print S
Stop