Vous êtes sur la page 1sur 46

CS113: Introduction to Programming

BEE4D

Samin Khaliq
Lecture 2: Outline

Pseudocode

Flowcharts
Problem Solving and
Implementation
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
Developing a program

Problem solving
phase

Implementation
phase
Definitions
Pseudocode
Informal high level description of computer
instructions written in ordinary natural
language (e.g., English, French, German, etc.).

Flowchart
Diagrammatic representation of steps for
solving the given problem.

Program
A precise sequence of steps to solve a
particular problem.
Problem 1

Problem
Calculate the area of a rectangle.

Pseudocode
INPUT: height of rectangle, width of rectangle
PRINT area = height times width
Problem 2

Read in the temperature. If the


temperature is less than 32, indicate it as
below freezing on the screen. Else if the
temperature is above freezing then
indicate the same on the monitor screen.

Divide the above problem into


manageable parts.
Input
Processing
Output
Pseudocode

Input: Temp
if Temp < 32 then
Print BELOW FREEZING
else
Print ABOVE FREEZING
end
Problem 3

Problem: Print numbers from 0 to 9

Pseudocode:
count assigned 1
while count < 10
{
Print count
Increment count
}
Basic Logic Structures

1. Sequence

2. Selection (IF...THEN...ELSE or
IF....THEN)

3. Iteration (WHILE or DO...WHILE)


Flowchart Basic Blocks
Flowchart Basic Blocks
Sequence Structure
Pseudocode instructions are written in the
order, or sequence, in which they are to be
performed
Example of a sequence structure
Another Example
Sequence Structure Exercise

Draw a flowchart that will read the two


sides of a rectangle and calculate its area.
Selection Structure

Also known as decision logic, is used for


making decisions.
Used for selecting the proper path out of
the two or more alternative paths in the
program logic.
Selection logic is depicted as either an
IF...THEN...ELSE or IF.....THEN structure.
IF THEN ELSE
IF THEN
Example

Pseudo Code

IF Tea
Then Pour a cup of
Tea
Else Pour a cup of

Coffee
Example
Exercise

Write pseudocode and then draw a


flowchart to determine whether a value
entered by a user is positive or negative.
Exercise

WRITE "Enter a number"


READ N

IF N < 0 THEN
WRITE "Negative number"
ELSE
WRITE "Positive number"
ENDIF
END
Flowchart
Derived Structure: Case

Case structure is useful in representing a


series of IFTHENELSE statements
where there are more than two choices to
be made.
Case Structure
CASE drink
Lemon
Pour juice into
glass from Lemon
jug.
Orange
Pour juice into
glass from Orange
jug.
Pineapple
Pour juice into
glass from Pineapple
jug.
END CASE
Class Exercise

Write pseudocode and then draw flowchart


that reads two values, determines the
largest value and prints the largest value
with an identifying message.
Iteration Structure

Used when one or more instructions may


be executed several times depending on
some condition.

It uses two structures


WHILE
DO WHILE
Iteration (Repetition) structure

While structure Do-while structure

count assigned 0 count assigned 0


While count < 5 Do
Display Hello !!!" Display Hello !!!"
Increment count Increment count
Endwhile While count < 5
WHILE Structure

Check
Process
Condition
DO WHILE Structure

Process

Check
Condition
Difference between WHILE & DO
WHILE

WHILE: The body of the loop may never


get executed.

DO WHILE: The body of the loop is


executed at least one time regardless of
the results of the conditional.
Difference between WHILE & DO
WHILE
Example: WHILE & DO WHILE
Class Exercise

Figure a Figure b

Question: A certain file contains 5 data records. Which of the flowcharts


shown in Figures (a) and (b) will read 5 and only 5 data records?
Example ATM

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
Pseudocode: Example

Approach ATM If option = deposit then


Output 'Enter amount to
Repeat deposit'
Output 'Select option' Input amount
balance balance +
Output '1) Make amount
withdrawal' If option = withdrawal then
Output '2) Make Output 'Enter amount to
withdraw'
deposit' Input amount
Output '3) Show balance balance
amount
balance'
if option=show balance then
Output '4) Quit' Output 'Balance is ' balance
Input option Until option = quit
Stop
Class Exercise

Convert the
flowchart
shown in figure
into
pseudocode
for finding the
largest of three
numbers.
Class Exercise
Write pseudocode and draw a flowchart to
a) read an employee name (NAME), overtime
hours worked (OVERTIME), hours absent
(ABSENT) and
b) determine the bonus payment (PAYMENT).
Bonus Schedule
OVERTIME (2/3)*ABSENT Bonus Paid

>40 hours $50


>30 but 40 hours $40
>20 but 30 hours $30
>10 but 20 hours $20
10 hours $10
Exercise

Write a pseudocode to determine a students


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

Input a set of 4 marks


Calculate their average by summing and dividing
by 4
if average is below 50
Print FAIL
else
Print PASS
Exercise
This example illustrates the policy of a bank or a financial
institution for giving a loan to an individual.
Input mortgage amount
IF amount < 25,000
THEN
down payment = 3% of amount
ELSE
payment1 = 3% of 25,000
payment2 = 5% of (amount - 25,000)
down payment = payment1 + payment2
END IF
print down payment
Exercise
The following program in pseudocode form illustrates the policy
of a company to give the commission to a sales person.
Input sales
IF sales < 500
THEN
Commission = 2% of sales.
ELSE
IF sales < 5000
THEN
Commission = 5% of sales
ELSE
Commission = 10% of sales
ENDIF
END IF
Print Commission
Exercise: Draw the flowchart
Tools

Pseudocode
MS Word 2010

Flowcharts
Visual Logic 2.2.4
Microsoft Visio 2010
Summary

Given problem

Pseudocode

Flowcharts

Vous aimerez peut-être aussi