Professional Documents
Culture Documents
Developing Algorithm
Developing Algorithm
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.
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. 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
B. Rational - used to compare two data values against each other and produce true or false results
C. Logical – used to combine two or more comparison tests into a single compound comparison 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
^ Exponentiation
*/% Multiplication, Division and Modulus Division have the same level of
precedence
+- Addition and Subtraction have the same level of precedence
ASSIGNMENT OPERATION
Examples
sum=num1+num2
A=B
grade=70
1. Constructive variable – maintains its value after being used in the process
2. Destructive variable – its value changes after the process
Algorithm
It is a set of instruction that specify a sequence of operations to be carried out in order to solve a specific problem.
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
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.
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
Start
num1 = 0
num2 = 0
sum = 0
Input
num1,num2
Output
sum
Stop
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:
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.
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.
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
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
Y
ctr < num
Stop
DCIT 22 Computer Programming 1 –Developing Algorithm
Start
SAMPLE FLOWCHART
Input
num
ctr = ctr +1
Y
ctr <= 10
ave = sum/10
Output
ave
Stop