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

Lect 2 Unit 1

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

Lect 2 Unit 1

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

Dr.

Sudhanshu Kumar Jha


Assistant Professor (Computer Science & Engineering)

Dept. of Electronics & Communication


University of Allahabad, Prayagraj – 211 002
Email: [email protected]
Mob: 9470311972
ALGORITHMS AND FLOWCHARTS

 A typical programming task can be divided into two phases:


 Problem solving phase
 produce an ordered sequence of steps that describe solution of problem
 this sequence of steps is called an algorithm
 Implementation phase
 implement the program in some programming language
Steps in Problem Solving

 First produce a general algorithm (one can use pseudocode)


 Refine the algorithm successively to get step by step detailed
algorithm that is very close to a computer language.
 Pseudocode is an artificial and informal language that helps
programmers develop algorithms. Pseudocode is very similar
to everyday English.
Pseudocode & Algorithm

 Example 1: Write an algorithm to determine a student’s final


grade and indicate whether it is passing or failing. The final
grade is calculated as the average of four marks.
Pseudocode & Algorithm

Pseudocode:
 Input a set of 4 marks
 Calculate their average by summing and dividing by 4
 if average is below 50
Print “FAIL”
else
Print “PASS”
Pseudocode & Algorithm

 Detailed Algorithm
 Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
The Flowchart

 (Dictionary) A schematic representation of a sequence of operations, as


in a manufacturing process or computer program.
 (Technical) A graphical representation of the sequence of operations in
an information system or program.
 Information system flowcharts show how data flows from source documents
through the computer to final distribution to users.
 Program flowcharts show the sequence of instructions in a single program or
subroutine. Different symbols are used to draw each type of flowchart.
The Flowchart

A Flowchart
 shows logic of an algorithm
 emphasizes individual steps and their interconnections
 e.g. control flow from one action to the next
Basic Flowchart Symbols
Name Symbol Use in Flowchart

Oval Denotes the beginning or end of the program

Parallelogram Denotes an input operation

Rectangle Denotes a process to be carried out


e.g. addition, subtraction, division etc.

Diamond Denotes a decision (or branch) to be made.


The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)

Hybrid Denotes an output operation

Flow line Denotes the direction of logic flow in the program


Example

START
Step 1: Input M1,M2,M3,M4
Input Step 2: GRADE  (M1+M2+M3+M4)/4
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
GRADE(M1+M2+M3+M4)/4
else
Print “PASS”
N IS Y endif
GRADE<50

PRINT PRINT
“PASS” “FAIL”

STOP
Example 2

 Write an algorithm and draw a flowchart to convert the length in feet to centimeter.
Pseudocode:
 Input the length in feet (Lft)
 Calculate the length in cm (Lcm) by multiplying LFT with 30
 Print length in cm (LCM)
Example 2

Algorithm Flowchart
 Step 1: Input Lft
START
 Step 2: Lcm  Lft x 30
Input
 Step 3: Print Lcm Lft

Lcm  Lft x 30

Print
Lcm

STOP
Example 3

Write an algorithm and draw a flowchart that will read the


two sides of a rectangle and calculate its area.
Pseudocode
 Input the width (W) and Length (L) of a rectangle
 Calculate the area (A) by multiplying L with W
 Print A
Example 3

Algorithm
START
 Step 1: Input W,L
Input
 Step 2: AL x W W, L

 Step 3: Print A
ALxW

Print
A

STOP
Flowcharts

 Flowcharts is a graph used to depict or show a step by step solution using symbols
which represent a task.

 The symbols used consist of geometrical shapes that are connected by flow lines.

 It is an alternative to pseudocoding; whereas a pseudocode description is verbal, a


flowchart is graphical in nature.
Flowchart Symbols

Terminal symbol - indicates the beginning and


end points of an algorithm.

Process symbol - shows an instruction other than


input, output or selection.

Input-output symbol - shows an input or an output


operation.

Disk storage I/O symbol - indicates input from or output to disk storage.

Printer output symbol - shows hardcopy printer


output.
Flowchart Symbols cont…

Selection symbol - shows a selection process


for two-way selection.

Off-page connector - provides continuation of a logical path on another


page.

On-page connector - provides continuation


of logical path at another point in the same
page.

Flow lines - indicate the logical sequence of


execution steps in the algorithm.
Typical Programming Environment

 Consists of three type of control structure


 Sequence Control Structure
 Selection control structure
 Repetition control structure
Flowchart – sequence control structure

Statement 1

Statement 2

Statement 3

:
Flowchart – selection control structure

No Yes
Condition

else- then-
statement(s) statement(s)
Flowchart – repetition control structure

yes Loop
Condition
Statement(s)

no
Flowchart – example 1

Begin

Read birth date

Calculate
Age = current year – birth date

Display
age

End
Flowchart – example 2

Begin

Read age

YES Age > 55? NO

print “Old Age” print “Still Yong”

End
Flowchart – example 5
To find the sum of first 10 natural numbers

Begin

sum = 0
current_number = 1

NO
current_number <= 10?
print sum

YES
End
sum = sum + current_number
current_number = current_number + 1
Example 4
 Write an algorithm and draw a flowchart that will calculate the
roots of a quadratic equation
ax 2  bx  c  0
 Hint: d = sqrt ( b 2  4ac), and the roots are: x1 = (–b + d)/2a
and x2 = (–b – d)/2a
Exercises: Algorithm & Flowchart

1.) Create an algorithm and a flowchart that will accept/read


two numbers and then display the bigger number.
Exercises: Algorithm & Flowchart

2.) Create an algorithm and a flowchart that will compute the area of
a circle.
Exercises: Algorithm & Flowchart

3.) Create an algorithm and a flowchart that will compute the sum of
two numbers. If the sum is below or equal to twenty, two numbers
will be entered again. If the sum is above 20, it will display the sum.
Activity: Algorithm & Flowchart

4) Create an algorithm and a flowchart that will output the largest


number among the three numbers.
Assignment 1
1.Create an algorithm and a flowchart that will output
for g.c.d.
2.Create an algorithm and a flowchart that will output
the factorial of a given number.
3.Create an algorithm and a flowchart that will output
the Fibonacci series up to a given number.
4.Create an algorithm and a flowchart that will output
all the prime numbers between given 2 numbers.
Program
Set of instructions that can be executed by a computer to perform a specific task.
Finding Sum of n numbers

Flowchart
Number System

A number system is defined as the representation of numbers by using digits or other symbols in a
consistent manner.

The value of any digit in a number can be determined by a digit, its position in the number, and the
base of the number system.

The numbers are represented in a unique manner and allow us to operate arithmetic operations
like addition, subtraction, and division.
In a positional base b numeral system (with b a natural number greater than 1 known as
the radix), b basic symbols (or digits) corresponding to the first b natural numbers including zero
are used. To generate the rest of the numerals, the position of the symbol in the figure is used. The
symbol in the last position has its own value, and as it moves to the left its value is multiplied
by b.

For example, in the decimal system (base 10), the numeral 4327 means
(4×103) + (3×102) + (2×101) + (7×100), noting that 100 = 1.
In general, if b is the base, one writes a number in the numeral system of base b by expressing it in
the form anbn + an − 1bn − 1 + an − 2bn − 2 + ... + a0b0 and writing the enumerated digits anan − 1an −

2 ... a0 in descending order. The digits are natural numbers between 0 and b − 1, inclusive.
Types of Number System
Binary Number System (Base 2)

A computer can understand only the “on” and “off” state of a switch.

These two states are represented by 1 and 0.

The combination of 1 and 0 form binary numbers. These numbers represent various data.

Binary numbers
All computers work with 0’s and 1’s so it is like learning alphabets before learning English
Binary to Decimal conversion

 Take each digit in the binary number and multiply it with its place
value.
 The sum of all the above products will give the equivalent decimal
number.

Ex :- (1011)2 to decimal number.


Sol :- (1011)2 = (1 x 23) + ( 0 x 22) + (1 x 21) + (1 x 20 )
= (1 x 8) + ( 0 x 4) + (1 x 2) + (1 x 1)
= (11)10
Ex :- Convert the binary number (0.1101)2 into decimal number.
1 1 1 1
(0.1101)2 = (1 x ) + ( 1 x ) + (0 x ) + (1 x )
2 4 8 16
= 1 x 0.5 + 1 x 0.25 + 0 x 0.125 +1 x 0.0625
= 0.5 +0.25 + 0 + 0.0625
= (0.8125)10
Fractional Binary number to Decimal number

For the fraction (after the decimal point) the place value starts
with negative power of 2. This negative power value increases from
left to right.
The place value of the first left digit in fraction is (2–1)
The place value of the next right digit in fraction is (2–2)
The place value of the next right digit in fraction is (2–3)
……. so on
Ex :- Convert the binary number (1010.0101)2 into decimal number.
Sol :- Integer part (1010)2 = (1 x 23) + ( 0 x 22) + (1 x 21) + (0 x 20 )
= (1 x 8) + ( 0 x 4) + (1 x 2) + (0 x 1 )
= (10)10
Fractional part (0.0101)2 = (0 x 2–1) + ( 1 x 2–2) + (0 x 2–3) + (1 x 2–4)
1 1 1 1
= (0 x ) + ( 1 x ) + (0 x ) + (1 x )
2 4 8 16
= 0 x 0.5 + 1 x 0.25 + 0 x 0.125 +1 x 0.0625
= 0 + 0.25 + 0 + 0.0625
= (0.3125)10
Decimal to Binary conversion

The decimal number is converted in to binary number by using successive divisions


method.
1. The decimal number (i.e. dividend) is divided by 2 (i.e. divisor).
2. If „1‟ or “0” occurs as remainder, transfer that „1‟ or “0” to the binary record.
3. Now take quotient as dividend and divide it by 2 and transfer the remainder to the
binary record.
4. The same procedure is continued until the quotient becomes zero
5. The last remainder is taken as most significant bit (MSB).
6. The first remainder is taken as least significant bit (LSB).
7. The equivalent binary number comprises with all the remainders in successive
divisions method in the order from MSB (bottom) to LSB (top).
5610=1110002

(56)10=(111000)2
Fractional Decimal number to binary

The fractional decimal number is converted in to binary number by using successive


fraction multiplications method.
1. The fractional decimal number is multiplied with 2 by successive fraction multiplications
method.
2. If „1‟ or „0‟ occurs in units place in the product, transfer that „1‟ or „0‟ to the binary record.
3. The multiplication is continued with the remaining fraction.
4. The same procedure is followed in each multiplication.
5. The first transferred number (1 or 0) to binary record is taken as most significant bit (MSB).
6. The last transferred number (1 or 0) to binary record is taken as least significant bit (LSB).
7. If the multiplication does not end, it can be stopped at any of our desired level.
General Concept for Number Conversion
Octal Number System (Base 8)

The octal number system uses eight digits: 0,1,2,3,4,5,6 and 7 with the base of 8.

Digits like 8 and 9 are not included in the octal number system.

Just as the binary, the octal number system is used in minicomputers but with digits from 0 to 7. For
example: 358, 238, 1418 are some examples of numbers in the octal number system.
Decimal Number System and Octal Number System

This system uses digits 0 to 7 (i.e. 8 digits) to represent a number and the numbers are as
a base of 8.

Conversion of Decimal Number System Octal Number System

Step 1: Identify the base of the required number. Since we have to convert the given number into
the octal system, the base of the required number is 8.

Step 2: Divide the given number by the base of the required number and note down the quotient
and the remainder in the quotient-remainder form. Repeat this process (dividing the quotient again
by the base) until we get the quotient less than the base.

Step 3: The given number in the octal number system is obtained just by reading all the
remainders and the last quotient from bottom to top.
Convert 432010 into the octal system.

432010 = 103408
Octal to Decimal
Hexadecimal Number System (Base 16)

16 digits used to represent a given number. Thus it is also known as the base 16 number system.

Each digit position represents a power of 16.

As the base is greater than 10, the number system is supplemented by letters.

Following are the hexadecimal symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

To take A, B, C, D, E, and F as part of the number system is conventional and has no logical or
deductive reason.
Example 2: Convert 5BC16 into the decimal system.

5BC16 is in the hexadecimal system. We know that B=11 and C= 12 in the hexadecimal system. So
we get the equivalent number in the decimal system using the following process:

5BC16 = 146810
Convert hexadecimal number 1F.01B into decimal number.

Since value of Symbols: B and F are 11 and 15 respectively. Therefore equivalent decimal number is,

= (1F.01B) 16

= (1x161+15x160 +0x16-1+1x16-2+11x16-3) 10

= (31.0065918) which is answer. 10


Number System Chart

Name Base Symbols Example

Decimal 10 0,1,2,3,4,5,6,7,8,9 (2795)10

Binary 2 0,1 111000010

Octal 8 0,1,2,3,4,5,6,7 (1576)8

Hexadecimal 16 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F 3DB


One Number System to Another

We already had discussed


Decimal to Binary and vice-versa
Decimal to Octal and vice-versa
Decimal to Hexadecimal and vice-versa

Now have to look for following


Any number system to any another such as

Binary to Octal and vice-versa


Binary to Hexadecimal and vice-versa
Octal to Hexadecimal and vice-versa
Thumb Rule

Given Number System  Decimal Number System  Desired Number System


Very Easy

Octal to binary Binary to octal

 (10 101)2 = (010 101)2 = (25)8


Octal Binary Decimal

27 010 111 23
 (10111.1) = (010 111 . 100) = (2 7 . 4)8
135 001 011 101 93

45.5 100 101.101 37.625

You might also like