Академический Документы
Профессиональный Документы
Культура Документы
UNIT I
Algorithms
● Algorithm is an ordered sequence of finite, well defined, unambiguous instructions for
completing a task.
● Step – by – step procedure to write a program.
Characteristics
● Precise and unambiguous.
● Should not be repeated.
● Should be written in sequence.
● It looks like normal English
Algorithm to find greatest among three numbers
Step 1 : Start
Step 2 : Read the three numbers A, B, C
Step 3 : Compare A & B, If A is greater, perform step 4 else perform step 5.
Step 4 : Compare A & C, If A is greater, output “A is greatest” else output “C is greatest”.
Perform step 6
Step 5 : Compare B & C, If B is greater, output “B is greatest” else output “C is greatest”.
Step 6 : Stop
● Instructions/Statements
● State
● Control Flow
● Functions
Instructions/Statements
An algorithm is a part of the blueprint or plan for the computer program. It is also an
effective procedure for solving a problem in a finite number of steps.
● Description of problem
● Set up
● Parameters
● Execution
● Conclusion
The simple example below is to find the sum of the two numbers
State
A computer program stores data in variables, which represent storage locations in the
computer's memory. The content of these memory locations, at any given point in the program’s
execution, is called the program's state.
Sequential control
Sequential control means that the steps of an algorithm are carried out in a sequential
manner, where each step is executed exactly once.
Algorithm
Step 1: Start
Step 2: Input the value of A and B.
Step 3: Find the sum of A and B.
SUM=A+B
Step 4: Print the value of SUM.
Step 5: Stop
Condition Control
In Condition Control only one flow of number of alternative steps is executed based on
the condition.
True
False
Start
Stop
Algorithm
Step 1: Start
Step 2: Read the value of n
Step 3: if (n%2==0) then
print it is an even number
else
print it is an odd number
Step 4: Stop
Control flow
In control flow one or more steps are performed repeatedly. This logic is used for
producing loops in program logic, when one or more instructions may be executed several times
depending on some conditions.
False
Start
Stop
True
Algorithm
Step 1: Start
Step 2: Read the values of n
Step 3: set initial values to Fact =i=1
Step 4: Is i<=n else go to step 6
Fact=Fact*i
i=i+1
Algorithm
Step 1: Start
Step 2: Read n1, n2, n3
Step 3: Call the function Average and pass the parameters
Step 4: Print the value of average
Step 5: Stop
Function Average
Step 1: Calculate sum=a+b+c
Step 2: Calculate result=sum/3
Step 3: Return the value of result to main function
Notation
Pseudocode
✓ Pseudocode is written in normal English and cannot be understood by the computer.
✓ Only concentrate on logic of the program without worrying the syntax of the
instructions.
Rules for writing Pseudocode
Flowchart
● A flowchart is a diagrammatic representation of the logic for solving a task.
Characteristics
● Program preparation can be simplified.
● Easier to understand.
● Easy to analyze and compare.
● Assist in reviewing and debugging
● Provide effective programming documentation.
Symbols
Symbol Name Symbol Use
Stop
Terminal Start symbol and End symbol
Start
Rules for drawing flowchart
∗ The standard symbols should only be used
∗ Arrow head represents the direction of flow of control.
∗ Usual direction – top to bottom or left to right
∗ The flowchart should be clear, neat and easy to follow.
∗ The flowchart must have a logical start and finish.
∗ Only one flow line should come out from a process symbol.
∗ Only one flow line should enter a decision symbol. However, two or three flow lines may
leave the decision symbol.
∗ Only one flow line is used with a terminal symbol.
∗ The flow lines should not cross each other.
∗ Be consistent in using names and variables.
∗ Keep the flowchart as simple as possible.
Advantages
✓ Understand logic clearly
✓ Better communication
✓ Effective analysis
✓ Effective coding
✓ Effective program maintenance
Disadvantages
✓ Complex logic
✓ Alterations and Modifications.
✓ Reproduction.
✓ Cost.
Programming Languages
List of some of the different fields of programming, as well as the languages to consider
for each is explained below.
Applications and Program development
Application and program development involves programs you work with on a daily basis.
For example, the Internet browser you are using to view this web page is considered a program.
If you are interested in developing a program, you should consider the following languages.
● C
● C#
● C++
● D
● Java
● Tcl
● Visual Basic
● Prolog
● Python
Database development
Database developers create and maintain databases. If you're interested in creating or
maintaining a database, you should consider any of the following languages.
● DBASE
● FoxPro
● MySQL
● SQL
● Visual FoxPro
Game development
Game development involves the development of computer games or other
entertainment software. If you're interested in developing a game, you should consider the
following languages.
● C
● C#
● C++
● DarkBASIC
● Java
Script development
Although it is not likely to become a career, knowing how to create and develop scripts
can increase productivity for you or your company, saving you countless hours. If you're
interested in developing scripts, consider the following languages.
● Autohotkey
● awk
● bash
● Batchfile
● Perl
● Python
● Tcl
Algorithmic Problem Solving
Algorithmic problem solving is about the formulation and solution of problems where
the solution involves, possibly implicitly, the principles and techniques that have been developed
to assist in the construction of correct algorithms.
The Problem Solving process consists of a sequence of sections that fit together
depending on the type of problem to be solved. These are:
● Problem Definition.
● Problem Analysis.
● Generating possible Solutions.
● Analyzing the Solutions.
● Selecting the best Solution(s).
● Planning the next course of action (Next Steps)
1. Problem Definition
2. Problem Analysis
problem is coming from, how it fits in with current developments and what the current
environment is, is crucial when working out whether a solution will actually work or not.
Similarly you must have a set of criteria by which to evaluate any new solutions or you will not
know whether the idea is workable or not. This section of the problem solving process ensures
that time is spent in stepping back and assessing the current situation and what actually needs to
be changed.
After this investigation, it is often good to go back one step to reconfirm that your
problem definition is still valid. Frequently after the investigation people discover that the
problem they really want to answer is very different from their original interpretation of it.
This section of the problem solving process is where you investigate the various factors
about each of the potential solutions. You note down the good and bad points and other things
which are relevant to each solution. Even at this stage you are not evaluating the solution
because if you do so then you could decide not to write down the valid good points about it
because overall you think it will not work. However you might discover that by writing down its
advantages that it has a totally unique advantage. Only by discovering this might you choose to
put the effort in to develop the idea so that it will work.
This is the section where you look through the various influencing factors for each
possible solution and decide which solutions to keep and which to disregard. You look at the
solution as a whole and use your judgement as to whether to use the solution or not. In
Innovation Toolbox, you can vote using either a Yes/No/Interesting process or on a sliding
scale depending on how good the idea is. Sometimes pure facts and figures dictate which ideas
will work and which will not. In other situations, it will be purely feelings and intuition that
decides. Remember that intuition is really a lifetimes experience and judgement compressed into
a single decision.
By voting for the solutions you will end up with a shortlist of potential solutions. You
may want to increase the depth in the analysis of each idea and vote again on that shortlist to
further refine your shortlist.
You will then end up with one, many or no viable solutions. In the case where you have
no solutions that work, you will need to repeat the generation of solutions section to discover
more potential solutions. Alternatively you might consider re-evaluating the problem again as
sometimes you may not find a solution because the problem definition is not well defined or
self-contradictory.
Sequential control means that the steps of an algorithm are carried out in a sequential
manner, where each step is executed exactly once.
Algorithm
Step 1: Start
Step 2: Input the value of A and B.
Step 3: Find the sum of A and B.
SUM=A+B
Step 4: Print the value of SUM.
Step 5: Stop
In Selection control only one flow of number of alternative steps is executed based on
the condition.
True
False
Start
Stop
Algorithm
Step 1: Start
Step 2: Read the value of n
Step 3: if (n%2==0) then
print it is an even number
else
print it is an odd number
Step 4: Stop
Iteration Control Structure (Looping)
In control flow one or more steps are performed repeatedly. This logic is used for
producing loops in program logic, when one or more instructions may be executed several times
depending on some conditions.
False
Start
Stop
True
Algorithm
Step 1: Start
Step 2: Read the values of n
Step 3: set initial values to Fact =i=1
Step 4: Is i<=n else go to step 6
Fact=Fact*i
i=i+1
Recursion
The process in which a function calls itself directly or indirectly is called recursion and
the corresponding function is called as recursive function. Using recursive algorithm, certain
problems can be solved quite easily. Examples of such problems are Towers of Hanoi
(TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.
Algorithm:
Step 1: Start
Step 2: Read input value n
Step 3: Call and print function fact(n)
Step 4: Stop
Function fact
Step 1: Function fact start
Step 2: if n=0 return 1 else goto step 3
Step 3: return (n*fact(n-1)
Illustrative Problems:
Find Minimum and Maximum In A List
Algorithm:
Step 1: Start
Step 2: Read the upper limit of the list ‘n’
Step 3: Read an list a[ ] of n numbers
Flowchart
Insert A Card In A List Of Sorted Cards
Algorithm
Step 1: Start
Step 2: if it is the first card, it is already sorted. Return 1
Step 3: Pick next card
Step 4: Compare with all cards in the sorted list of cards
Step 5: Shift all the cards in the sorted list of cards that is greater than the card to be
inserted.
Step 6: Insert the card
Step 7: Repeat until entire list is sorted
Step 8: Stop
Flowchart
Pseudocode
Guess An Integer Number In A Range
Algorithm
Step 1: start
Step 2: Read guess number and random number n
Step 3: Check if guess less than random number
Step 3.1: print “too low”
Step 4: check if guess greater than random number
Step 4.1: Print “too high”
Step 5: if guess equal to random number
Step 5.1: print “Good job”
Step 6: else print “You Lose”
Step 7: Stop
Flowchart
Pseudocode:
ELSE IF GUESS<n
PRINT “Too Low”
ELSE IF GUESS==n
PRINT “Good job”
ELSE
PRINT “You Lose”
Towers Of Hanoi
Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and
more than one rings is as depicted −
These rings are of different sizes and stacked upon in an ascending order, i.e. the smaller one
sits over the larger one. There are other variations of the puzzle where the number of disks
increase, but the tower count remains the same.
Rules
The mission is to move all the disks to some another tower without violating the sequence of
arrangement. A few rules to be followed for Tower of Hanoi are −
● Only one disk can be moved among the towers at any given time.
Tower of Hanoi puzzle with n disks can be solved in minimum 2 −1 steps. This presentation
n
Step 1: Start
Step 2: Procedure Hanoi(disk, source, dest, aux)
Step 3: if disk==0
Step 3.1: move disk from source to dest
Step 4: else
Step 4.1: Hanoi(disk-1, source, aux, dest)
Step 4.2: move disk from source to dest
Step 4.3: Hanoi(disk-1, aux, dest, source)
Step 5: end if
Step 6: Stop
Flowchart
Pseudocode