Developing Algorithm

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

DCIT 22 Computer Programming 1 –Developing Algorithm

Developing Algorithms

An algorithm is the list of instructions and rules that a computer needs to do to complete a task.

Algorithms are in everything we do. But for those that aren’t inclined toward maths and programming, the term ‘algorithm’ is less
than clear.(In fact, the joke runs that developers use the word ‘algorithm’ when they don’t want to explain what they’ve done.) So,
you might have heard the term before, even used it. But what is an algorithm exactly?

What is an algorithm?

In essence, algorithms are simply a series of instructions that are followed, step by step, to do something useful or solve a problem.
You could consider a cake recipe an algorithm for making a cake, for example.

In computing, algorithms provide computers with a successive guide to completing actions. They’re comprised of a precise list of
instructions that outline exactly how to complete a task.

So, what is an algorithm? A good way to think of them is as mini instruction manuals telling computers how to complete a given
task or manipulate given data.

How do computer algorithms work?

Computer algorithms work via input and output. They take the input and apply each step of the algorithm to that information to
generate an output.

For example, a search engine is an algorithm that takes a search query as an input and searches its database for items relevant
to the words in the query. It then outputs the results.

You can easily visualize algorithms as a flowchart. The input leads to steps and questions that need handling in order. When each
section of the flowchart is completed, the generated result is the output.

What is Pseudocode?

Pseudocode is an informal way of programming description that does not require any strict programming language syntax or
underlying technology considerations. It is used for creating an outline or a rough draft of a program. Pseudocode summarizes a
program’s flow, but excludes underlying details. System designers write pseudocode to ensure that programmers understand a
software project's requirements and align code accordingly.

IDENTIFIERS

these are names that represents numeric and non-numeric data which occupy computer memory location.

Types of Identifier

1. Variable - identifier that can store a changeable value


2. Constant – identifier with fixed value

General Rules in Naming Identifier

1. The names must be according to what they represent. As a good programming practice, identifiers must be short and
descriptive.
2. Space is not allowed to be part of identifiers.
3. It must not start with a digit.
4. Special characters (except for an underscore) are not permitted to use as identifiers.
DCIT 22 Computer Programming 1 –Developing Algorithm

OPERATORS

These are symbols that instruct the compiler to perform mathematical and logical manipulations.

Classification of Operators

A. Arithmetic - used to perform mathematical operations and produce numerical values

SYMBOL MEANING EXAMPLE


+ Addition num1 + num2
- Subtraction gross - expense
* Multiplication unit * grade
/ Division sum/10
% Modulus Division 5%2
^ Exponentiation x^3

B. Rational - used to compare two data values against each other and produce true or false results

SYMBOL MEANING EXAMPLE


< Less than x<y
<= Less than or equal to subjectLoad<=21
> Greater than score>70
>= Greater than or equal to num1>=num2
‘==’ Equal to GPA==2.5
!= Not equal to Salary!=1000

C. Logical – used to combine two or more comparison tests into a single compound comparison expression.

SYMBOL MEANING DESCRIPTION EXAMPLE


&& And Returns true if both expression age>=13 && age <=19
are true
|| Or Return true if at least one num1<0 || num2>100
expression is true
! Not Negates the results of an !(x=7)
expression

CONVERSION OF ALGEBRAIC EXPRESSION INTO COMPUTER EXPRESSION

1. When converting expression, the appropriate equivalent symbols of operators must be used
2. the operators are always carried out from left to right
3. Hierarchy of operators must be considered when performing evaluation. Hierarchy decides which operators will be used
first by the compiler.
4. Expressions can be grouped with the use of parentheses. As a rule, the expressions inside the parenthesis are always
evaluated first by the compiler.
5. The result of an algebraic expression must be the same with the result of a computer expression.
DCIT 22 Computer Programming 1 –Developing Algorithm

HIERARCHY OF ARITHMETIC OPERATORS

^ Exponentiation
*/% Multiplication, Division and Modulus Division have the same level of
precedence
+- Addition and Subtraction have the same level of precedence

ASSIGNMENT OPERATION

- this operation is used to store a new value to the variable.

- can be distinguished through an equal symbol(=).

Examples

sum=num1+num2

A=B

grade=70

Types of Variable Utilized in an Assignment Operation

1. Constructive variable – maintains its value after being used in the process
2. Destructive variable – its value changes after the process

ALGORITHM AND FLOWCHART

Algorithm

It is a set of instruction that specify a sequence of operations to be carried out in order to solve a specific problem.

Ex. A recipe in cookbook is an excellent example of an algorithm.

Flowchart

 It is a diagram representing the logical sequence in which a combination of steps operations is to be performed.
 Generally, flowchart contains all the three stages of data processing namely: Input, Process, and Output.
 It consists of labelled geometrical symbols that are interconnected to provide a pictorial representation of a data
processing procedure.
 It is actually a visual representation of an algorithm.

Flowcharting Symbols

Symbol Representation Function

oblong Terminal Indicates the starting and ending points of the


flowchart.

hexagon Initialization/Preparation Assigns the initial values of the variables,


usually 0 to numeric and null to string.
DCIT 22 Computer Programming 1 –Developing Algorithm

parallelogram Input/Output Represents variables to be processed and


prints the required information.

rectangle Process Executes arithmetic operations involved.

Diamond Decision Select paths for various conditions

Circle On – Page Connector Shows an exit of the program path line from
one part of the flowchart or an entry to
another part of the flowchart on the same
page
Arrows Directions Indicates the direction of the flow in
processing data

Pentagon Off- Page Connector Shows an exit of the program path line from
one part of the flowchart or an entry to
another part of the flowchart of different
page.
Horizontal/Vertical Lines Flow Lines Used to show reading order or sequence in
which flowchart symbols are to be read.

Guidelines for Preparing a Flowchart

1. A flowchart always begin with a START/BEGIN and is completed by a STOP/END;


2. Symbols are interconnected using flow lines and arrows;
3. Use comma (,) to separate data;
4. An arrow’s head indicates the direction to be followed;
5. All symbols except diamond may have only one arrow branching out, but may have one or more arrows branching in;
6. Whenever circles are used, the symbol leading to circle should flow to the symbol where a circle containing a similar
character lead to.
7. The sequence of symbols matter because it indicates the logical steps to be followed;
8. A flowchart may contain many symbols of the same kind as necessary in the solution of the problem; and
9. There may be many types of flowcharts to solve one problem but the simplest flowchart is the most efficient.

Basic Control Structures – these are construct that are meant to direct the flow of flowcharts or programs in one way or another.

1. Sequence - the instructions are executed from one to another in a straightforward manner.
DCIT 22 Computer Programming 1 –Developing Algorithm

SAMPLE FLOWCHART

(a flowchart that displays the sum of two numbers)

Start

num1 = 0
num2 = 0
sum = 0

Input

num1,num2

sum = num1 + num2

Output

sum

Stop

STATEMENTS AND EXPRESSION

Statement – is a simple command that causes something to happen. It represent a single action taken in a Java program.
All the following are simple Java statements:

int weight =225;


System.out.println(“Hello World”);
sum = num1 + num2;
if(num1 == num2)

An expression is a statement that produces a value. The value produced by a statement is called its return value.

Some expression produce a numerical return value, as when two numbers are added together or multiplied. And others
produce a Boolean value – true or false.

VARIABLES
A variable is a place where information can be stored while a program is running. The value can changed at any point in
the program – hence the name.
DCIT 22 Computer Programming 1 –Developing Algorithm

Creating Variables
Before you can use a variable in a Java program, you must create the variable by declaring its name and the type of
information it will store. The types of information is listed first, followed by the name of the variable.

int num;
String message;
boolean gameOver;
float num1,num2,num3;

Variables can be assigned a value when they are created by using an equal sign (=) followed by the value.

int zipCode = 4110;


String name1 = “Umasou”,name2= ”Heart”,name3 =”Light”;
boolean gameOver = false;

Naming Variables
Variable names in Java must start with a letter, an underscore character (“_”), or a dollar sign ($). They cannot start with
a number. After the first character, variable names can include any combination of letters or numbers. However, special characters
such as @,#, etc are not permitted. In addition, spaces are not allowed to use in any variable. Java is case sensitive the
capitalization of letters must be consistent.

Java includes a number of keywords (if, else, for, do, while, etc). These keywords are reserved words that represent
command. They cannot be used as variables.

Data Types
There are eight basic types for the storage of integers, floating-point numbers, character, and Boolean values. These
often are called primitive types because they are built-in parts of the Java language

There are four data types you can use to store integers which one you use depends on the size of the integers.

Types Memory size Values that can be stored


byte 8 bits -128 to 127
short 16 bits -32,768 to 32,767
int 32 bits -2,147,483,648 to 2,147,483,647
long 64 bits -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807

Another types of number that can be stored is a floating-point number, which has the type float or double. The float
type should be sufficient for most uses because it can handle number from 1.4E-45 to 3.4E+38. If not, the double type can be
used for more precise numbers raging from 4.9E-324 to 1.7E+308

The char type is used for individual characters, such as letters, numbers, punctuation, and other symbols.

The last of the eight primitive data types is boolean. This data type holds either true or false in Java.

Looping

In computer science, a loop is a programming structure that repeats a sequence of instructions until a specific condition is met.
Programmers use loops to cycle through values, add sums of numbers, repeat functions, and many other things.
DCIT 22 Computer Programming 1 –Developing Algorithm

Loops are supported by all modern programming languages, though their implementations and syntax may differ. Two of the most
common types of loops are the while loop and the for loop.

Repetition (Looping) – provides for the repetitive execution of instructions in a specified number of times or until a
specified condition is satisfied.

Types of Loop

1. Pre-conditional loop
It is a conditional loop that checks the condition first before the instructions are executed.
2. Post-conditional loop
It is a conditional loop that executes the instructions first before checking the condition.
DCIT 22 Computer Programming 1 –Developing Algorithm

Create a flowchart that will display the Fibonacci sequence of a positive whole number. In this Fibonacci, the current third number
is the sum of the two previous numbers.
(Ex. If the user enters 7, the output should be 1 1 2 3 5 8 13)

Start

num = 0
ctr = 1
a=1
b=2
c=0

Input
num

N num > 0
Invalid input &&
num % 1 == 0

c=a+b ctr = ctr + 1

Output: a

a=b

b=c

Y
ctr < num

Stop
DCIT 22 Computer Programming 1 –Developing Algorithm

Create a flowchart that will accept a positive whole number and print the numbers from 1 to n; where n is the inputted number.

Start

num = 0
ctr =1

num

N
Invalid negative
numbers
num > 0

ctr ctr = ctr +1

Y
ctr < num

Stop
DCIT 22 Computer Programming 1 –Developing Algorithm

Start

SAMPLE FLOWCHART

(a flowchart that displays the


average of 10 numbers) num = 0
ctr = 1
sum = 0
ave = 0

Input
num

sum = sum +num

ctr = ctr +1

Y
ctr <= 10

ave = sum/10

Output
ave

Stop

You might also like