You are on page 1of 17

TEST 1

7 NOV 2017 (Tuesday)


3:00 pm – 5:00 pm
Dewan Serbaguna
Algorithms
Step‐by‐Step Procedures to 
Solve Engineering Problems 
Using Programming 

Prepared by Assoc Prof Dr Junita Mohamad Saleh 2


Introduction to Algorithms
• 2 types of algorithms:
1. Flowchart
• A diagram which shows the flow of operations 
• Must use correct symbols for each operation

2. Pseudocode
• List of instructions in correct sequence
• Must use simple language

Prepared by Assoc Prof Dr Junita Mohamad 
3
Saleh
Flowchart: uses symbols

• Start/end start end

• Input/Output/
Get A, B C=319 Display C
Assignment
Print V Output M

• Process R= R1 + R2
Calculate power = I*V
Compute regression
4
Prepared by Assoc Prof Dr Junita Mohamad Saleh
(cont) Flowchart: uses symbols
• Condition: if
No/false
if C > 600 if N == 0?
Yes/true

No/false Yes/true

• Loop: while
No/false
C > 600 ? N != 0?
Yes/true

No/false Yes/true

Prepared by Assoc Prof Dr Junita Mohamad Saleh 5


(cont) Flowchart: uses symbols

• Function execution
• (use function name in the symbol)

Calc_Power (I, V) Display_Result ( )

6
Prepared by Assoc Prof Dr Junita Mohamad Saleh
(cont) Flowchart: uses symbols

• Continue on a new page (a) (a) H H

• Continue on the same page (a) (a) A A

• Control flow
– Control flow can point forward, backward or in a loop.

Prepared by Assoc Prof Dr Junita Mohamad Saleh 7


Pseudocode: uses English words
• Must begin with the word “start”

• Must end with the word “end”

• Words commonly used in a pseudocode:
– Input: get, enter, assign or use assignment statement e.g. 
A=38
– Process: compute, calculate, sort, arrange or use equation
– Condition: if, while, for, do‐while

Prepared by Assoc Prof Dr Junita Mohamad 
8
Saleh
Example 1
• Draw a flowchart and write the pseudocode for a process 
to print the sentence “Learn C++ without fear”
Flowchart Pseudocode

start
start
Print “Learn C++ without fear”
Display “Learn C++ 
without fear” end

end
Prepared by Assoc Prof Dr Junita Mohamad 
9
Saleh
Example 2: Flowchart
start
• Draw a flowchart and 
write a pseudocode for  get 
a program which  A, B
computes the addition 
of two integer values,  C=A+B
A and B and displays 
the result.
print C

end

Prepared by Assoc Prof Dr Junita Mohamad 
10
Saleh
Example 2: Pseudocode
Write a pseudocode to compute the addition of two integer 
values.

Start
Get value of first integer, A
Get value of second integer, B
Compute total, C=A+B
Display C
End

Prepared by Assoc Prof Dr Junita Mohamad 
11
Saleh
Quiz 1
Prepare a flowchart  for a C++ program 
which calculates the total resistance for 
a circuit with 3 resistors in series.  The 
resistance values are obtained from 
user. Then display the result.

Prepared by Assoc Prof Dr Junita Mohamad 
12
Saleh
Answer to Quiz 1

start

get 
R1, R2 and R3

Total = R1 + R2 + R3

print Total

end
13
Prepared by Assoc Prof Dr Junita Mohamad Saleh
Example1: User-defined functions
Write a program to get two numbers and print the
numbers either in ascending or descending order based
on user preference.

 Identify inputs and outputs:


○ Input: number1, number2, choice
○ Output: large, small

 Decide on the functions needed:


1. Get_choice(choice)
2. Get_number(number1, number2)
3. Sort_number(number1, number2, large, small)
4. Display_ordering(choice, large, small)
--Cont – Example1:
Flowchart of main program
choice == ‘a’ no
start get_choice
|| ‘A’||’d’||’D’

yes
get_number

sort choice == Display “Error:


no ’a’||’A’ invalid choice”
yes

Display large Display small


Display small Display large
end
Example2: User-defined functions
Write a program which asks the user to choose whether to
compute the perimeter or the area of a rectangle or square.
Then, it asks the user for the length and width of the rectangle.
Finally it does the computation and displays the result.

 Identify inputs and outputs:


○ Input: length, width, choice
○ Output: result of perimeter or area

 Decide on the functions needed:


1. Get choice of computation (choice) // ‘p’, ‘P’, ‘a’ or ‘A’
2. Get inputs (length, width)
3. Compute perimeter (length, width)
4. Compute area (length, width)
5. Display result (choice, length, width, result)
--Cont – Example2:
Flowchart of main program
choice == ‘p’ no
start Get_choice
|| ‘P’||’a’||’A’

yes
GetInputs

yes choice == no
’a’||’A’
Compute_
Compute Perimeter
_Area

Display
result end

You might also like