0% found this document useful (0 votes)
26 views

Lecture 02 C#

Uploaded by

levisqueen26
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Lecture 02 C#

Uploaded by

levisqueen26
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

CS 111: Procedural Programming in 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”.

Therefore Algorithm refers to a sequence of finite steps to solve a


particular problem.
Algorithms can be simple and complex depending on what you want to achieve.
It can be understood by taking the example of cooking a new recipe. To cook a
new recipe, one reads the instructions and steps and executes them one by one,
in the given sequence. The result thus obtained is the new dish cooked
perfectly. Every time you use your phone, computer, laptop, or calculator you
are using Algorithms. Similarly, algorithms help to do a task in programming
to get the expected output.

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:

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.
In order to test the algorithm, implement it in C language. (This will be done later )

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.

The two factors of Algorithm Complexity are:


Time Factor: Time is measured by counting the number of key operations such as
comparisons in the sorting algorithm.
Space Factor: Space is measured by counting the maximum memory space required
by the algorithm to run/execute.
How to express an Algorithm?
Natural Language :- Here we express the Algorithm in natural English language. It
is too hard to understand the algorithm from it.
Flow Chat :- Here we express the Algorithm by making graphical/pictorial
representation of it. It is easier to understand than Natural Language.
Pseudo Code :- Here we express the Algorithm in the form of annotations and
informative text written in plain English which is very much similar to the real
code but as it has no syntax like any of the programming language, it can’t be
compiled or interpreted by the computer. It is the best way to express an algorithm
because it can be understood by even a layman with some school level
programming knowledge.
FLOWCHARTS
Flowcharts are nothing but the graphical representation of the data or the
algorithm for a better understanding of the code visually. It displays step-by-step
solutions to a problem, algorithm, or process. It is a pictorial way of representing
steps that are preferred by most beginner-level programmers to understand
algorithms of computer science, thus it contributes to troubleshooting the issues in
the algorithm. A flowchart is a picture of boxes that indicates the process flow in a
sequential manner. Since a flowchart is a pictorial representation of a process or
algorithm, it’s easy to interpret and understand the process. To draw a flowchart,
certain rules need to be followed which are followed by all professionals to draw a
flowchart and is widely accepted all over the countries.
Use of a flowchart
Following are the uses of a flowchart:

1) It is a pictorial representation of an algorithm that increases the readability of the program.


2) Complex programs can be drawn in a simple way using a flowchart.
3) It helps team members get an insight into the process and use this knowledge to collect data,
detect problems, develop software, etc.
4) A flowchart is a basic step for designing a new process or add extra features.
5) Communication with other people becomes easy by drawing flowcharts and sharing them.
Here are some of the common flowchart symbols;
1. Terminal: This box is of an oval shape which is used to indicate the start or end
of the program. Every flowchart diagram has this oval shape that depicts the start of
an algorithm and another oval shape that depicts the end of an algorithm. For
example:
2. Data: This is a parallelogram-shaped box inside which the inputs or outputs are
written. This basically depicts the information that is entering the system or
algorithm and the information that is leaving the system or algorithm.
For example: if the user wants to input a from the user and display it, the flowchart
for this would be:
3. Process This is a rectangular box inside which a programmer writes the main
course of action of the algorithm or the main logic of the program. This is the crux
of the flowchart as the main processing codes is written inside this box. For
example: if the programmer wants to add 1 to the input given by the user, he/she
would make the following flowchart:
4. Decision This is a rhombus-shaped box, control statements like if, or condition
like a > 0, etc are written inside this box. There are 2 paths from this one which is
“yes” and the other one is “no”. Like every decision has either yes or no as an
option, similarly, this box to have these as options. For example: if the user wants
to add 1 to an even number and subtract 1 if the number is odd, the flowchart
would be:
5. Flow This arrow line represents the flow of the algorithm or process. It
represents the direction of the process flow. in all the previous examples, we
included arrows in every step to display the flow of the program. arrow increases
the readability of the program.
Advantages of Flowchart
1) It is the most efficient way of communicating the logic of system.
2) It act like a guide for blueprint during program designed.
3) It also helps in debugging process.
4) Using flowchart we can easily analyze the programs.
5) flowcharts are good for documentation.
Disadvantages of Flowchart
6) Flowcharts are difficult to draw for large and complex programs.
7) It does not contain the proper amount of details.
8) Flowcharts are very difficult to reproduce.
9) Flowcharts are very difficult to modify.
Question 1. Draw a flowchart to find the greatest number among the 2
numbers.
Solution:
Algorithm:
1. Start
2. Input 2 variables from user
3. Now check the condition If a > b, goto step 4, else goto step 5.
4. Print a is greater, goto step 6
5. Print b is greater
6. Stop
Flowchart
Pseudo code
Pseudo code is a term which is often used in programming and algorithm based fields. It
is a methodology that allows the programmer to represent the implementation of an
algorithm. Simply, we can say that it’s the cooked up representation of an algorithm.
Often at times, algorithms are represented with the help of pseudo codes as they can be
interpreted by programmers no matter what their programming background or knowledge
is. Pseudo code, as the name suggests, is a false code or a representation of code which
can be understood by even a layman with some school level programming knowledge.
Algorithm: It’s an organized logical sequence of the actions or the approach towards a
particular problem. A programmer implements an algorithm to solve a problem.
Algorithms are expressed using natural verbal but somewhat technical annotations.
Pseudo code: It’s simply an implementation of an algorithm in the form of annotations and
informative text written in plain English. It has no syntax like any of the programming
language and thus can’t be compiled or interpreted by the computer.
Advantages of Pseudocode
1) Improves the readability of any approach. It’s one of the best approaches to start implementation of an
algorithm.
2) Acts as a bridge between the program and the algorithm or flowchart. Also works as a rough
documentation, so the program of one developer can be understood easily when a pseudo code is
written out. In industries, the approach of documentation is essential. And that’s where a pseudo-code
proves vital.
3) The main goal of a pseudo code is to explain what exactly each line of a program should do, hence
making the code construction phase easier for the programmer.

How to write a Pseudo-code?


4) Arrange the sequence of tasks and write the pseudocode accordingly.
5) Start with the statement of a pseudo code which establishes the main
goal or the aim.
How to write a Pseudo-code?
 Arrange the sequence of tasks and write the pseudocode accordingly.
 Start with the statement of a pseudo code which establishes the main goal or the aim.
Example: This program will allow the user to check the number whether it's even or odd.
 The way the if-else, for, while loops are indented in a program, indent the statements likewise, as it helps
to comprehend the decision control and execution mechanism. They also improve the readability to a
great extent.
Example:
if "1"
print response
"I am case 1"
if "2"
print response
"I am case 2"
 Use appropriate naming conventions. The human tendency follows the approach to follow
what we see. If a programmer goes through a pseudo code, his approach will be the same as
per it, so the naming must be simple and distinct.
 Use appropriate sentence casings, such as CamelCase for methods, upper case for constants
and lower case for variables.
 Elaborate everything which is going to happen in the actual code. Don’t make the pseudo
code abstract.
 Use standard programming structures such as ‘if-then’, ‘for’, ‘while’, ‘cases’ the way we use
it in programming.
 Check whether all the sections of a pseudo code is complete, finite and clear to understand
and comprehend.
 Don’t write the pseudo code in a complete programmatic manner. It is necessary to be simple
to understand even for a layman or client, hence don’t incorporate too many technical terms.
Difference between Algorithm, Pseudocode and Program
In this post, we will discuss the most common misconception that an
algorithm and a pseudocode is one of the same things. No, they are
not!

Algorithm : Systematic logical approach which is a well-defined, step-


by-step procedure that allows a computer to solve a problem.
Pseudocode : It is a simpler version of a programming code in plain
English which uses short phrases to write code for a program before it
is implemented in a specific programming language.
Program : It is exact code written for problem following all the rules of
the programming language.
Quiz. Draw a flowchart to print the input number 5
times.

You might also like