Lecture - 9
Course Code: CSC 1101 Course Title: Introduction to computer studies
Dept. of Computer Science
Faculty of Science and Technology
Lecturer No: 09 Week No: 09 Semester:
Jubayer Ahamed [
[email protected]]
Lecture Outline
Pseudo code and algorithm.
Examples
Algorithm:
An algorithm is a step by step procedure to solve a problem.
A procedure is a finite sequence of instructions, where each is carried
out in a finite amount of time.
Every problem can be solved with the help of an algorithm.
For example, when the user wants to login to a Facebook account,
First he has to go to Facebook.com.
Then he has to give the correct username and password.
Then he has to click the login button.
If the username and password are correct, the user can enter his
account.
Likewise, every problem has a sequence of steps to solve it. This is
also an algorithm because it provides a correct sequence of steps to
solve the problem.
4
WHY DO WE NEED TO BUILD ALGORITHMS?
If we wish to build a house, we need to design it first.
Can you think of some possible consequences of not designing
a house before building it?
Similarly, computer programs (especially large and
complex ones) need to be designed before they are
written.
Can you think of some possible consequences of not designing
a program before building it?
One of the things considered when designing a
computer program is the algorithm which it will be
based on.
5
ALGORITHMS IN PROGRAM DESIGN
A computer program is built to solve a certain problem.
Examples:
1. A program to calculate the grade obtained given a mark.
2. A program to convert a Gregorian date to an Islamic
date.
3. A program to produce a document.
6
Steps for solving a program
Below are steps (in fact, an algorithm) for building a program
to solve a particular problem:
Analyze the problem
Design a computer solution to the problem by developing an
algorithm.
Write a computer program based on the algorithm.
Test the program.
Example 1
An algorithm to change a numeric exam result to a letter grade.
Step 1: Start.
Step 2: Input One number.
Step 3: if the number is between 70 and
100 then Set the grade to “A”.
Step 4: if the number is between 50 and
69 then Set the grade to “B”.
Step 5: if the number is between 40 and
59 then Set the grade to “C”.
Step 6: Return Grade.
Step 7: End.
Example 2
An algorithm that finds the average of two numbers
Step 1: Start.
Step 2: Input Two numbers.
Step 3: Add the two numbers.
Step 4: Divide the result by 2.
Step 5: Return the result.
Step 6: End.
9
PSEUDOCODE
Pseudocode is an informal way of writing a program.
It is not exactly a computer program. It represents the
algorithm of the program in natural language and
mathematical notations.
Usually, there is no particular code syntax to write a
pseudocode. Therefore, there is no strict syntax as a usual
programming language. It uses simple English language.
An outline of a program, written in a form that can easily be
converted into real programming statements.
It resembles the actual program that will be implemented
later. However, it cannot be compiled nor executed.
PSEUDOCODE (CONT..)
Pseudocode normally codes the following actions:
Initialization of variables
Assignment of values to the variables
Arithmetic operations
Relational operations
Example
Pseudocode to add 2 numbers is as follows;
SumOfTwoNumbers()
Begin
Set sum=0;
Read: num1, num2;
Set sum = num1+num2;
Print sum;
End
Pseudocode to find the area of a Rectangle is as follows:
Area()
Begin
Read: width, length;
Set area = width * length;
Print area;
End
Classroom Activity
Write an algorithm to find out the odd or even number.
Write an algorithm to find the largest between three
numbers.
Write a Pseudocode to find out the odd or even number.
Write a Pseudocode to find out a Palindrome number.
Difference Between Algorithm and Pseudocode
An algorithm is an unambiguous specification of
how to solve a problem.
Pseudocode is an informal high-level description of
the operating principle of a computer program or
other algorithm.
Usage
An algorithm helps to simplify and understand the problem. On
the other hand, pseudocode is a method of developing an
algorithm.
References
1. https://www.geeksforgeeks.org/how-to-write-a-pseudo-code/
2. https://pediaa.com/difference-between-algorithm-and-pseudocode/
3. https://blog.usejournal.com/how-to-write-pseudocode-a-beginners-guide-2995
6242698