0% found this document useful (0 votes)
16 views9 pages

Understanding Algorithms and Flowcharting

Uploaded by

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

Understanding Algorithms and Flowcharting

Uploaded by

6dnp76cvfz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Cebu Institute of Technology – University

Computer Engineering Department


Algorithm and Flowcharting

Algorithm
• Relates to the name of mathematician Al-Khwarizmi, which means a procedure or a
technique.
• An algorithm is a sequence of steps to solve a particular problem or algorithm. It is an
ordered set of unambiguous steps that produces a result and terminates in a finite time.
• Algorithms are universal and can be translated to any programming language.
Algorithm vs. Syntax vs. Program
Algorithm - A sequence of logical instructions or steps needed to finish a task.
Syntax - The spelling and grammar of a programming language.
Program - A sequence of instructions written in a code/syntax that a computer can interpret and
execute.

Characteristics
• Input – an algorithm may or may not require an input.
• Output – each algorithm is expected to produce at least one result.
• Definiteness – each instruction must be clear and unambiguous.
• Finiteness – the algorithm should terminate after finite number of steps.
Three Types of Control Structures
• Sequential – statements are placed one after the other and the execution takes place
starting from up to down.
• Conditional/Branching – there is a condition and according to that condition, a decision
of either TRUE or FALSE is achieved. In case of TRUE, one of the two branches is
explored; but in the case of a FALSE condition, the other alternative is taken. Generally,
“IF-ELSE” is used to represent the branch control.
• Loop (Iteration) – allows a statement/s to be executed repeatedly based on a certain loop
condition.
Advantages of having a well written algorithm
• It is a stepwise representation of a solution to a given problem, which makes it easy to
understand.
• An algorithm uses a definite procedure.
• It is not dependent on any programming language, so it is easy to understand for anyone
even without programming knowledge.
• very step in an algorithm has its own logical sequence so it is easy to debug.

How to write Algorithms


Step 1. Define your algorithms input: Many algorithms take in data to be processed, e.g., to
calculate the area of rectangle input may be the rectangle height and rectangle width.
Step 2. Define the variables: Algorithm's variables allow you to use it for more than one place.
We can define two variables for rectangle height and rectangle width as HEIGHT and WIDTH (or
H & W). We should use meaningful variable name e.g., instead of using H & W use HEIGHT and
WIDTH as variable name.
Step 3. Outline the algorithm's operations: Use input variable for computation purpose, e.g.,
to find area of rectangle multiply the HEIGHT and WIDTH variable and store the value in new
variable (say) AREA. An algorithm's operations can take the form of multiple steps and even
branch, depending on the value of the input variables.
Step 4. Output the results of your algorithm's operations: In case of area of rectangle output
will be the value stored in variable AREA. if the input variables described a rectangle with a
HEIGHT of 2 and a WIDTH of 3, the algorithm would output the value of 6.
Step 5. Follow the programming structure in writing your algorithm which is:

I. Start
II. Declaration
III. Input
IV. Operations
V. Output

Engr. Jundith D. Alterado


[Link]@[Link]
Cebu Institute of Technology – University
Computer Engineering Department
Algorithm and Flowcharting

Example of Sequential Control Structure Algorithms:


I. Algorithms with no needed input
1. Write an algorithm of a program that would display the phrase: “Welcome to CIT-
University!”.
I. Start (your algorithm should ALWAYS have a start)
(Declaration: There is no ‘variable’ needed since there is no input and the
output is a constant phrase. Therefore, there is no Declaration.)
(There is no need for a process/operation as well, since this problem doesn’t
need any arithmetic operation)
II. Output: Print “Welcome to CIT-University.” (We will use the word print instead
of display)
III. End (an algorithm should have an ending).

2. Write an algorithm of a program that would solve and display the area of a square
with one of its side equal to 12.
I. Start
II. Output: Print “24”
III. End
II. Algorithms with input
1. Write an algorithm of a program that would display the phrase the user would input
from the keyboard.
I. Start (your algorithm should ALWAYS have a start)
II. Declaration: phrase
(This time we have a declaration because we need a variable that would
receive the phrase the user would input. I named my variable as phrase. You
have the freedom to name your variables as long as it follows the naming
rules)
III. Input: phrase
(Ask the user for the value of phrase)
(There is no need for a process/operation, since this problem doesn’t need
any arithmetic operation)
IV. Output: Print phrase (Display the value of the input variable)
V. End (an algorithm should have an ending).

2. Write an algorithm of a program that would solve and display the area of a square.
This time, the value of the side is unknown and can be any value
I. Start
II. Declare: side, area
(We need to declare two variables for the input variable side and the output
variable area)
III. Input: side
(Ask the user for the value of the input variable side.)
IV. Process: area = side * side
(Solve for the area using the formula for the area of a rectangle)
(Make sure you use the same variable name you declared in the declaration.
It is wrong to suddenly write area = s * s)
V. Output: Print area
(Display the value of our result or output)
VI. End

Engr. Jundith D. Alterado


[Link]@[Link]
Cebu Institute of Technology – University
Computer Engineering Department
Algorithm and Flowcharting

III. More examples

1. Write an algorithm of a program that would print the sum of two numbers.
I. Start
II. Declare: num1, num2, sum
III. Input: num1, num2
IV. Process: sum = num1 + num2
VII. Output: Print sum
VIII. End
2. Write an algorithm of a program that would convert the inputted time in minutes to seconds.
I. Start
II. Declare: time_mins, time_sec
III. Input: time_mins
IV. Process: time_sec = time_mins*60 (1 minute = 60 seconds)
V. Output: Print time_sec
VI. Ends
3. Write an algorithm of a program that would accept two numbers, calculate and display their
sum and difference.
I. Start
II. Declare: num1, num2, sum, diff
III. Input: num1, num2
IV. Process:
sum = num1 + num2
diff = num1 – num2
V. Output: Print sum and diff
VI. End

Example of Conditional Control Structure Algorithms:

Selection Control Statement – Algorithm used to represent a choice. The result of the
statement can either be TRUE or FALSE.
Formal Example Meaning
if(Conditional/Logical Expression) if (A < B) Check/Compare if A is less
than B. Result is TRUE if A is
less than B, else FALSE

Examples:

1. Write an algorithm of a program that 2. Write an algorithm of a program that


would print the phrase “Equal” if two would print “Positive” if the inputted
numbers are equal in value and “Not number is greater than or equal to
Equal” if not. zero and “Negative” if not.
V. Start VII. Start
VI. Declare: num1, num2 VIII. Declare: num
VII. Input: num1, num2 IX. Input: num
VIII. Process: X. Process: if(num >=0)
if (num1 == num2) XI. Output if TRUE: Print
“Positive”
IX. Output if TRUE: Print
“Equal” XII. Output if FALSE: Print
“Negative”
X. Output if FALSE: Print “Not
Equal” XIII. Ends
XI. End

Engr. Jundith D. Alterado


[Link]@[Link]
Cebu Institute of Technology – University
Computer Engineering Department
Algorithm and Flowcharting

Flowcharting
• Diagrammatic/Graphical/Visual Representation of an algorithm.
• Considered as a blueprint of a design used for solving a specific problem.
• Uses Symbols along with phrases to represent the algorithm.
Advantages of a flowchart:
• Flowchart is an excellent way of communicating the logic of a program.
• Easy and efficient to analyze problem using flowchart.
• During program development cycle, the flowchart plays the role of a blueprint, which makes
program development process easier.
• After successful development of a program, it needs continuous timely maintenance during the
course of its operation. The flowchart makes program or system maintenance easier.
• It is easy to convert the flowchart into any programming language code.
Symbols

Note: Declaration is done inside the Preprocessor symbol.

Engr. Jundith D. Alterado


[Link]@[Link]
Cebu Institute of Technology – University
Computer Engineering Department
Algorithm and Flowcharting

Guidelines in Flowcharting:

a. In drawing a proper flowchart, all necessary requirements should be listed out in logical
order.
b. The flowchart should be clear, neat and easy to follow. There should not be any room for
ambiguity in understanding the flowchart.
c. The usual direction of the flow of a procedure or system is from left to right or top to
bottom.
d. Only one flow line should come out from a process symbol.

or

e. 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.

f. Only one flow line is used in conjunction with terminal symbol.

h. 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.

Instead of: We will use:

The label ‘1’ indicates that you want


to go to another connecter also
labeled ‘1’.

i. Ensure that the flowchart has a logical start and finish.


j. It is useful to test the validity of the flowchart by passing through it with a simple test
data.

The language used to write algorithm is simple and similar to day-to-day life language. The
variable names are used to store the values. The value store in variable can change in the solution
steps.

Engr. Jundith D. Alterado


[Link]@[Link]
Cebu Institute of Technology – University
Computer Engineering Department
Algorithm and Flowcharting

Example of Sequential Control Structure Flowcharts:

1. Draw a flowchart of a program that would compute and display the sum of two numbers.
Algorithm Flowchart
Step-1 Start

Step-2 Declare variables SUM, A, B

Step-3 Input first number A

Step-4 Input second number B

Step-5 SUM = A + B

Step-6 Print SUM

Step-7 End

Or, you can also combine consecutive steps that uses the same symbol.
Algorithm Flowchart
Step-1 Start

Step-3 Declare variables SUM, A, B

Step-3 Input values for A, B

Step-4 SUM = A + B

Step-5 Print SUM

Step-6 End

Engr. Jundith D. Alterado


[Link]@[Link]
Cebu Institute of Technology – University
Computer Engineering Department
Algorithm and Flowcharting

2. Write an algorithm and draw a flowchart of a program that would convert the inputted
temperature from Celsius to Fahrenheit.
Algorithm Flowchart
Step-1 Start

Step-2 Declare variables F, C

Step-3 Input value for C

Step-4 F = (9.0/5.0 x C) + 32

Step-5 Print F

Step-6 End

3. Write an algorithm and draw a flowchart of a program that would compute for the Area and
Perimeter of a Square.
Algorithm Flowchart
Step-1 Start

Step-2 Declare AREA, PERIMETER, L

Step-3 Input value of L

Step-4 AREA = L x L

Step-5 PERIMETER = 4 x L

Step-6 Print AREA, PERIMETER

Step-7 End

Engr. Jundith D. Alterado


[Link]@[Link]
Cebu Institute of Technology – University
Computer Engineering Department
Algorithm and Flowcharting

Example of Conditional Control Structure Flowcharts:

1. Write an algorithm and draw a flowchart of a program that would print the phrase “Equal” if
the two numbers are equal and “Not Equal” if not.
Algorithm Flowchart
Step-1 Start
Step-2 Declare num1, num2
Step-3 Input values for num1, num2
Step-4 if (num1==num2)
Step-5 if Step-3 is TRUE, print “Equal”
Step-6 if Step-3 is FALSE, print “Not Equal”
Step-7 End
False
Note: Don’t forget to put label on your conditions,
so that it’s clear where in the result for TRUE or
FALSE.
True

2. Write an algorithm and draw a flowchart of a program that would print “Positive” if the inputted
number is greater than or equal to zero and “Negative” if not.
M Algorithm Flowchart
Step-1 Start
Step-2 Declare variable X
Step-3 Input value for X
Step-4 if (X>=0)
Step-5 if Step-3 is TRUE, print “POSITIVE”
Step-6 if Step-3 is FALSE, print “NEGATIVE”
Step-7 End

Engr. Jundith D. Alterado


[Link]@[Link]
Cebu Institute of Technology – University
Computer Engineering Department
Algorithm and Flowcharting

3. Write an algorithm and draw a flowchart of a program that would find the smaller of two numbers
and display its value. Display the value of the first number if they are equal.
Algorithm Flowchart
Step-1 Start

Step-2 Declare variables NUM1, NUM2

Step-3 Input values of NUM1,NUM2

Step-4 IF NUM1 < NUM2

Step-5 IF Step-3 is TRUE, display NUM2

Step-6 Else, if not, display NUM1

Step-7 End

Engr. Jundith D. Alterado


[Link]@[Link]

Common questions

Powered by AI

There are three types of control structures: Sequential, Conditional/Branching, and Loop (Iteration). Sequential structures execute statements one after another; for example, printing a welcome message . Conditional structures execute different blocks of code based on a true or false condition, exemplified by an "if-else" statement that checks if two numbers are equal . Loops repeatedly execute code as long as a condition is met, such as calculating a sum iteratively until a desired numeric goal is reached .

Variable declarations in algorithms act as placeholders to store values that will be used for computations, ensuring data is organized and accessible during algorithm operations. Meaningful variable names enhance clarity and understanding, making the algorithm easier to follow and debug. Rather than arbitrary letters, names like HEIGHT and WIDTH give immediate context regarding the data they hold, aiding effective communication of the algorithm's logic .

Flowcharts provide several benefits such as simplifying the understanding of complex algorithms, aiding in system maintenance, and communicating logic effectively during the development process. The visual nature allows for quick identification of logical errors and optimizing workflows. However, creating detailed flowcharts can be time-consuming and complex, especially for large systems, and maintaining updated flowcharts is challenging as program requirements evolve .

Flowcharting aids in communicating the logic of a program through visual representation, making it easier to analyze and maintain systems. Its components include symbols for processes, inputs/outputs, decisions, and flow lines that indicate the sequence of steps. Flowcharts help outline a clear pathway from start to end and streamline translating this blueprint into any programming language code .

Loop (iteration) structures enable an algorithm to execute repeated operations based on specific conditions, allowing them to efficiently manage repetitive tasks without redundancy. Iterations can significantly reduce the complexity of algorithms by consolidating repeated logic into concise loops, optimizing computation time and resource usage. This capability enhances problem-solving by allowing dynamic handling of varying input sizes and conditions, increasing algorithm scalability and efficiency .

Testing a flowchart using test data ensures the flowchart accurately represents the desired algorithm logic, revealing flaws or misunderstanding in the design. This testing allows developers to rigorously verify the logic steps and flow direction before implementation in programming, reducing errors in the development phase. It acts as a validation step to confirm that the depicted process achieves intended solutions efficiently .

Algorithms are independent of programming languages, ensuring they are universally understandable and interpretable by anyone, regardless of specific language syntax. This independence allows individuals without programming knowledge to comprehend and utilize the step-by-step solutions algorithms provide, promoting broader accessibility and understanding of logical problem-solving methods .

Algorithms are defined by several key characteristics: Input, Output, Definiteness, Finiteness, and Effectiveness. These characteristics are crucial as they ensure that the algorithm can process data (input), produce results (output), have clear instructions (definiteness), complete in a fixed number of steps (finiteness), and achieve desired tasks efficiently (effectiveness).

To calculate the area and perimeter of a square using an algorithm, one must declare a variable for the side length. The algorithm computes the area by squaring the side length and the perimeter by multiplying the side length by four. Logical structuring ensures each step is completed in order and without redundancy, which simplifies debugging and implementation. For instance, declaring variables, followed by computation and output, highlights a logical progression from input to result .

In a conditional control structure, the flow of the algorithm branches based on whether a condition evaluates to true or false. This affects which set of instructions is executed. For example, in an algorithm that compares two numbers, the condition checks if the numbers are equal; if true, it prints "Equal," otherwise "Not Equal" . This branching ensures that the program can make decisions and handle different cases dynamically.

You might also like