Anda di halaman 1dari 20

CS001P

Algorithms
An Algorithm is a step-by-step solution to a
problem.
It is like a cooking recipe for mathematics.
An algorithm is a set of rules that precisely defines a
sequence of operations
Algorithm is the step-by-step sequence of
instructions that describe how the data is to be
processed to produce the desired output

Logic
a proper or reasonable way of thinking about or
understanding something
a particular way of thinking about something
the formal systematic study of the principles of valid
inference and correct reasoning
the science that studies the formal processes used
in thinking and reasoning
Art of reasoning

Algorithms as applied to
Real-Life Situations
Algorithm for posting a Status in Facebook
Algorithm for Searching in Google
Algorithm for Sending SMS using your
Cellphone
Algorithm for Saving a Microsoft Document
Algorithm for Frying Sunny Side-Up Egg

Pseudocode and Flowchart

When programmers plan the logic for a solution


to a programming problem, they often use one
of two tools, PSEUDOCODE (pronounced suedoe-code) or FLOWCHARTS

Pseudocode
An English-like representation of the same thing.

PSEUDO = FALSE
CODE = A program means to put in a
programming language

Written in a computer programming language but do not


necessarily follow all the syntax rules of any specific language
An artificial and informal language that helps programmers
develop algorithms.
Also known as Program Design Language (PDL).

Advantages in Using Pseudocode


It bridges the gap between human language and
computer language.
It is an intermediate notation that allows expression
of program logic in a straight forward, easy to
understand manner without concerning the
programmer with syntax details.
It is easier to make changes to pseudocode than to a
source program in a high-level language.
It allows for structured walkthroughs.

Rules for Pseudocode


There are rules to be followed when using
pseudocode. However, many versions of
pseudocode exist, and there is no industry
standard. The rules described below are
adhered to by most organisations that use
pseudocode.

The pseudocode must be language independent. Try to avoid the use of


words peculiar to any programming language.
Indent lines to make the pseudocode easy to read and understand.
Show key words in CAPITAL LETTERS.
Example:
IF condition THEN
DO action
ENDDO
ENDIF
Punctuation is optional.
Every IF must end with an ENDIF.
Every DO, DO FOR and DO WHILE must end with ENDDO.
The main routine (the one that goes from START to STOP) has to be shown
first. All other routines are to follow.

Pseudolanguage Instructions

Example Problem 1:
We want to be able to enter the ages of two
people and have the computer calculate their
average age and display the answer.

Answer:
1. DISPLAY a message asking the user to enter the first age
2. GET the first age and STORE to AGE1
3. DISPLAY a message asking the user to enter the second
age
4. GET the second age and STORE to AGE2
5. CALCULATE AVG_AGE by ADDING AGE1 and AGE2
DIVIDE BY 2
6. DISPLAY AVG_AGE on the screen

Example Problem 2:
Create a pseudo-code that will convert
Fahrenheit temperature to Celsius
temperature.

1. DISPLAY a message asking for an input for


temperature in Fahrenheit
2. GET the input and STORE it to ftemp
3. CALCULATE ctemp using the formula (5 / 9) *
(ftemp - 32)
4. DISPLAY the value of ctemp on the screen

Board Work

Problem 1: Compute for the Area of the Square.


Problem 2: Compute for the Area of Rectangle
Problem 3: Compute for the Area of a Circle
Problem 4: Compute for Total Cost given Unit
Price and Quantity
Problem 5: Compute for the Salary given Hrs.
Worked and Hourly Rate