Lecture 02 C#
Lecture 02 C#
Mr. Gambadu
Facilitator
Unit II: Basic Solutions Designing using Simple
Algorithms
Contents:
Algorithms and their importance
Algorithmic design
Syntax and semantics
Stepwise refinement of algorithms
Sequence
Selection
Algorithms
The word Algorithm means ” A set of finite rules or instructions to be followed in
calculations or other problem-solving operations ” Or ” A procedure for solving a
mathematical problem in a finite number of steps that frequently involves recursive
operations”.
The Algorithm designed are language-independent, i.e. they are just plain
instructions that can be implemented in any language, and yet the output will
be the same, as expected.
As one would not follow any written instructions to cook the recipe, but only the standard one.
Similarly, not all written instructions for programming is an algorithms. In order for some
instructions to be an algorithm, it must have the following characteristics:
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. It may
or may not take input.
Well-Defined Outputs: The algorithm must clearly define what output will be yielded and it
should be well-defined as well. It should take at least 1 output.
Finite-ness: The algorithm must be finite, i.e. 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 or anything.
Language Independent: The Algorithm designed must be language-independent, i.e. it must be
just plain instructions that can be implemented in any language, and yet the output will be the
same, as expected.
Properties of Algorithm:
It should terminate after a finite time.
It should produce at least one output.
It should take zero or more input.
It should be deterministic means giving the same output for the
same input case.
Every step in the algorithm must be effective i.e. every step should
do some work.
Advantages of Algorithms:
It is easy to understand.
An algorithm is a step-wise representation of a solution to a given problem.
In 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.
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).
How to Design an Algorithm?
In order 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 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, in order to write an algorithm, its pre-requisites 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 ‘+’ 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:
Note that: One problem, many solutions; The solution to an algorithm can be
or cannot be more than one. It means that while implementing the algorithm, there
can be more than one method to implement it. For example, in the above problem
to add 3 numbers, the sum can be calculated in many ways like:
+ operator
Bit-wise operators
. . etc
Algorithm complexity and how to find it?
An algorithm is defined as complex based on the amount of Space and Time it
consumes. Hence the Complexity of an algorithm refers to the measure of the
Time that it will need to execute and get the expected output, and the Space it will
need to store all the data (input, temporary data and output). Hence these two
factors define the efficiency of an algorithm.