PREVIOUS
TOPIC:
Variables &
Data Types
Dr. Ronaldo S. Tinio
Professorial Lecturer
ACT_1
Variables &
Data Types
Identify the data types of the
following variables –
character, integer, floating-
point, boolean, string.
studentName allowance
yearLevel civilStatus
gender address
age interestRate
course idNumber
salary absent
graduating weight
ACT_2
Operators
Determine the result of the ff.
expressions using the order of
precedence.
Assume that A = 2, B = 4, C = 3, D = 5
1. (B / A) + D * C = (4/2) + 5 * 3 = 2 + 5 * 3 =
2 + 15 = 17
2. (B / A) – D + B * C
3. (B * C) + D – C
4. C%A–B*C
5. A+B*C–D%A
Determine the truth value of
the ff. statements using the
given values of A, B, C, & D.
1. (A + B) > 10 AND A < B
= (2 + 4) > 10 AND 2 < 4
= 6 > 10 AND 2 < 4
= false AND true
= false
1. A <> 10 OR B > 5
2. B / A < 10 AND NOT D – 2 > 5
3. A <> 2 OR B >= 2 AND A + B == 6
4. B – A > 5 OR NOT C <> A
LESSON 3
Algorithm
Representation
Algorithm
According to Donald Knuth, algorithms are the
threads that tie together most of the subfields of
computer science.
An algorithm is:
1. an ordered sequence of instructions for doing a
task or solving a problem
2. a finite set of discrete statements(or steps) in some
particular sequence(or order) to accomplish
a predefined particular task
A computer program is simply an algorithm for a
computer that is written in a programming
language.
Nature of Algorithm
Algorithms
are used in many activities and
come in many forms.
Instructions for assembling kits (e.g. furniture,
toys), recipes, steps for processing credit
card approvals, directions to a destination,
all involve algorithms.
It
is important that algorithms are
unambiguous and precise as possible.
Properties of an Algorithm
An algorithm must have the following
properties:
1. Input(s): An algorithm must have one(1) or
more pre-specified input(s), taken from a
specified set of objects.
2. Output(s): An algorithm must have one(1)
or more output(s), which have a specified
relation to the inputs.
Properties (continued)…
3. Definiteness: Each step must be precisely
defined; the actions to be carried out
must be rigorously and unambiguously
specified for each case.
4. Finiteness: The algorithm must always
terminate after a finite number of steps
and in finite(tolerable) amount of time.
Properties (continued)…
5. Effectiveness: All operations to be
performed must be sufficiently basic that
they can be done exactly and in finite
length.
6. Correctness: An algorithm should always
produce correct result with respect to it's
domain (of the inputs).
Problems vs Algorithms vs Programs
For each problem or class of problems,
there may be many different algorithms.
For each algorithm, there may be many
different implementations (programs).
Expressing Algorithms
An algorithm may be expressed in a
number of ways, including:
1. Natural language: usually verbose and
ambiguous
2. Flowcharts: graphically shows logic
in solution algorithm; difficult to modify
w/o specialized tools; largely
standardized
Expressing Algorithms (continued)
3. Pseudo code: an artificial and informal
language; vaguely resembles common
elements of programming languages; no
particular agreement on syntax
4. Programming language: tend to require
expressing low-level details that are not
necessary for a high-level understanding
Common Elements of Algorithms
Acquire data (input):
some means of reading values from an
external source; most algorithms require
data values to define the specific problem
(e.g., coefficients of a polynomial)
Common Elements of Algorithms
Selection:
somemeans of choosing among two or
more possible courses of action, based
upon initial data, user input and/or
computed results
Common Elements of Algorithms
Computation:
somemeans of performing arithmetic
computations, comparisons, testing logical
conditions, and so forth...
Common Elements of Algorithms
Iteration:
some means of repeatedly executing a
collection of instructions, for a fixed number
of times or until some logical condition
holds
Common Elements of Algorithms
Report results (output)
some means of reporting computed results
to the user, or requesting additional data
from the user
Steps in Algorithm
The steps are normally "sequence,"
"selection," "iteration," and a case-type
statement.
Pseudo codes
These consists of short, English phrases
used to explain specific tasks within a
program's algorithm.
These should not include keywords in any
specific computer languages.
These should be written as a list of
consecutive phrases.
Indentation can be used to show the
logic in pseudo codes as well.
Pseudo code Statements
At the moment we will be looking at three main
statements (operations): Assignment, Input, and
Display
Assignment
Assignment is used to store a value. This might
involve either (a) simply storing a value or (b)
calculating the answer to an arithmetic problem
and then storing the result.
The equals sign (=) is usually used to indicate
assignment.
(a) Storing a Value
Example: total = 0
The first example means: store zero in the
variable named total.
Pseudo code Statements
(b) Arithmetic Calculations
Arithmetic operators are used in calculations.
The operators are:
Operator Meaning Example
() Parenthesis. Grouping y = (a + b) * (c + d)
* Multiply x=a*b
/ Divide average = total / count
+ Add i=i+1
- Subtract y = x - 0.5
Example: area = length * width
The first example means: multiply the value
stored in length by the value stored in width and
store the result in area.
Pseudo code Statements
Input
Display a message asking the user for a value and
store the value typed by the user in a variable.
Example: Input custName
The example means display a message asking the
user to input a customers name and store the value
typed by the user in the variable called custName.
Pseudo code Statements
Display
Displays data on the computer screen (monitor).
Examples:
Display "Width = ", width
Display "Hello World"
Display grossIncome, taxPayable
Values in quotation marks are displayed exactly as
stated (minus the quotation marks)
The values held in variables are displayed rather
than the variable name.
For example, if the variable width held the value 72,
the first Display statement above would display the
following: Width = 72
Pseudo code: Example
Write a program that allow the user to input two integers and
display the sum of these integers.
Initialize sum to 0
Prompt the user to enter the first integer
Read user's first integer input
Prompt the user to enter a second integer
Read user's second integer input
Add first integer and second integer
Store the result in an sum variable
Display an output prompt that indicates the answer as
the sum
Display sum
Pseudo code: Example
Write a program that will display a remark of “passed” if the
student grade is greater than or equal to 75, otherwise, the
remark is “failed.”
Prompt the user to enter student’s grade
Read grade
If student's grade is greater than or equal to 75
Display "passed"
Else
Display "failed"
End if
Pseudo code: Example
Write a program that will input the grades of ten students and
output their class average.
Set total to zero
Set grade counter to zero
While grade counter is less than ten
Read the next grade
Add the grade into the total
Add one to the grade counter
End while
Set the class average to the total divided by
ten
Display the class average
Keywords that should be used
Get, Read, Input
Set, Initialize
Generate, Compute, Process, Calculate, etc.
Reset, Increment, Decrement, Add, Subtract,
Multiply, Divide, Edit
Print, Display, Output, Prompt
Test, Compare
If...Endif
While...Endwhile; Do Until...Enddo;
Case...Endcase
Call; Return
ACT_1
Sequence
Constructs
Write pseudo codes for the
following cases/scenarios.
1. Design a pseudo code specification for
a program that reads in two integers
and then outputs their sum, difference,
and product.
2. A class has four exams in one term.
Design a pseudo code specification
using a sequence structure for programs
that reads in a student’s four exam
scores, as integers, and outputs the
student’s average.
3. A Celsius (centigrade) temperature C
can be converted to an equivalent
Fahrenheit temperature F according to
the following formula: F = (9/5)*C + 32.
4. Design a pseudo code specifications for
programs that reads in a Celsius
temperature and then outputs the
equivalent Fahrenheit temperature
ACT_2
Selection
Contructs
Write pseudo codes for the
following cases/scenarios.
1. The IT Department checks whether a student
is qualified to pursue IT courses based on his
general weighted average. Design a
pseudo code specification using a selection
structure for programs that will obtain
student’s average and will determine and
display the a message indicating whether as
student is qualified or not.
2. One large chemical company pays its
salespersons on a commission basis. The
salespersons receive P3500 per week plus 10
percent of their gross sales for that week if
the sales quota of P10,000 has been met. For
example, a salesperson who sells P10, 000
worth of chemicals in a week receives P3500
plus 10 percent of P10, 000, or a total of
P4500. Design a pseudo code specification
using a selection structure for programs that
will obtain salesperson’s gross sales and will
calculate and display the salesperson’s total
earnings.
NEXT:
Flowcharts