0% found this document useful (0 votes)
77 views13 pages

Chapter II

Uploaded by

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

Chapter II

Uploaded by

Lina Hamrit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

I.

Introduction :

Algorithm is a step by step logical approach and program is a set of instructions for a given
task.If you want to know more about this then read this article.

An algorithm can be defined as a systematic approach to solving a specific problem. An


understanding of algorithms is essential for programmers to program more efficiently. It is
equally important for programmers to understand the difference between algorithms and
programs for optimizing a program according to the available resources.
II.What is an Algorithm?
An algorithm is a step-by-step and logical approach that defines a systematic process for
computers to solve a specific problem. It consists of a set of rules defining how a task will
be executed to get the expected results. Algorithms are conceptual and can be described
using language or flowcharts. We can implement them in different programming languages
. When we use a computer to solve a specific problem, we need to tell these steps to the
solution clearly to the computer.

III. Characteristics of an Algorithm


 An algorithm should be unambiguous.

 It should take well-defined inputs.

 The algorithm should take well-defined outputs.

 It should be simple, generic, and can be executed using the available resources.

 The algorithm must be finite.

 It must be language-independent.

III.1 Types of Algorithms


Here are the most popular type of algorithms:

 Recursive Algorithm – The Algorithm will call itself repeatedly until the problem is
solved. Example: Computing factorials
 Greedy Algorithm – Algorithms that are used for solving optimization problems.
Example: Huffman tree
 Brute Force Algorithm – It is one of the simplest algorithms in the concept. It iterates
all possible solutions to search for one or more possible solutions that may solve a
function. Example: Matching algorithm
 Backtracking Algorithm – This algorithm follows an incremental approach to finding
a solution to a problem. The algorithm will solve problems recursively. It will find a
solution to a problem by solving one piece of the problem at a time. If one of the
solutions fails, it is removed, and the algorithm backtracks to find another solution.
Example: Queens Problem
 Divide and Conquer Algorithm – As the name suggests, the algorithm is divided into
two parts. The first part divides the problem into smaller subproblems of the same type.
In the second part, these smaller problems are solved and then combined to produce the
final solution to the problem. Example: Merge sort
IV. What is a Program?
A program is a set of instructions a computer follows to perform a specified task. Many
programming languages can be used to write computer programs. Some popular
programming languages include pascal ,Python, Java, C++, JavaScript, PHP, and Ruby.
These high-level programming languages are human-readable and writable. These
languages are converted into low-level machine languages by compilers, interpreters, and
assemblers within the computer system.
A program tells the computer how to accept input, manipulate that input, and display the
output in some form that humans find useful.

For Example, in a word processor:

 The input will be the characters you type from the keyboard

 The program will format the text and correct the spelling

 Well-organized will be displayed on the screen as output

V. Difference Between Program and Algorithm


Computer algorithms solve the problem while computer programs implement them in a
form that a computer can execute. Here are the main differences between algorithms and
programs:
IV. Characteristics of an Algorithm :
There are some characteristics which every algorithm should follow.There are five different
characteristics which deal with various aspects of algorithm.They are as follows:
1.Input specified
2.Output specified
3.Definiteness
4.Effectiveness
5. Finiteness
6.Independent
Let us see these characteristics one by one.
1)Input specified
The input is the data to be transformed during the computation to produce the output.An
algorithm should have 0 or more well-defined inputs.Input precision requires that you know
what kind of data, how much and what form the data should be
2)Output specified
The output is the data resulting from the computation (your intended result). An algorithm
should have 1 or more well-defined outputs, and should match the desired output.Output
precision also requires that you know what kind of data, how much and what form the output
should be (or even if there will be any output at all!).
3)Definiteness
Algorithms must specify every step and the order the steps must be taken in the
process.Definiteness means specifying the sequence of operations for turning input into
output. Algorithm should be clear and unambiguous.Details of each step must be also be
spelled out (including how to handle errors).It should contain everything quantitative and not
qualitative.
You could not expect a computer to understand something if you yourself are ambiguous
about it.Right!
4)Effectiveness
For an algorithm to be effective, it means that all those steps that are required to get to output
must be feasible with the available resources.It should not contain any unnecessary and
redundant steps which could make an algorithm ineffective.
For example,suppose you are cooking a recipe and you chop vegetables which are not be
used in the recipe then it is a waste of time.

5)Finiteness
The algorithm must stop, eventually.Stopping may mean that you get the expected output
OR you get a response that no solution is possible. Algorithms must terminate after a finite
number of steps.An algorithm should not be infinite and always terminate after definite
number of steps.
There is no point in developing an algorithm which is infinite as it will be useless for us.
6)Independent
An algorithm should have step-by-step directions, which should be independent of any
programming code.It should be such that it could be run on any of the programming
languages.
Thus,these are the characteristics that an algorithm should have for its fruitfulness.
VI. 1.Advantages of Algorithms:
 It is easy to understand.
 An algorithm is a step-wise representation of a solution to a given problem.
 In an Algorithm the problem is broken down into smaller pieces or steps hence, it is
easier for the programmer to convert it into an actual program.
IV.2. Disadvantages of Algorithms:
 Writing an algorithm takes a long time so it is time-consuming.
 Understanding complex logic through algorithms can be very difficult.
 Branching and Looping statements are difficult to show in Algorithms(imp).
VI.3. How to Design an Algorithm?
To write an algorithm, the following things are needed as a pre-requisite:
1. The problem that is to be solved by this algorithm i.e. clear problem definition.
2. The constraints of the problem must be considered while solving the problem.
3. The input to be taken to solve the problem.
4. The output is to be expected when the problem is solved.
5. The solution to this problem is within the given constraints.
Then the algorithm is written with the help of the above parameters such that it solves the
problem.
Example: Consider the example to add three numbers and print the sum.
Step 1: Fulfilling the pre-requisites
As discussed above, to write an algorithm, its prerequisites must be fulfilled.
1. The problem that is to be solved by this algorithm: Add 3 numbers and print their sum.
2. The constraints of the problem that must be considered while solving the problem:
The numbers must contain only digits and no other characters.
3. The input to be taken to solve the problem: The three numbers to be added.
4. The output to be expected when the problem is solved: The sum of the three numbers
taken as the input i.e. a single integer value.
5. The solution to this problem, in the given constraints: The solution consists of adding
the 3 numbers. It can be done with the help of the ‘+’ operator, or bit-wise, or any other
method.
Step 2: Designing the algorithm
Now let’s design the algorithm with the help of the above pre-requisites:
Algorithm to add 3 numbers and print their sum:
1. START
2. Declare 3 integer variables num1, num2, and num3.
3. Take the three numbers, to be added, as inputs in variables num1, num2, and num3
respectively.
4. Declare an integer variable sum to store the resultant sum of the 3 numbers.
5. Add the 3 numbers and store the result in the variable sum.
6. Print the value of the variable sum
7. END
Step 3: Testing the algorithm by implementing it.
To test the algorithm, let’s implement it in C or pascal language.

Here is the step-by-step algorithm of the code:


1. Declare three variables num1, num2, and num3 to store the three numbers to be added.
2. Declare a variable sum to store the sum of the three numbers.
3. Use the cout statement to prompt the user to enter the first number.
4. Use the cin statement to read the first number and store it in num1.
5. Use the cout statement to prompt the user to enter the second number.
6. Use the cin statement to read the second number and store it in num2.
7. Use the cout statement to prompt the user to enter the third number.
8. Use the cin statement to read and store the third number in num3.
9. Calculate the sum of the three numbers using the + operator and store it in the sum
variable.
10. Use the cout statement to print the sum of the three numbers.
11. The main function returns 0, which indicates the successful execution of the program.
VI.4. Flowcharts:

Flowcharting is a tool developed in the computer industry, for showing the steps involved
in a process. A flowchart is a diagram made up of boxes, diamonds and other shapes,
connected by arrows - each shape represents a step in the process, and the arrows show the
order in which they occur. Flowcharting combines symbols and flowlines, to show
figuratively the operation of an algorithm.

In computing, there are dozens of different symbols used in flowcharting (there are even
national and international flowcharting symbol standards). In business process analysis, a
couple of symbols are sufficient. A box with text inside indicates a step in the process,
while a diamond with text represents a decision point. See the figure for an example.

If the flowchart is too messy to draw, try starting again, but leaving out all of the decision
points and concentrating on the simplest possible course. Then the session can go back
and add the decision points later. It may also be useful to start by drawing a high-level
flowchart for the whole organisation, with each box being a complete process that has to
be filled out later.

From this common understanding can come a number of things - process improvement
ideas will often arise spontaneously during a flowcharting session. And after the session,
the facilitator can also draw up a written procedure - a flowcharting session is a good way
of documenting a process.
Process improvement starts with an understanding of the process, and flowcharting is the
first step towards process understanding.
VI.5. Flowcharting Symbols

There are 6 basic symbols commonly used in flowcharting of assembly language


programs: Terminal, Process, input/output, Decision, Connector and Predefined Process.
This is not a complete list of all the possible flowcharting symbols, it is the ones used
most often in the structure of Assembly language programming.

Symbol Name Function

Process Indicates any type of internal operation


inside the Processor or Memory

input/output Used for any Input / Output (I/O) operation.


Indicates that the computer is to obtain data
or output results

Decision Used to ask a question that can


be answered in a binary format (Yes/No,
True/False)

Connector Allows the flowchart to be drawn without


intersecting lines or without a reverse flow.

Predefined Process Used to invoke a subroutine or an


interrupt program.

Terminal Indicates the starting or ending of the


program, process, or interrupt
program.

Flow Lines Shows direction of flow.

Generally, there are many standard flowcharting symbols.


General Rules for flowcharting

1. All boxes of the flowchart are connected with Arrows. (Not lines)
2. Flowchart symbols have an entry point on the top of the symbol with no other
entry points. The exit point for all flowchart symbols is on the bottom except for
the Decision symbol.
3. The Decision symbol has two exit points; these can be on the sides or the bottom
and one side.

4. Generally a flowchart will flow from top to bottom. However, an upward flow
can be shown as long as it does not exceed 3 symbols.
5. Connectors are used to connect breaks in the flowchart. Examples are:
 From one page to another page.
 From the bottom of the page to the top of the same page.
 An upward flow of more then 3 symbols
6. Subroutines and Interrupt programs have their own and independent flowcharts.
7. All flow charts start with a Terminal or Predefined Process (for interrupt
programs or subroutines) symbol.
8. All flowcharts end with a terminal or a contentious loop.

Flowcharting uses symbols that have been in use for a number of years to represent the
type of operations and/or processes being performed. The standardised format provides a
common method for people to visualise problems together in the same manner. The use
of standardised symbols makes the flow charts easier to interpret, however, standardising
symbols is not as important as the sequence of activities that make up the process.

Flowcharting Tips

 Chart the process the way it is really occurring. Do not document the way a written
process or a manager thinks the process happens.

 People typically modify existing processes to enable a more efficient process. If the
desired or theoretical process is charted, problems with the existing process will not
be recognised and no improvements can be made.

Note all circumstances actually dealt with.


 Test the flow chart by trying to follow the chart to perform the process charted. If
there is a problem performing the operation as charted, note any differences and
modify the chart to correct. A better approach would be to have someone unfamiliar
with the process try to follow the flow chart and note questions or problems found.
 Include mental steps in the process such as decisions. These steps are sometimes left
out because of familiarity with the process, however, represent sources of problems
due to a possible lack of information used to make the decision can be inadequate or
incorrect if performed by a different person.

VI.6. Examples of Algorithms and Flowcharts:

The problem with this algorithm is that, some of the steps appear more than once, i.e. step
5 get second number, step 7, get third number, etc. One could shorten the algorithm or
flowchart as follows:

1. Start
2. Sum = 0
3. Get a value
4. sum = sum + value
5. Go to step 3 to get next Value
6. Output the sum
7. Stop

This algorithm and its corresponding flowchart are a bit shorter than the first one. In this
algorithm, step 3 to 5 will be repeated, where a number is obtained and added to sum.
Similarly the flowchart indicates a flowline being drawn back to the previous step
indicating that the portion of the flowchart is being repeated. One problem indicates that
these steps will be repeated endlessly, resulting in an endless algorithm or flowchart. The
algorithm needs to be improved to eliminate this problem. In order to solve this problem,
we need to add a last value to the list of numbers given. This value should be unique so
that, each time we get a value, we test the value to see if we have reached the last value.
In this way our algorithm will be a finite algorithm which ends in a finite number of steps
as shown below. There are many ways of making the algorithm finite.
The new list of numbers will be 26, 49, 498, 9387, 48962, 1, -1. The value –1 is a unique
number since all other numbers are positive.

1. Start
2. Sum = 0
3. Get a value
4. If the value is equal to –1, go to step 7
5. Add to sum ( sum = sum + value)
6. Go to step 3 to get next Value
7. Output the sum
8. Stop

Corresponding flowchart

3. DATA TYPES

Although some contemporary languages allow programmers to invent his own data
types, and define their related operations, there are a number of traditional data types
found in most languages:

Integer
Integers are numeric data items, which are either positive or negative including zero, i.e.
1, 488, -22, 0, 456. Some programming languages put restrictions on the magnitude of
integers which may be used in program instructions. These restrictions are usually
dependent on the size of the memory location of the computer in which the language
may run.

Real Numbers

There are two types of real numbers, Fixed-Point and Floating Point.

Fixed Point

Fixed point data items are numbers which have embedded decimal point i.e. 1.5,
458.4589, -0.569.

Floating Point

Floating point data items are numbers, which are, held as binary fractions by a computer.
The numbers are expressed in a form where you have a mantissa and an exponent, for
example

Number Mantissa Exponent


12.3 = 0.123 * 102 0.123 2
123000 = 0.123 * 106 0.123 6
0.000123 =0.123 * 10-3 0.123 -3

Floating point representation of data is used to overcome the restrictions placed on the
magnitude of numbers by the size of computer’s memory locations.

Character
Character data, sometimes referred to as “string” data, may consist of any digits, letters of
the alphabet or symbols which, the internal coding system of the computer is capable of
representing. Many programming languages require character data to be enclosed by
quotation marks when used in program instructions, for example PRINT “HAPPY NEW
YEAR”.

Boolean
Boolean data items are used as status indicators and may contain only one of two possible
values: True or False.

DATA ITEM

There are two basic methods of using data items in a program:


a) Constants

Data items are sometimes required to keep their values throughout the program, hence the
term constant. A constant is a specific value or character string used explicitly in an
operation. Consider the constant values 47.5, 1, and 13 in the example below.

Multiply … by 47.5
Add 1 to …
If … = 13
Writt “PLEASE INPUT TODAY’S DATE”
b) Variables and Variable names

A variable is a symbolic name assigned to a data item by the programmer. At any


particular time, a variable will stand for one particular data, called the value of a variable,
which may change from time to time during a computing process. The value of a variable
may change many times during the execution of a program. A variable is usually given a
name by the programmer.
13

You might also like