Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Ch1 Concept of Algorithm

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

IT 112 – COMPUTER PROGRAMMING 1

CHAPTER 1: INTRODUCTION TO PROGRAMMING

CONCEPT OF
ALGORITHM
Asst. Prof. Benjamin E. Bulawit Jr, DIT
Faculty-BS Information Technology
Isabela State University-Angadanan Campus
Learning Outcomes
At the end of the lesson, you are expected to:
 define and explain algorithm
 write an algorithm base on the given scenario/problem
What is an ALGORITHM?
• An algorithm is a description of how a specific problem should be solved.
It is a sequence of steps to solve a particular problem or an ordered set
of unambiguous steps that produce results and end in a finite time
(Christodoulou, Szczygiet, Ktapa & Kolarz, 2018)

• An algorithm is a procedure for solving a problem in terms of the actions


to be executed and the order in which those actions are to be executed.
An algorithm is merely the sequence of steps taken to solve a problem.
The steps are normally "sequence," "selection, " "iteration," and a
case-type statement.
• The "selection" is the "if then else" statement, and the iteration is
satisfied by a number of statements, such as the "while," " do," and the
"for," while the case-type statement is satisfied by the "switch" statement.
Making cup of coffee as an example of algorithm
Input: Ingredients
Output: A cup of coffee
Steps/Process:
• Pour drinking water into electric kettle
• Plug electric kettle and wait for it to boil
• While waiting, prepare coffee cup
• Add instant coffee powder into cup
• Check boiling water
• If water is boiled, unplug kettle and pour water into cup
• If water is not boiled, press the “on” switch to continue and wait until boiled
• Refer back to step 6
• Stir coffee mix
• Drink!
Assumptions:
• I have a cup/glass to put my coffee in
• I have a kettle
• I can drink coffee
What is an ALGORITHM?
• In algorithms, steps are expressed in the form of a statement or
instruction. Consequently, a computer program contains a set of
statements that indicate which procedure to perform to the machine.

• Algorithms must be in a 'program' format to be executed by


computers. A program is written in a software application called
programming language, and the action to express an algorithm as a
program is called programming.
In order to use a computer for process
execution it is necessary to:
● design an algorithm to describe how the process is to be carried out;
● to express the algorithm in a program, use the programming language;
● execute the program on the computer

Note: It is therefore necessary to note that algorithms are independent of the


programming language used, and that each algorithm can be expressed and
executed on different computers in different programming languages. This is why
algorithm design is a fundamental aspect of information technology education. The
algorithm design is a rigorous theoretical task, which is considerably harder than
presenting the algorithm as a program.
How to use a computer for process
execution?
• Processes performed on the computer more often require
input in data form, and very often the output produced
will also be in data form.
• For example, consider the process of estimating your
savings in a month that will include input from your
load, travel, food project, etc. expenses and will
generate outputs such as total expense and savings
amount.
How to write an algorithm?
STEP 1 DEFINE YOUR ALGORITHMS INPUT:
As discussed earlier, algorithms take in data to be processed. For instance, to
calculate the area of a rectangle, input may be the rectangle height and
rectangle width.

STEP 2 DEFINE THE VARIABLES: Variables of the algorithm let you use it
for more than one location. We may define two variables as HEIGHT and
WIDTH (or H & W) for the rectangle height and rectangle width.

Note: We should use significant variable name based on its purpose. Example, instead of using H
& W use HEIGHT and WIDTH as variable name. This is for you to easily remember the name when
used in different location of your algorithm.
How to write an algorithm?
STEP 3 OUTLINE THE ALGORITHM'S OPERATIONS: Use input variable for
computation purpose. For our example finding area of rectangle, multiply the HEIGHT
and WIDTH variable and store the value in new variable like 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 the


example area of rectangle, output will be the value stored in variable AREA.

Considering the defined algorithm’s operation, if the input variables defined a rectangle
with a HEIGHT of 2 and a WIDTH of 3, the algorithm would output the AREA with
value of 6.
To write these processes to become an
algorithm of finding the area of a rectangle,
it would be as follows:

1. Input height and width of a rectangle.


2. To compute for the area of a rectangle, multiply the
HEIGHT and WIDTH variables and store the value in
new variable like AREA.
3. Display the value of AREA.
PSEUDOCODE

Pseudo code is a term which is often used in programming and algorithm-based fields. It is a
methodology that allows the programmer to represent the implementation of an algorithm. Simply,
we can say that it’s the cooked-up representation of an algorithm.
Often at times, algorithms are represented with the help of pseudo codes as they can be interpreted
by programmers no matter what their programming background or knowledge is. Pseudo code, as
the name suggests, is a false code or a representation of code which can be understood by even a
layman with some school level programming knowledge.

Algorithm: It’s an organized logical sequence of the actions or the approach towards a particular
problem. A programmer implements an algorithm to solve a problem. Algorithms are expressed
using natural verbal but somewhat technical annotations.

Pseudo code: It’s simply an implementation of an algorithm in the form of annotations and
informative text written in plain English. It has no syntax like any of the programming language and
thus can’t be compiled or interpreted by the computer.
PSEUDOCODE

Advantages of Pseudocode

 Improves the readability of any approach. It’s one of the best approaches to start
implementation of an algorithm.

 Acts as a bridge between the program and the algorithm or flowchart. Also works as a
rough documentation, so the program of one developer can be understood easily when
a pseudo code is written out. In industries, the approach of documentation is essential.
And that’s where a pseudo-code proves vital.

 The main goal of a pseudo code is to explain what exactly each line of a program
should do, hence making the code construction phase easier for the programmer.
PSEUDOCODE

Disadvantages of Pseudocode

 Pseudocode does not provide a visual representation of the logic of programming.

 There are no proper format for writing the for pseudocode.

 In Pseudocode their is extra need of maintain documentation.

 In Pseudocode their is no proper standard very company follow their own standard for
writing the pseudocode.
HOW TO WRITE PSEUDOCODE

 Arrange the sequence of tasks and write the pseudocode accordingly.


 Start with the statement of a pseudo code which establishes the main goal or
the aim.

Example:
This program will allow the user to check the number whether it's even or odd.
The way the if-else, for, while loops are indented in a program, indent the statements
likewise, as it helps to comprehend the decision control and execution mechanism. They
also improve the readability to a great extent.

Example:
if "1"
print response
"I am case 1"

if "2"
print response
"I am case 2"
HOW TO WRITE PSEUDOCODE
Guidelines in Writing Pseudocode

 Use appropriate naming conventions. The human tendency follows the approach to
follow what we see. If a programmer goes through a pseudo code, his approach will be
the same as per it, so the naming must be simple and distinct.
 Use appropriate sentence casings, such as CamelCase for methods, upper case for
constants and lower case for variables.
 Elaborate everything which is going to happen in the actual code. Don’t make the
pseudo code abstract.
 Use standard programming structures such as ‘if-then’, ‘for’, ‘while’, ‘cases’ the way we
use it in programming.
 Check whether all the sections of a pseudo code is complete, finite and clear to
understand and comprehend.
 Don’t write the pseudo code in a complete programmatic manner. It is necessary to be
simple to understand even for a layman or client, hence don’t incorporate too many
technical terms.
HOW TO WRITE PSEUDOCODE
HOW TO WRITE PSEUDOCODE
Examples:

1. Checking if a student passed or failed using 60 as the passing grade

If student's grade is greater than or equal to 60


Print "passed"
else
Print "failed"

2. Getting the class grade average of 10 students

Set total to zero


Set grade counter to one
While grade counter is less than or equal to ten
Input the next grade
Add the grade into the total
Set the class average to the total divided by ten
Print the class average.
HOW TO WRITE PSEUDOCODE

Examples:

3. Getting the class grade average based on all inputted grades.

Initialize total to zero


Initialize counter to zero
Input the first grade
while the user has not as yet entered the sentinel
add this grade into the running total
add one to the grade counter
input the next grade (possibly the sentinel)
if the counter is not equal to zero
set the average to the total divided by the counter
print the average
else
print 'no grades were entered'
HOW TO WRITE PSEUDOCODE
Examples:

4. Getting the number of students who passed and failed in an exam and if 8 or more
students passed show a message that “raise the tuition”.

initialize passes to zero


initialize failures to zero
initialize student to one
while student counter is less than or equal to ten
input the next exam result
if the student passed
add one to passes
else
add one to failures
add one to student counter
print the number of passes
print the number of failures
if eight or more students passed
HOW TO WRITE PSEUDOCODE

Some Keywords That Should be Used

For looping and selection, The keywords that are to be used include Do While...EndDo; Do Until...End
do; Case...End Case; If...Endif; Call ... with (parameters); Call; Return ....; Return; When; Always use
scope terminators for loops and iteration.

As verbs, use the words Generate, Compute, Process, etc. Words such as set, reset, increment,
compute, calculate, add, sum, multiply, ... print, display, input, output, edit, test , etc. with careful
indentation tend to foster desirable pseudocode.

Do not include data declarations in your pseudocode.


A. Write down an algorithm for cooking rice.
B. Write an algorithm for computing the sum of two numbers.
C. Write the algorithm for getting the area of rectangle.

Reminder

Answer Assessment Task 1 on our Google Classroom


1. Getting the Area of a Circle

2. Getting the sum of 5 numbers and if the


sum of the five numbers is higher than 100,
display the messsage “Above 100” if not
display the message “Below 100”

You might also like