1/20
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
What is an algorithm
Set of rules to obtain the expected output from the given input.
Algorithmic Programming
its all about writing a set of rules with a finite number of steps that instruct the computer how to perform a task
a computer program is essentially an algorithm that tells the computer what specific steps to execute, in what order, in order to carry out a specific task.
algorithms are written using particular syntax, depending on the programming language being used
Rules of an algorithm
The sequence must have a starting point. This is the first step to be completed.
Each step (action) must lead to another step or complete the algorithm. This means you cant have hanging blocks that don’t go anywhere.
The task must be completed in a finite number of actions
Characteristics of an algorithm
Clear and Unambiguous: The algorithm should be clear and unambiguous. Each of its steps should be clear in all aspects and must lead to only one meaning
Well defined inputs: if an algorithm says to take inputs, it should be well defined inputs
well defined outputs: the algorithm must clearly define what output will be yielded and it should be well defined as well
finite-ness: the algorithm must be finite, ie, it should terminate after a finite time
feasible: the algorithm must be simple, generic, and practical, such that it can be executed with the available resources. It must not contain some future technology
language independent: the algorithm designed must be language independent it must be just plain instructions that can be implemented in any language, and yet the output will be the same, as expected
Parts of an Algorithm
Input - the values that are put into the algorithm
Process - what happens in the algorithm
Output - the value (or print out) that the algorithm produces
Ways to specify an Algorithm
Natural language
Pseudocode
Flowchart
Natural Language
it is very simple and easy to specify an algorithm using natural language, but many times, the specification of an algorithm by using natural language is not clear and thereby we get brief specification
such specification creates difficulty while actually implementing it. Hence, many programmers prefer to have specification of algorithm by means of pseudocode
Example of Natural Language
an algorithm to perform addition of 2 numbers
read the first number, say a
read the first number, say b
add the above 2 numbers and store the result in c
display the result from c
Pseudo-Code
pseudo code means fake code. It is a mixture of a natural language an programming language constructs.
it is text that is written pragmatically but designed to be read by humans and not a machine
there is no standard convention on what pseudo code should look like
the advantage of pseudocode is that anyone can read it regardless of which languages they are able to code in
it is usually more precise than natural language
for assignment operation left arrow “←”, for comments two slashes “//”, if conditions, for, while loops are used
Pseudo code for addition of two numbers
Algorithm sum (a,b)
//Problem Desc: This algorithm performs addition of 2 numbers
//Input: Two integers a and b
//Output: Addition of two integers
c ← a+b
return c
This specification is more useful for implementation of any language
Another way:
Read a, b
Add two numbers
Sum a+b;
Result Sum “the sum is”;
End
Write an algorithm to find the sum of 4 numbers
Start
Sum=0 and Count=0
Read the number n
Now, Sum = Sum+n and then the counter will be incremented by 1
count = count+1
Now check whether the count is less than 4 or not. If count is less than 4, then go to step 3. Otherwise, write “the sum is”
Stop
Write an algorithm to find out the largest of three numbers
Start
Read three numbers p,q,r
If p>q, go to step 5
If q>r, then write q is the largest number, else, write is the largest number
If p>r then write p is the largest number else write r is the largest number
stop
Write n algorithm to calculate the result when a number is given. If the number is greater than 50, then number must be increased 5 times. Otherwise, the number is decreased by 10
1) Start
2) Read number P
3) If P>50 then,
write P=P+5
else
write P-10
4) Write P
5) Stop
Flowcharts
Flowcharts are a graphical way of displaying an algorithm
it is a graphical representation of the logical flow of data
it uses the standard graphical symbols to narrate the sequential process of a specific module. The steps must be followed while designing the whole program.
Flowcharts are very effective in understanding how a process works
Flowcharts are also known as process maps that can be used to identify: (a) Flow of info, (b) Number of steps in a process, c) Branches in a process, (d) Interdependent operations
Flowchart symbols:
start/stop
every flowchart has a starting point and a terminating point. The symbol that is used is a rounded rectangle for representation. It is called a terminal
Input/Output
Every time you take an input from a user and rather an output to the user, an input/output symbol is used in the flowchart. The symbol that is used for better input/output related actions is a parallelogram
Process
if you are running instruction, you need to use a rectangular box in the flowchart
Decision Symbol
in a flowchart, a decision symbol as shown below is used for answering questions in the form of either true or false or yes or no. Note that each answer can lead you to a different path in the flowchart
Flow Lines
they depict the direction of a flow in a flowchart. There are 4 lines
Connector
It connects different steps in s flowchart hat are on different pages and gives a sense of continuation. Overall, it is use in extremely complex flowcharts and is denoted by a small circle
Guidelines for drawing a Flowchart
describe the process to be charted
start with a trigger event. Eg: count your money for starting the process of counting
3. usually, direction of flow of a process if from left to right or top to bottom
please note that only one flow line should come out of a process symbol
also, only on flow line should enter a decision symbol. However, two or three flow lines can leave the same decision symbol
only one flow line is used in conjunction with terminal symbol
it is important to ensure that a flowchart has a start and end
a flowchart can have only one start terminal. However, it can sometimes lead to more than one terminal symbol
it is also important to stop a flowchart at a logical conclusion