0% found this document useful (0 votes)
7 views

Program Logic Formulation

turbo c programming

Uploaded by

rooodychan143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Program Logic Formulation

turbo c programming

Uploaded by

rooodychan143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

Program Logic Formulation

Programming Languages
● Programming Language
– a standardized communication technique for
expressing instructions to a computer

– Like human languages, each language has its own


syntax and grammar

– There are different types of programming languages that


can be used to create programs, but regardless of what
language you use, these instructions are translated into
machine language that can be understood by
computers.

Jedi: Introduction to programming


Categories of Programming
Languages
● High-level Programming Languages
– a programming language that is more
user-friendly, to some extent platform-
independent, and abstract from low-
level
computer processor operations such
as memory accesses
– A programming statement may
be translated into one or several
machine instructions by a compiler.
Examples: Java, C, C++, Basic, Fortran
Jedi: Introduction to programming
Categories of Programming
Languages
● Low­level Assembly Language
– Assembly languages are similar to machine
languages, but they are much easier to program
because they allow a programmer to substitute
names for numbers

– Assembly languages are available for each CPU


family, and each assembly instruction is
translated into one machine instruction by an
assembler program

Jedi: Introduction to programming


Program Development Life
Cycle
●Basic steps in trying to solve a problem
on the computer:
1. Problem Definition
2. Problem Analysis
3.Algorithm design and representation
(Pseudocode or flowchart)
4. Coding and debugging

Jedi: Introduction to programming


1. Problem Definition
●A clearly defined problem is already half
the solution.
●Computer programming requires us to
define the problem first before we even try
to create a solution.
● Let us now define our example problem:
“Create a program that will determine the number of
times a name occurs in a list.”

Jedi: Introduction to programming


2. Problem Analysis
●After the problem has been adequately defined, the
simplest and yet the most efficient and effective approach
to solve the problem must be formulated.
●Usually, this step involves breaking up the problem into
smaller and simpler sub problems.
● Example Problem:
Determine the number of times a name occurs in a
list
● Input to the program:
list of names (let's call this nameList)
name to look for (let's call this
keyName)
● Output of the program:
the number of times the name occurs
in a list
Jedi: Introduction to programming
3. Algorithm Design and
representation
● Algorithm
a clear and unambiguous
specification of the steps needed
to solve a problem.
● It may be expressed in either :
-Human language (English,
Tagalog)
-Graphical representation like
a flowchart
-Pseudocode - which is a cross
programming
between human language andJedi: Introduction to programming
3. Algorithm Design &
representation–Human Lang.
● Expressing our solution through Human language:

1. Get the list of names, let's call this nameList


2. Get the name to look for, let's call this the keyname
3. Compare the keyname to each of the names in
nameList
4. If the keyname is the same with a name in the list, add 1
to the count
5. If all the names have been compared, output the result

Jedi: Introduction to programming


3. Algorithm Design and
representation ­Flowchart

Jedi: Introduction to programming


Flowchart Symbols

Jedi: Introduction to programming


Flowchart Symbols

Jedi: Introduction to programming


Flowchart Symbols

Jedi: Introduction to programming


3. Algorithm Design and
representation ­Pseudocode
○ Count
○ NameList
○ KeyName
● Count ← 0

NameList has names


Name == KeyName?
● Count ← Count
+1

● Count ← Count
● next name in
NameList

● Display Count
● Stop
More on Algorithms
FINITE – The problem must be solvable in a
finite number of operations
UNAMBIGUOUS – Each instruction must
have a unique interpretation.
ORDERED – Each instruction must have a
predecessor (except the start)
DEFINED INPUT/OUTPUT ­

pshscs2.tripod.com/files/plf.pdf
4. Coding and Debugging
●After constructing the algorithm, it is now
possible to create the source code. Using
the algorithm as basis, the source code
can now be written using the chosen
programming language.

● Debugging
– The process of fixing some errors (bugs)
in your program

Jedi: Introduction to programming


Problem Solving & Solution
Design Concepts
• Problem solving steps (specifically
algorithm definition) is similar in all
programming languages.

pshscs2.tripod.com/files/plf.pdf
Six Steps in Problem Solving
1. Identify the problem.
2. Understand the problem.
3. Identify alternative ways to solve the
problem.
4. Select the best ways to solve the
problem from the alternatives.
5. List instructions that enable you to solve
the problem from the selected method.
6. Evaluate the solution.
pshscs2.tripod.com/files/plf.pdf
Seven Basic Elements of
Programming
• Data: Constants, Variables
• Input: reading of values from input devices
(keyboard, disk drives, etc…)
• Output: writing of information to any output
device (disk drives, printer, etc…)
• Operations: comparing values, assigning
values, combining values.
• Conditions / Selections: If­Then­Else, Switch­
Case,
• Loops / Iterations: While, do­While, for­do,
repeat­until
• Subroutines / Modules: functions,
procedures pshscs2.tripod.com/files/plf.pdf
Problems that can be solved on
computers:
Computational
Problems involving some sort of
mathematical processing
Logical
Involve relational or logical
processing
Repetitive
Involve repeating a set of mathematical
and / or logical instruction.
pshscs2.tripod.com/files/plf.pdf
Logical Control Structures
• Elementary building blocks of structured
programs
• Statements that control the order in which
other program statements are executed
• Refer to different ways in which program
instructions may be executed

pshscs2.tripod.com/files/plf.pdf
Logical Control Structures
1. Sequence
2. Selection / Decision
3. Iteration / Loop
4. Case

pshscs2.tripod.com/files/plf.pdf
SEQUENCE
• Instructions are executed in the order in which
they appear
• “Step­by­step” execution of instructions

pshscs2.tripod.com/files/plf.pdf
SELECTION / DECISION
• A logical control structure that execute
instructions depending on the existence of a
condition
• Sometimes called an “If­Then­Else”
logical control structure

pshscs2.tripod.com/files/plf.pdf
ITERATION / LOOP
• A logical control structure indicating the repeated
execution of a series of steps (or instructions).

pshscs2.tripod.com/files/plf.pdf
CASE
• A logical control structure that is used when
there are numerous paths to be followed
depending on the content of a given variable.

pshscs2.tripod.com/files/plf.pdf
Example: Program Development
Flow

pshscs2.tripod.com/files/plf.pdf
DATA
Constant
–A value that never changes during the
processing of all the instructions in a
solution.
Variable
–The value of a variable does change
during processing.
– Also called as “identifier”

pshscs2.tripod.com/files/plf.pdf
CONSTANT
• Can be any type of data: numerical,
alphanumeric (or character), or special symbol
• Two (2) kinds of constants
–Literal: refers to the actual value itself (e.g.
3.1416, “pshs”)
–Named: uses a name or alias to represent an
actual or literal value (e.g. PI, school_name)

pshscs2.tripod.com/files/plf.pdf
VARIABLE
• Can be categorized by the kind of data it
can hold.
• They must hold data that are of the same
type, otherwise a mismatch error will
occur.
• Can be any type of data: numerical,
alphanumeric (or character), logical, or
special symbol

pshscs2.tripod.com/files/plf.pdf
Example: Constants & variables
on the computer
• Constants
8935084, ­1.5, 3.1416, “pshs”, “*”
• Variables
AGE=12, PRICE=99.99, CITY=“Quezon
City”, Student_Name=“Pisay dela Cruz”,
ZIP_CODE=“1008”, MARK=“A”,
End_of_File=False

pshscs2.tripod.com/files/plf.pdf
DATA TYPES
• Numeric
• Character
• Logical
• Date / Time

pshscs2.tripod.com/files/plf.pdf
Numerical Data
• Include all types of numbers (i.e., integers, non­
integers)
• The only data type that can be used in
calculations
• Subtypes:
– Integer: negative numbers & whole
numbers
– Real: decimal numbers
– Float: numbers in exponential /
scientific form
pshscs2.tripod.com/files/plf.pdf
Character Data
• Consists of all numbers, letters, and
specialcharacters available to the
computer (#, &, *, +, ­, 0­9, A­Z, a­z) and
placed within quotation marks.
• Cannot be used for calculations even if
they consist of only numbers.
• String: means a string of characters
• Concatenation: means joining of two or
more pieces of character or string data

pshscs2.tripod.com/files/plf.pdf
Logical Data
• Consist of two pieces of data in the data
set
– the words TRUE and FALSE.
• Logical data are used in making a yes or
no decision.

pshscs2.tripod.com/files/plf.pdf
OPERATORS
• Are the data connectors within
expressions and equations.
• They tell the computer how to process the
data.
• They also tell the computer what type of
processing needs to be done
(i.e., mathematical, relational, or
logical).

pshscs2.tripod.com/files/plf.pdf
Types of operators used in
calculations & problem solving:
1. Mathematical
2. Relational
3. Logical

pshscs2.tripod.com/files/plf.pdf
Mathematical Operators
Include the following:
– Addition +
– Subtraction
­
– Multiplication /
*
– Integer Division \
– Modulo
– Division Division
– MOD
Powers ^ or **
– Functions FunctionName (parameters)

pshscs2.tripod.com/files/plf.pdf
Relational Operators
Include the following:
– Equal to =
– Less than <
– Greater than >
– Less than or equal to <=
– Greater than or equal to >=
– Not equal to <>
or !=
pshscs2.tripod.com/files/plf.pdf
Relational Operators
• A programmer uses relational operators
to program decisions.
• The resultant of a relational operator is
the logical data type TRUE or FALSE.
• Are also used to control repetitive
instructions called loops.

pshscs2.tripod.com/files/plf.pdf
Logical Operators
• Are used to connect relational
expressions (decision­making
expressions) & to perform operations on
logical data.

• Logical operators include the following:


NOT – Not
AND – And
OR –
Or
pshscs2.tripod.com/files/plf.pdf
Expressions & Equations
• An Expression processes data (the operands) through
the use of operators.
• An equation stores the resultant of an expression in a
memory location in the computer through the equal sign (
= ).
• Equations are often called “assignment statements.”
• The equal sign does not mean equality, but means
“replaced by” or “is assigned the value of.”
• The right­hand side of the equation is processed before
the assignment is made.

pshscs2.tripod.com/files/plf.pdf
Expressions & Equations

pshscs2.tripod.com/files/plf.pdf
Pseudo­Code Description
Format
Format Explanation Format Explanation
This is the where {comment} Used for comments.
procedures, variables,
Declaration types, etc., are declared Conditional Indicates selective
section expression processing.
● Proce If the conditional
ss 1 expression is true,
Declares the names, Process 1 is
○ types, etc., of procedures ● Proce executed; if the
, variables. ss 2 conditional expression
This is the area where is false, Process 2 is
processing is described. Conditional Ienxdeicautesd.a
Processing expression loop with the
section termination
● Proce condition at top.
ss The process is
executed as long as
Assigns the value of the
●Variable←Expressio expression to the
n variable.
Example 1 “2 Branch”
When mathematics score is less than 60, the student gets
a failing mark

Mathematics Exam. Declarati ○ Integer Variable:


on SCORE
section
Yes
● SCORE ← Mathematics
SCORE <
Mark
60
No
Processin
g Score <
Fail section 60
● Fail
● Do Nothing
Example 2 “3 Branch”
When mathematics score is less than 60, and English
score is less than 60, the student gets a failing mark.

Mathematics Exam.
Declarati ○Eng.SCORE
Integer Variable: Math.SCORE,
on
English Exam. section
Yes
● Math.SCORE ← Mathematics
Math. SCORE < Mark
Yes ● Eng.SCORE ← English Mark
60 Processin
No Math.Score < 60
Eng. SCORE < g
60 section
Eng.Score <
No
Fail 60
● Fail

● Do Nothing

● Do Nothing
Example 3 “3 Branch”
When mathematics score is less than 60, or English
score is less than 60, the student gets a failing mark.

Mathematics & English Exam.


Declarati ○Eng.SCORE
Integer Variable: Math.SCORE,
on
Yes No section
Math. SCORE < ● Math.SCORE ← Mathematics
60 Mark
Yes ● Eng.SCORE ← English Mark
Fail Eng. SCORE < Processin Math.Score < 60
60 g ● Fail
section
No Eng.Score <
Fail 60

● Fail

● Do Nothing
● Do Nothing
Example 4 “3 Branch”
When mathematics score is greater than equal 80
evaluation A, when score is less than 80 and greater than
equal 60 evaluation B, when score is less than 60 then
evaluation C.
Mathematics Exam.
Declarati ○ Integer Variable: Math.SCORE

80≦Score Score < on
section
60 Math. SCORE
● Math.SCORE ←
60 ≦Score < Mathematics Mark
Math.Score≧80
80 Processin
● Evaluation A
Evaluation A g
section
Evaluation B Math.Score≧60

Evaluation C ● Evaluation B

● Evaluation C
Example 5 “Repeat”
Variable x has an initial value 0. And add 1 to x. It repeats
addition while x is not 5 (until x becomes 5).

X←0
Declarati ○ Integer Variable: X
on
section
● X←
No 0
X≠5
Processin X ≠5
Yes g
● X=X+1
section
X ← X+1
Example 6 “Loop”
Variable x has an initial value 0. And add 3 to x. It repeats
addition 5 times.

X ← 0, Y ← 0
Declarati ○ Integer Variable: X, Y
on
section
● X←
No 0
Y < 5 ● Y←
Processin
Yes g 0
section Y < 5
X←X+3 ● X=X+3

● Y=Y+1
Y ←Y+
1

Y becomes the Loop Counter.


Example 7 “Loop”
Calculate the sum of odd number 1, 3, 5, 7, and 9.

X ← 0, Y ← 1
Declarati ○ Integer Variable: X, Y
on
Loop
section
till Y > 9 ● X←
0
X←X+Y Processin
● Y←
g 1
Y ←Y+ section Y≦9
2 ● X=X+Y

Loop ● Y=Y+2
Example 8 “Loop Array”
There are four money savings boxes. Their names are
"week1", "week2", "week3", and "week4".
Get the total amount of money at the end of the
month.
Save(1) ← 120, Save(2) ← 340 ○ Integer Variable: Sum, i
Save(3) ← 230, Save(4) ← 180 Declarati

Sum ← 0, i ← 1 on ○ Integer Array: Save(4)


section
No ● Save(1)← 120 ~ Save(4) ←
i≦4 ● S1u8m0 ←0, i ← 1
Yes
Processin
gsection
i≦4
Sum ← Sum + Save(i) ● Sum = Sum + Save(i)

● i=i+1
i ←i+
1
Example 9 “Loop Array & Branch”
There are the
Calculate foursum
money savings
of odd numberboxes.
1, 3,Their name
5, 7, and 9. are
"week1 to week4". Only when the amount of savings on the
weekend is 200 or more is it added to Sum.

Save(1) ← 120, Save(2) ← 340 ○ Integer Variable: Sum, i


Declarati
Save(3) ← 230, Save(4) ← 180 on ○ Integer Array: Save(4)
Sum ← 0, i ← 1 section
● Save(1)← 120 ~
No
Save(4) ←
i≦4
● S1u8m0 ←
Processin 0, i ← 1
Yes i≦
No g
4
Save( section Save(i) ≧ 200
i) ≧ ● Sum = Sum + Save(i)

Sum ← Sum +200


Save(i) ● Nothing
Yes
● i=i+1
i ←i+
1
Practice 1
Sum of integer 1 to 10
Sum ← 0, i ← 1
Declarati ○ Integer Variable: Sum, i
on
section

No ● Sum ← 0
● i← 1
i ≦ 10
Processin
Yes g
section
Sum ← Sum + i i ≦ 10

i ←i+ ● S
1 u
m

S
u
Practice 2
Given an array T(n) which
contains some
values stored in ascending
order, look for a given value in
this array by using the binary
search method.
Before Practice 2
low ← ------ mid -------→ high
T(n­1 T(n
T(1) T(2) T(3) … … …
) )

mid =(low + high) / 2 (ignore decimal


After compare data & fractions.)
T(mid)
T(1) T(2) T(3) …
T(mi …
T(n­1 T(n
d) ) )

low ← ---

low ← ----→ high
high data <
T(mid) Target found
data >
T(mid)

data =
T(mid)
Practice 2
idx ← 0, low ← 1, high ← n

Declarati

section
○ Integer Variable:
idx, low, high, mid
Loop

on
low > high or idx ≠ 0
● idx ← 0
mid ← (high + Low)/2 ● low ← 1
● high ← n
No
data = T(mid) low ≦high and

Processin

section
Yes No idx = 0
● mid ←
data > T(mid) (low + high)/2
Yes

g
data = T(mid)
● idx ← mid
idx ← mid low ← mid + 1 high ← mid - 1
data >
T(mid)
● low ←
mid+1
Loop
● high←mid ­
1

You might also like