0% found this document useful (0 votes)
31 views32 pages

1.5 Algorithms

The document provides an introduction to algorithms and data structures. It discusses algorithm representation using flowcharts and pseudocode. Key points covered include: - Algorithms are step-by-step solutions to problems and must be unambiguous, finite, and produce the desired output. - Flowcharts use standardized symbols to visually represent algorithm logic and instruction order. Pseudocode resembles programming instructions but uses plain language. - Programs can be structured using sequence, selection, and iteration logic. Common structures include IF/THEN/ELSE, WHILE, FOR. - Examples demonstrate algorithms for basic math problems and grading student exam results represented as pseudocode and flowcharts. Assignments are provided to practice additional algorithm

Uploaded by

Aprajita Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views32 pages

1.5 Algorithms

The document provides an introduction to algorithms and data structures. It discusses algorithm representation using flowcharts and pseudocode. Key points covered include: - Algorithms are step-by-step solutions to problems and must be unambiguous, finite, and produce the desired output. - Flowcharts use standardized symbols to visually represent algorithm logic and instruction order. Pseudocode resembles programming instructions but uses plain language. - Programs can be structured using sequence, selection, and iteration logic. Common structures include IF/THEN/ELSE, WHILE, FOR. - Examples demonstrate algorithms for basic math problems and grading student exam results represented as pseudocode and flowcharts. Assignments are provided to practice additional algorithm

Uploaded by

Aprajita Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

CS1101

Introduction to Computer Programming and


Data Structures
Dr. B Ramachandra Reddy
Outline

• Algorithm
• Flow Charts
• Pseudo Code
Program Planning

 To write a correct program, a programmer must write each and every


instruction in the correct sequence
 Logic (instruction sequence) of a program can be very complex
 Hence, programs must be planned before they are written to ensure
program instructions are:
 Appropriate for the problem
 In the correct sequence
Algorithm

 Refers to the logic of a program and a step-by-step description of how to


arrive at the solution of a given problem.
 In order to qualify as an algorithm, a sequence of instructions must
have following characteristics:
 Each and every instruction should be precise and unambiguous
 Each instruction should be such that it can be performed in a finite time
 One or more instructions should not be repeated infinitely. This ensures that the
algorithm will ultimately terminate
 After performing the instructions, that is after the algorithm terminates, the
desired results must be obtained
Representation of Algorithms

 As flowcharts
 As pseudocodes
Flowchart

 Flowchart is a pictorial representation of an algorithm


 Uses symbols (boxes of different shapes) that have
standardized meanings to denote different types of
instructions
 Actual instructions are written within the boxes
 Boxes are connected by solid lines having arrow marks
to indicate the exact sequence in which the instructions
are to be executed
 Process of drawing a flowchart for an algorithm is called
flowcharting
Basic Flowchart Symbols
Examples of Decision Symbol

No A<B Compare A>B


Is I = 10?
A&B

Yes A=B

(a) A two-way branch decision. (b) A three-way branch decision.


Examples of Decision Symbol

I=?

=0 =1 =2 =3 =4 =5 = Other

(c) A multiple-way branch decision.


Flowchart examples

To check the given number is odd or


even
Sample Flowchart
Pseudocode

 A program planning tool where program logic is written in


an ordinary natural language using a structure that
resembles computer instructions

 “ Pseudo” means imitation or false and “ Code” refers to


the instructions written in a programming language.
Hence, pseudocode is an imitation of actual computer
instructions

 Because it emphasizes the design of the program,


pseudocode is also called Program Design Language
(PDL)
Basic Logic (Control) Structures

• Any program logic can be expressed by using only


following three simple logic structures:

1. Sequence logic,
2. Selection logic, and
3. Iteration (or looping) logic

• Programs structured by using only these three logic


structures are called structured programs, and the
technique of writing such programs is known as structured
programming
Pseudocode
• read/get – indicates a user will be inputting something
• display/print/show – indicates that an output will appear on the screen
• SET, INIT: To initialize values
• Expressions
• ← Assignment (like = in C)
• = Equality testing (like = = in C)
• n2 superscripts and other mathematical formatting allowed
• Method call
• var.method (arg [, arg…])
• Return value
• return expression
Sequence Logic

Process 1

Process 1
Process 2
Process 2

(a) Flowchart (b) Pseudocode


Algorithm- add two numbers
Algorithm- area of circle
Selection Logic

• Also known as decision logic, it is used for making decisions

• Three popularly used selection logic structures are


1. IF…THEN…ELSE
2. IF…THEN
3. CASE
Selection Logic (IF…THEN…ELSE Structure)

Yes No IF Condition THEN


IF (condition)
Process 1

THEN ELSE ELSE


Process 2
Process 1 Process 2
ENDIF

(a) Flowchart (b) Pseudocode


Selection Logic (IF…THEN Structure)

Yes No
IF (condition)
IF Condition THEN

THEN Process 1
Process 1 ENDIF

(a) Flowchart (b) Pseudocode


Sample Algorithm
Algorithm- Grade
Selection Logic (CASE Structure)

Yes
Type 1 Process 1

No
Yes
Type 2 Process 2 CASE Type

Case Type 1: Process 1


No
Case Type 2: Process 2

Yes
Type n Process n
Case Type n: Process n
No
ENDCASE

(a) Flowchart
(b) Pseudocode
Iteration (or Looping) Logic

 Used to produce loops in program logic when one or more instructions may
be executed several times depending on some conditions

 Two popularly used iteration logic structures are

1. DO…WHILE
2. REPEAT…UNTIL
Iteration (or Looping) Logic
(DO…WHILE Structure)

False
Condition?

True
Process 1 DO WHILE Condition
Process 1
Block
Process n

Process n ENDDO

(a) Flowchart (b) Pseudocode


Iteration (or Looping) Logic
(REPEAT…UNTIL Structure)

Process 1

REPEAT
Process 1
Process n

Process n
False
Condition? UNTIL Condition

True
(a) Flowchart (b) Pseudocode
Sample Algorithm (Example 1)

• There are 50 students in a class who appeared in their


final examination. Their mark sheets have been given to
you.

• The division column of the mark sheet contains the


division (FIRST, SECOND, THIRD or FAIL) obtained by the
student.

• Write an algorithm to calculate and print the total


number of students who passed in FIRST division.
Sample Algorithm (Example 1)
Step 1: Initialize Total_First_Division and
Total_Marksheets_Checked to zero.

Step 2: Take the mark sheet of the next student.

Step 3: Check the division column of the mark sheet to see if it is


FIRST, if no, go to Step 5.

Step 4: Add 1 to Total_First_Division.

Step 5: Add 1 to Total_Marksheets_Checked.

Step 6: Is Total_Marksheets_Checked = 50, if no, go to Step 2.

Step 7: Print Total_First_Division.

Step 8: Stop.
Flowchart Vs Pseudo code
Flowchart Pseudocode

Start/End Begin/End

Input/output Read: read x,y


Print: Print y
Process Computation: X<-y+6

Decision If/if-lse/case/while/
do-while/for
Connector Not Applicable

Predefined Process Not Applicable


Flowchart Vs Pseudo code

• Flowchart Advantages • Pseudocode Advantages


• Standard • Easily modified
• Visual • Implements structured concepts
• Done easy on Word Processor
• Flowchart Disadvantages • Pseudocode Disadvantages
• Hard to modify • Not Visual
• Structured design elements not • Not accepted standard, varies
implemented from company to company
• Special software required
Assignments

• Write a pseudo code and flowchart to find the factorial of the given
number
• Write a pseudo code and flowchart to find the given number is prime
or not?
• Write a pseudo code and flowchart to generate the Fibonacci series
upto n numbers.
• Write a pseudo code and flowchart to check the given number is
palindrome or not?

You might also like