Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

CS1002

Programming Fundamentals
2
Algorithms: Pseudocode
3 Algorithms

Algorithm is a process that a computer could carry out to


complete a well-defined task within finite time and resources
The objective of computer science is to solve problems by
developing, analyzing, and implementing algorithmic solutions
4
Algorithms

An algorithm is a procedure for solving a problem in terms of


the actions to be executed and the order in which those actions
are to be executed.
An algorithm is merely the sequence of steps taken to solve a
problem. The steps are normally "sequence," "selection, "
"iteration," and a case-type statement.
5 Pseudocodes

Pseudocode: is an informal English-like


representation of the program's algorithm.

Pseudocode is written more like the actual


code that will be used to write the program.
Pseudocodes
6

Pseudocode is an artificial and informal language


that helps programmers to develop algorithms.
Pseudocode is a "text-based" detail (algorithmic)
design tool.

The rules of Pseudocode are reasonably


straightforward. All statements showing
"dependency" are to be indented. These include if,
switch, while, do, for.
7 Algorithms & Pseudocode
 A typical programming task can be divided into two phases
 Problem Solving phase:
 Produce an ordered sequence of steps that describe solution of problem
 This sequence of steps is called an algorithm
 Implementation phase:
 Implement the program in some programming language
8 Sample problem
 Input two numbers from the user and print the sum of those two
numbers?
Analyze the problem:
 What are the inputs?
 Two number (Where to store these)
 Need two containers
 What is the output?
 A result of sum of the two numbers (Where to store it)
 Need another container
 What is the process?
 Perform the arithmetic operation of summation
9 Sample problem
Plan the solution:
Steps Involved (Algorithm)
1. Take input of one number and store in a container named as num1
2. Take input of another number and store in another container named as num2
3. Perform operation num1 + num2 and store the result in another container names
results
4. Print the value in results on the screen
10 Sample problem (Pseudo code)
1. declare num1, num2, results
2. input num1
3. input num2
4. results 🡨 num1 + num2
5. Print results
11 Examples
 1.. If student's grade is  2. Set total to zero
greater than or equal to 60

 Set grade counter to one


 Print "passed"
 else
 Print "failed"  While grade counter is less than or
equal to ten

 Input the next grade


 Add the grade into the total
 Set the class average to the total
divided by ten

 Print the class average.


12 Some Keywords That Should be Used
For looping and selection,
The keywords that are to be used include
Do While...EndDo; Do Until...Enddo; Case...EndCase; If...Endif; Call
... with (parameters); Call; Return ....; Return; When;

Always use scope terminators for loops and iteration.

As verbs, use the words Generate, Compute, Process, etc.


Words such as set, reset, increment, compute, calculate, add, sum,
multiply, ... print, display, input, output, edit, test , etc.
with careful indentation tend to foster desirable pseudocode.

Do not include data declarations in your pseudocode.


13 Pseudo-Code: Decision Making

 If-then
 General form:
if (condition is met) then
statement(s)

Example:
if temperature < 0 then
wear a jacket
14 Pseudo-Code: Decision Making
 If-then
 General form:
if (condition is met) then
statement(s)

Example:
if temperature < 0 then
wear a jacket
15 Pseudo-Code: Decision Making

If-then-else
General form:
if (condition is met) then
statement(s)
else
statements(s)
Example:
if (at work) then
Dress formally
else
Dress casually
16 Decision Making

Commonly used relational operators in expressions:-

> Greater Than


< Less Than
= Equals To
<> Not Equals To
>= Greater Than or Equals To
<= Less Than or Equals To
() Brackets used for prioritising certain calculations
17 Decision Making
Compound expressions can be represented
using the following operators:-

AND Every expression must evaluate to be


true in order for the whole expression to
be true.
OR As long as any one of the expression
can be true, the entire IF statement will
be true.
NOT The inverse (opposite) of the entire
expression.
Decision Making
18
For example, this statement.........
IF (Salary>4000) And (CreditCard=YES) THEN
Say “Yes I Will Take The Job!!”
ENDIF
can be represented like this.........
IF (Salary>4000) THEN
IF (CreditCard=YES) THEN
Say “Yes I Will Take The Job!!”
ELSE
Say “No Credit Card?”
Say “Sorry!!”
ENDIF
ELSE
Say “Not Enough Pay!!”
ENDIF
........ whereby more possibilities can be represented.
Decision Making
19
For good practice...........
IF (Salary>4000) THEN
IF (CreditCard=YES) THEN
Say “Yes I Will Take The Job!!”
ELSE
Say “No Credit Card?”
Say “Sorry!!”
ENDIF
ELSE
Say “Not Enough Pay!!”
ENDIF

........ ensure that statements are properly


indented to indicate block of statements
which belong together.
20 Pseudo-Code: Repetition (3)

while-do

 Repeat zero or more times (check condition before statement(s))

 General form:
while (condition is met)
statement(s)

Example:
while students ask questions
Answer questions
21 Algorithms and Pseudocode
 Take and input of four marks and calculate average. If the
average is above 50 the student is declared pass.
22 Algorithms and Pseudocode

Algorithm:
Input a set of four marks
Calculate their average by summing and dividing the sum by 4
If average is above 50
Print : “PASS”
23 Algorithms and Pseudocode

Pseudocode:
1.0 Declare M1, M2, M3, M4, average
2.0 Input M1, M2, M3, M4
3.0 average = (M1+ M2+ M3+ M4) / 4
4.0 if (average > 50) then
4.1 Print “PASS”
5.0 endif
24 Algorithms and Pseudocode

Example 1: Write an algorithm to determine a student’s final grade and


indicate whether he is passing or failing. The final grade is calculated as the
average of four marks.
25 Algorithms and Pseudocode

Algorithm:
 Input a set of four marks
 Calculate their average by summing and dividing the sum by 4
 If average is above 50
 Print : “Pass”
else
 Print “FAIL”
26 Algorithms and Pseudocode

Pseudocode:
 Step 1: Input M1, M2, M3, M4
Step 2: Grade = (M1+ M2+ M3+ M4) / 4
Step 3: if (Grade > 50) then
Print “PASS”
else
Print “FAIL”
endif
27 Pseudo-Code: Decision Making

If-then-else
General form:
if (condition is met) then
statement(s)
else
statements(s)
Example:
if (at work) then
Dress formally
else
Dress casually
28 Algorithms and Pseudocode

 Take and input of four marks and calculate average. If the average is
above 50 the student is declared pass otherwise failed.
29 Algorithms and Pseudocode

Algorithm:
 Input a set of four marks
 Calculate their average by summing and dividing the sum by 4
 If average is above 50
 Print : “PASS”
else
 Print “FAIL”
30 Algorithms and Pseudocode

Pseudocode:
1.0 Declare M1, M2, M3, M4, average
2.0 Input M1, M2, M3, M4
3.0 average = (M1+ M2+ M3+ M4) / 4
4.0 if (Grade > 50) then
4.1 Print “PASS”
5.0 else
5.1 Print “FAIL”
6.0 endif
31

Pseudocode – Comparing two numbers


32 Pseudocode – Comparing two
numbers
1.0 start
2.0 declare n1, n2
3.0 input n1,n2
4.0 if (n1 > n2)
4.1 print "n1 is greater"
5.0 endif
6.0 if (n2 > n1)
6.1 print "n2 is greater"
7.0 if (n2 = n1)
7.1 print "they are equal"
8.0 endif
9.0 end
33 Pseudo-Code: Fast Food Example

 Use pseudo-code to specify the algorithm for a person who is ordering food
at a fast food restaurant

 At the food counter, the person can either order or not order the following
items:
 a burger
 Fries
 a drink
 After placing her order the person then goes to the cashier and pays the
bill
34 Algorithm: Fast Food Example

1.0 Approach counter


2.0 if want burger then
2.1. order burger
3.0 if want fries then
3.1. order fries
4.0 if want drink then
4.1 order drink
5.0 Pay cashier
35 Pseudo-Code: Fast Food Example
(Computer)
1.0 Declare order_burger, order_fries, order_drink, Bill = 0
2.0 Print ‘Do you want to order burger?’
3.0 Input order_burger
4.0 if order_burger = yes then
4.1. Bill  Bill + 300
5.0 endif
6.0 Print ‘Do you want to order fries?’
7.0 Input order_fries
8.0 if order_fries = yes then
8.1. Bill  Bill + 100
9.0 Output 'Do you want to order drink?’
10.0 Input order_drink
11.0 If order_drink = yes then
11.1. Bill  Bill + 50
12.0 Pay Bill to cashier
36 Nested if/else Selection Structure

Nested if/else structures


 One inside another, test for multiple cases
 Once condition met, other statements skipped
37 Example 1– Comparing two numbers
with nested if-else
1.0 start
2.0 declare n1, n2
3.0 input n1,n2
4.0 if (n1 > n2)
4.1 print "n1 is greater"
5.0 else
5.1 if (n2 < n1)
5.1.1 print "n2 is greater"
5.2 else
5.2.1 print "they are equal“
5.3 endif
6.0 endif
7.0 end
38 Example 2: Grading with Nested if-else:

if student’s grade is greater than or equal to 90


Print “A”
else
if student’s grade is greater than or equal to 80
Print “B”
else
if student’s grade is greater than or equal to 70
Print “C”
else
if student’s grade is greater than or equal to 60
Print “D”
else
Print “F”
39 Example 2: Pseudocode for Grading
with Nested if-else:

if ( grade >= 90 ) then // 90 and above


Print "A"
else if ( grade >= 80 )then // 80-89
Print "B"
else if ( grade >= 70 ) then // 70-79
Print << "C";
else if ( grade >= 60 ) then // 60-69
Print "D";
else // less than 60
Print "F";
end
40 if/else Selection Structure

Compound statement
 Set of statements within a block

if grade >= 60 then


Print "Passed"
else
Print "Failed"
Print "You must take this course again"
endif
41 Example 3

1.0 Declare num1


2.0 Print “Enter a number”
3.0. input num1
4.0 if num1 > 0 then
4.1 print “You entered a positive number”, num1
5.0 else if num1< 0 then
5.1 Print “You entered a negative number” , num1
6.0 else
6.1 print “You entered 0”
7.0 endif
8.0 print “This line is always printed”
42 Example 4

 Write pseudocode that will calculate the roots of a quadratic equation


 ax2 + bx + c = 0

 Hint d = b2 – 4ac and roots are


 X1 = (-b + sqrt(d )) / 2a
 X2 = (-b – sqrt(d)) / 2a
 Real roots are only possible if d > 0
 Cater this as well
43 Example 5

 Write a pseudocode
 to read an employee name (NAME), overtime hours worked (OVERTIME), hours
absent (ABSENT)
 determine the bonus payment (PAYMENT)
Bonus Schedule
OVERTIME – (2/3)*ABSENT Bonus Paid

>40 hours 5000


>30 but  40 hours 4000
>20 but  30 hours 3000
>10 but  20 hours 2000
 10 hours 1000
44 Pseudo-Code: ATM Example
Use pseudo-code to specify the algorithm for an ATM bank
machine. The bank machine has four options:

1) Show current balance


2) Deposit money
3) Withdraw money
4) Quit
After an option has been selected, the ATM will continue
displaying the four options to the person until he selects the
option to quit the ATM
45
Pseudo-Code: ATM Example
If option = deposit then
Approach ATM
Output 'Enter amount to
Repeat deposit'
Output 'Select option' Input amount
Output '1) Make balance  balance + amount
withdrawal' If option = withdrawal then
Output '2) Make deposit' Output 'Enter amount to
Output '3) Show balance' withdraw'
Input amount
Output '4) Quit'
balance  balance – amount
Input option
If option = ‘Show Balance’
Output 'Balance is ' balance
Until option = quit
Stop
46

Thank you

You might also like