You are on page 1of 9

HOLY ANGEL UNIVERSITY

COLLEGE OF INFORMATION & COMMUNICATION TECHNOLOGY

LOGFOR (LOGIC FORMULATION) - Y115

TOPICS:
1. INTRODUCTION TO ALGORITHMS
2. PSEUDOCODE
3. FLOWCHARTS

ALGORITHMS
There are many ways to solve a problem. Moreover, given a certain way to solve a
problem, there are many ways to express that into algorithm. Problem solving is the
process of transforming the description of a problem into the solution of that problem.

The study of algorithms began as a subject in mathematics. The search for algorithms
was a significant part of early mathematicians. Their goal then was to find a single set of
instructions that can be used to solve any problem of a particular type.

An algorithm is a type of effective method in which a definite list of well-defined and


unambiguous instructions for completing a task, when given an initial state, will proceed
through a well-defined series of successive states or operations, eventually terminating
in an end-state. It is a step by step solution to a given problem. An algorithm therefore is
not unique.

An algorithm is a finite state of unambiguous executable instructions that directs


a terminating activity.

EXAMPLE:

Write an algorithm to compute the distance traveled and the average miles per gallon on
a trip when given as input the number of gallons used and the starting and ending
mileage readings on the odometer. Repeat this process until user wants to stop.

Variables: response, gallons, start, end, distance, mpg


0 Set response to “Yes”
1 Repeat steps 2-8 until response = “No”
2 Get gallons, start, end
3 Set distance to end - start
4 Set mpg to distance ÷ gallons
5 Print mpg
6 if mpg > 25.0 then
print “You are getting good gas mileage”
else print “You are NOT getting good gas mileage”
7 Print “Do you want to do this again, Yes or No?”
8 Get response
9 Stop
HOLY ANGEL UNIVERSITY
COLLEGE OF INFORMATION & COMMUNICATION TECHNOLOGY

LOGFOR (LOGIC FORMULATION) - Y115

PSEUDOCODE
Pseudocode consists of short, English phrases used to explain specific tasks within a
program's algorithm. Pseudocode should not include keywords in any specific computer
languages. It should be written as a list of consecutive phrases.. Indentation can be used
to show the logic in pseudocode as well.

It is a compact and informal high-level description of a computer programming algorithm


that uses the structural conventions of some programming language, but typically omits
details that are not essential for the understanding of the algorithm, such as subroutines,
variable declarations and system-specific code.

The purpose of using pseudocode is that it may be easier for humans to read than
conventional programming languages, and that it may be a compact and environment-
independent description of the key principles of an algorithm. No standard for
pseudocode syntax exists, as a program in pseudocode is not an executable program.

Looks like a programming language but is NOT. It is a strict form of english.

You can’t draw diagrams in Pseudocode. Instead you use special words (called key
words) to specify the logic of an algorithm.

Start

Walk
to door
BEGIN
Open Walk to door
door Open door
Move through door
Move
through END
door

Note the ‘indentation’


End
HOLY ANGEL UNIVERSITY
COLLEGE OF INFORMATION & COMMUNICATION TECHNOLOGY

LOGFOR (LOGIC FORMULATION) - Y115

FLOWCHARTS
The flowchart is a means of visually presenting the flow of data through an information
processing systems, the operations performed within the system and the sequence in
which they are performed. Flowcharts are generally drawn in the early stages of
formulating computer solutions. Flowcharts facilitate communication between
programmers and business people. These flowcharts play a vital role in the
programming of a problem and are quite helpful in understanding the logic of
complicated and lengthy problems. Once the flowchart is drawn, it becomes easy to
write the program in any high level language.
Often we see how flowcharts are helpful in explaining the program to others. Hence, it is
correct to say that a flowchart is a must for the better documentation of a complex
program.

HISTORY OF FLOWCHARTS
The first structured method for documenting process flow, the flow process chart, was
introduced by Frank Gilbreth to members of ASME (American Society of Mechanical
Engineers) in 1921 in the presentation “Process Charts—First Steps in Finding the One
Best Way”

ADVANTAGES OF USING FLOWCHARTS


• Communication: Flowcharts are better way of communicating the logic of a system to
all concerned.
• Effective analysis: With the help of flowchart, problem can be analyzed in more
effective way.
• Proper documentation: Program flowcharts serve as a good program documentation,
which is needed for various purposes.
• Efficient Coding: The flowcharts act as a guide or blueprint during the systems
analysis and program development phase.
• Proper Debugging: The flowchart helps in debugging process.
• Efficient Program Maintenance: The maintenance of operating program becomes
easy with the help of flowchart. It helps the programmer to put efforts more efficiently
on that part

DISADVANTAGES OF USING FLOWCHARTS


• Complex logic: Sometimes, the program logic is quite complicated. In that case,
flowchart becomes complex and clumsy.
• Alterations and Modifications: If alterations are required the flowchart may require re-
drawing completely.
• Reproduction: As the flowchart symbols cannot be typed, reproduction of flowchart
becomes a problem.
• The essentials of what is done can easily be lost in the technical details of how it is
done
HOLY ANGEL UNIVERSITY
COLLEGE OF INFORMATION & COMMUNICATION TECHNOLOGY

LOGFOR (LOGIC FORMULATION) - Y115

FLOWCHART SYMBOLS

Start or end of the program

Computational steps or processing function of a program

Input or output operation

Decision making and branching

Connector or joining of two parts of program

Off-page connector

Flow line

Preparation / Initialization

GUIDELINES ON CREATING FLOWCHARTS


• In drawing a proper flowchart, all necessary requirements should be listed out in
logical order.
• The flowchart should be clear, neat and easy to follow. There should not be any room
for ambiguity in understanding the flowchart.
• The usual direction of the flow of a procedure or system is from left to right or top to
bottom.
• Only one flow line should come out from a process symbol.
• Only one flow line should enter a decision symbol, but two or three flow lines, one for
each possible answer, should leave the decision symbol.
• Only one flow line is used in conjunction with terminal symbol.
• If the flowchart becomes complex, it is better to use connector symbols to reduce the
number of flow lines. Avoid the intersection of flow lines if you want to make it more
effective and better way of communication.
• Ensure that the flowchart has a logical start and finish.
• It is useful to test the validity of the flowchart by passing through it with a simple test
data.
HOLY ANGEL UNIVERSITY
COLLEGE OF INFORMATION & COMMUNICATION TECHNOLOGY

LOGFOR (LOGIC FORMULATION) - Y115

FEW EXAMPLES ON FLOWCHARTING

Now we shall present few examples on flowcharting for proper understanding of this
technique. This will help in student in program development process at a later stage.

Example 1. Draw a flowchart to find the sum of first 50 natural numbers.


HOLY ANGEL UNIVERSITY
COLLEGE OF INFORMATION & COMMUNICATION TECHNOLOGY

LOGFOR (LOGIC FORMULATION) - Y115

Example 2. Flowchart for finding out the largest of three numbers

Example 3. Draw a flowchart for computing factorial N (N!). Where N! = 1 ´ 2 ´ 3 ´


HOLY ANGEL UNIVERSITY
COLLEGE OF INFORMATION & COMMUNICATION TECHNOLOGY

LOGFOR (LOGIC FORMULATION) - Y115


HOLY ANGEL UNIVERSITY
COLLEGE OF INFORMATION & COMMUNICATION TECHNOLOGY

LOGFOR (LOGIC FORMULATION) - Y115

TYPES OF DATA

A C program is composed of a sequence of characters that are grouped by a compiler


into identifiable tokens. These tokens can be classified as literals, identifiers, and
keywords.

Literals are classified into two:


• Numeric literals can be further subdivided into whole numbers and real numbers.
Whole numbers are also called integers. These types of numbers do not contain any
fractional part and it should not have a decimal point. Real numbers, on the other
hand, are also called floating point numbers. These numbers have fractional parts
and it could also be expressed using scientific notation.
• Non-numeric literals may be in the form of a character or a series of characters. A
series of characters is called a string. A string should be enclosed in double quotes,
while a character is enclosed in single quotes. Some special characters may be
used, but it should be preceded by the escape character \ (backslash). Identifiers are
composed of a sequence of letters, digits, and the special character _ (underscore).
• Identifiers are defined by the programmer, thus they should be descriptive. Avoid
using names that are too short or too long. Limit the identifiers from 8 to 15
characters only.

Some rules must be followed for defining identifiers. They are as follows:
1. It must consist only of letters, digits, and underscores.
2. An identifier cannot begin with a digit.
3. An identifier defined in the C standard library should not be redefined.
4. Identifiers are case sensitive; meaning uppercase is not equal to the lowercase.

• Variables are identifiers that can store a value that can be changed.
These can be of different data types. They can store literals or some type
of structured data. Structured data can contain one or more values or data.
• Constants are identifiers that can store a value that cannot be changed.
Usually, constant identifiers are all capital letters with underscores between
each word to distinguish them from variables.
• Keywords are words that have a strict meaning in C. These are special
words “reserved” by the programming language for processing, thus these
may not be redefined by the programmer.

SYNTAX AND SEMANTICS


• Syntax refers to the representation itself. It also refers to the rules governing word
usage and punctuation in a programming language.
• Semantics refers to the concept represented (i.e., the logic).

In the English language, we have both syntax and semantics. Syntax is the grammar of
the language. Semantics is the meaning. An English sentence may be syntactically
correct, yet semantically incorrect. This is also true of algorithms. And it is also true of
computer code.
HOLY ANGEL UNIVERSITY
COLLEGE OF INFORMATION & COMMUNICATION TECHNOLOGY

LOGFOR (LOGIC FORMULATION) - Y115

Pseudocode is used in
• designing algorithms
• communicating an algorithm to the customer
• converting an algorithm to code (used by the programmer)
• debugging logic (semantic) errors in a solution before coding (hand tracing)

GUIDELINES
• Any user prompts should appear exactly as you wish the programmer to code them.
• The destination of any output data should be stated, such as in “Display”, which
implies the screen.
• Make the data items clear (e.g., surround them by < and > ) and give them
descriptive names.
• Use formulas wherever possible for clarity and brevity.
• Use keywords (such as Read and While) and use them consistenty. Accent them in
some manner.
• Use indentation for clarity of logic.
• Avoid using code. Pseudocode should not be programming language-specific.
• Always keep in mind that you may not be the person translating your pseudocode
into programming language code. It must, therefore, be unambiguous.
• You may make up your own pseudocoding guidelines, but you MUST be consistent.

Any problem can be solved using only three logical control structures:

• Sequence - A series of steps or statements that are executed in the order they are
written.
Display “Enter two numbers: “
Read <number1>
Read <number2>
<sum> = <number1> + <number2>
Display “sum = “, <sum>

• Selection - Defines one or more courses of action depending on the evaluation of a


condition. Synonyms: conditional, branching, decision
If (condition is true) If (condition is true)
do this do this
End_if Else
do that
End_if

• Repetition - Allows one or more statements to be repeated as long as a given


condition is true. Synonyms: looping, iteration

While (condition is true)


do this
End_while

You might also like