You are on page 1of 50

Computer

Programming I
Compiled by:

Mary Ann B. Taduyo


Learning Outcome
 Understand the use of program design tools.
 Develop algorithms for solving problem.
 Evaluates the best algorithms for solving the
problem
Program Design Tools
Program Design Tools:
Pseudocode
 Pseudocode is an English-like nonstandard
language

 A very high level quite informal description of a


program that typically contains the essential
elements.

 Logic of the program but does not include all the


details that will be required for the actual code.
Example of
Pseudocode
Program Design Tools:
Pseudocode
Program Design Tools:
Pseudocode
Program Design Tools:
Algorithm
 A finite set of instructions which when followed
accomplish a particular task. Is also defined as
step by step instructions.

 Algorithm can be defined as: “A sequence of


activities to be processed for getting desired
output from a given input.”
Program Design Tools:
Algorithm
 While writing algorithms we will use following
symbol for different operations:
‘+’ for Addition ‘/’ for Division and
‘-’ for Subtraction ‘=’ for Assignment
‘*’ for Multiplication ‘%’ for modulo division
Example of Algorithm
Algorithm

Problem 1:
Find the area of a Circle of
radius r
Algorithm
Problem 1:
Find the area of a Circle of radius r.
Inputs to the algorithm:
Radius r of the Circle.
Expected output:
Area of the Circle
Algorithm
Problem 1:
Find the area of a Circle of radius r.
Inputs to the algorithm: Radius r of the Circle.
Expected output: Area of the Circle
Algorithm:
Step1: Read/input the Radius r of the Circle
Step2: Area = PI*r*r (calculation of an area)
Step3: Print Area
Algorithm

Problem 2:
Write an algorithm to read
two numbers and find their
sum.
Algorithm
Problem 2:
Write an algorithm to read two numbers and find their
sum.
Inputs to the algorithm:
First num1, Second num2
Expected output:
Sum of the two numbers
Algorithm
Algorithm:
Step 1: Start
Step 2: Read/input the first num1
Step 3: Read/input the second num2
Step 4 :Sum = num1+num2(calculation of sum)
Step5: Print Sum
Step6: End
Algorithm

Problem 3:
Write an algorithm that will
convert temperature Fahrenheit to
Celsius
Algorithm
Problem 3:
Convert temperature Fahrenheit to Celsius
Inputs to the algorithm:
Temperature in Fahrenheit (F)
Expected output:
Temperature in Celsius (C)
Algorithm
Algorithm:
Step1: Start
Step 2: Read Temperature in Fahrenheit
(F)
Step 3: C=5/9*(F-32)
Step 4: Print Temperature in Celsius ( C )
Step5: End
Algorithm
Types of Algorithms
•The algorithm and flowchart, classification to the
three types of control structures. They are:

• Sequence

• Branching (Selection)

• Loop (Repetition)
Algorithm
Types of Algorithms
 The sequence is exemplified by sequence
of statements place one after the other –
the one above or before another gets
executed first.
Algorithm
Types of Algorithms -
sequence
Algorithm
Types of Algorithms
 The branch refers to a binary decision based on
some condition. If the condition is true, one of the
two branches is explored; if the condition is false,
the other alternative is taken.
Algorithm
Types of Algorithms -
branch
Example of Algorithm
– branch type
Algorithm – branch type

Problem 1:
Write an algorithm to find
the greater number between
two numbers.
Algorithm – branch type
Problem 1:
Write an algorithm to find the greater number between
two numbers. Display the greater value.
Step 1: Start
Step 2: Read/input A and B
Step 3: If A greater than B then C=A
Step 4: If B greater than A then C=B
Step 5: Print C
Step 6: End
Algorithm – branch type

Problem 2:
Write an algorithm to find
the largest value of any
three numbers.
Algorithm – branch type
Problem 2:
Write an algorithm to find the largest value of any three
numbers. Display the largest number.
Step 1: Start
Step 2: Read/input A, B and C
Step 3: If (A>=B) and (A>=C) then Max=A
Step 4: If (B>=A) and (B>=C) then Max=B
Step 5: If (C>=A) and (C>=B) then Max=C
Step 6: Print Max
Step 7: End
Algorithm
Types of Algorithms
 The loop allows a statement or a sequence of
statements to be repeatedly executed based on
some loop condition.
 A trip around the loop is known as iteration.
 The loop is also known as the repetition structure.
Algorithm
Types of Algorithms –
loop type
Example of Algorithm
– loop type
Algorithm – loop type

Problem 1:
Write an algorithm to
calculate even numbers
between 0 and 99.
Algorithm – branch type
Problem 1:
Write an algorithm to calculate even numbers between
0 and 99.
Step 1. Start
Step 2. I=0
Step 3. Write I in standard output
Step 4. I = I+2
Step 5. If (I <=98) then go to line 3
Step 6. End
Algorithm – branch type

Problem 2:
Design an algorithm which
gets a natural value, n, as its
input and calculates odd
numbers equal or less than n.
Algorithm – branch type
Problem 2:
Design an algorithm which gets a natural value, n, as its
input and calculates odd numbers equal or less than n
Step 1. Start
Step 2. Read n
Step 3. I = 1
Step 4. Write I
Step 5. I = I + 2
Step 6. If ( I <= n) then go to line 4
Step 7. End
Algorithm – branch type
Problem 3:
Design an algorithm which
generates even numbers
between 1000 and 2000 and
then prints them in the
standard output. It should also
print total sum:
Algorithm – branch type
Problem 3:
Design an algorithm which generates even numbers
between 1000 and 2000 and then prints them in the standard
output. It should also print total sum:

• Step 1. Start • Step 6. If (I <= 2000)


• Step 2. I = 1000 and then go to
line 3 else go
S=0
to line 7
• Step 3. Write I
• Step 7. Write S
• Step 4. S = S + I
• Step 8. End
• Step 5. I = I + 2
Algorithms
 An algorithm is a logical sequence of steps
leading to the solution of ONE TASK in a
bigger problem
– Define expected OUTPUT
– Describe desired INPUT
– Determine PROCESSING needed to
produce desired output from expected
input
Algorithm
Properties of
Algorithms
 Donald Ervin Knuth has given a list of five
properties for an algorithm, these properties
are:
Algorithm
Properties of Algorithms
1. Finiteness: An algorithm must always
terminate after a finite number of steps.

2. Definiteness: Each step of an algorithm must


be precisely defined. It is done by well thought
actions to be performed at each step of the
algorithm.
Algorithm
Properties of Algorithms
3. Input: Any operation you perform need some
beginning value/quantities associated with
different activities in the operation.
4. Output: One always expects output/result
(expected value/quantities) in terms of output from
an algorithm.
5. Effectiveness: Algorithms to be
developed/written using basic operations.
Be Ready for Guided
Exercises in Pseudocode
and Algorithms
Guided Exercises 2

1. Given the three numbers A, B, C. Write an


algorithm to computes and print out the sum, the
average, and the product of these values.

2. Write an algorithm that will ask the user to get final


grades. Evaluate the final grade, if the final grade is
greater than75, indicate the remarks are “Passed”, if
not the remarks are “Failed”.
Guided Exercises 2
Direction:
> Write your algorithm in one sheet yellow paper
or bond paper or use MS Word directly write your
algorithm in canvas
> If you use yellow paper or bond paper,
capture/screenshot your answer, then upload it to
Canvas.
> File naming convention is: PrelimPT2
Discussion of answers in
Guided Exercises 2
Research Work 1:
Search for the Flowcharting
symbols.

Directions:
 Follow the sample format below.
 Use MSWord
 File naming convention: PrelimP3.doc
 Submit your assignment in canvas
 Look for the rubrics attached in canvas
Reminders
Review and be
ready for a
seatwork
exercises next
meeting on
algorithm
Thank you and Happy
Day Everyone

You might also like