Monday 6 July 2015

Algorithms and Pseudocode

Last time we had a look at C++ operators and data types.

The objective of computer science is to solve problems by developing, analyzing, and implementing algorithmic solutions, so today we will learn about algorithms and pseudo-codes.

Algorithms: 
Algorithm is a step by step procedure for solving a problem, it represents a process that a computer carries out, in order to complete a well defined task. 

Al-Khwarizmi Principle:

  • All complex Problems can be solved by breaking them into simpler sub-problems
  • then solve them , one at a time (in a specified order)
  • When all the steps are solved the original problem is solved
  • this is called Algorithm (originating from Al-Khwarizmi)
Lets consider an example, suppose we have to calculate and print the average grade of 3 tests for the entire class, in this case: 



Flow charts:
A flowchart is a visual or graphical representation of an algorithm.The flowchart employs a series of blocks and arrows, each of which represents a particular operation or step in the algorithm.The arrows represent the sequence in which the operations are implemented.

given below are the most common symbols used in a flow chart:


for example the flowchart below shows how to find the solution of a quadratic expression Ax^2+Bx+C=0, given A, B and C:

another example is:


Algorithm representation:
  • Algorithms can be represented in natural language (English or any other language)  but this representation is too verbose,unstructured, ambiguous and imprecise.
  • representation through programming language is not too good either, as the syntax is too rigid and we have to worry too much about the implementation details. it is very easy to lose sight of the real task.
  • so for this purpose we need a compromise between the two. computer scientists use pseudocode to express algorithms. these are English like constructs but modeled to look like statements in a typical programming language.

given below is a comparison of algorithms, flowcharts and pseudocode:


Pseudo-code is kind of a programming language without a rigid syntax, for example we can write:
– Set the value of A to (B+C) 
as
–Set A to (B+C)
or even:
Set the value of sum to 0
Set the value of GPA to 0
as
Set sum and GPA to 0
another example is:


Tracing an Algorithm:
The current values of algorithm variables at various  points during execution can be known by tracing the algorithm with a table called Trace Table 

Problem: Determine the value of the variable x and y after the following algorithm is executed

Pseudo-code:
x = 5
Y = 7
If x = 5 then
  y= 8
else
  y= 0
if y = 7 then
  x = 6
else
  x = 3
  if x = y then
  y = 0 
trace table for this algorithm will be:







About the Author

Unknown

Editorial Team

Post a Comment

 
Codexify © 2015 - Designed by Templateism.com