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

1.Basics of algorithms-flowchart

The document outlines prerequisites for understanding algorithms, pseudocode, flowcharts, and control structures, emphasizing their importance in problem-solving and communication. It provides examples of algorithms for various tasks, such as determining even or odd numbers, leap years, and sorting. Additionally, it includes review questions to reinforce learning on algorithms and their applications.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

1.Basics of algorithms-flowchart

The document outlines prerequisites for understanding algorithms, pseudocode, flowcharts, and control structures, emphasizing their importance in problem-solving and communication. It provides examples of algorithms for various tasks, such as determining even or odd numbers, leap years, and sorting. Additionally, it includes review questions to reinforce learning on algorithms and their applications.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Prerequisite

Dr. Amrita Jyoti


Copyright © 2021, ABES Engineering College
References

• “Fundamentals of data structure in C” Horowitz, Sahani &


Freed, Computer Science.
• “Fundamental of Data Structure” ( Schaums Series)
• Robert Kruse, Data Structures and Program Design , Prentice
Hall, 1984

2
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Session Plan
The prerequisite will cover following topics:-
• What is an algorithm?

• What pseudocode?

• What is flowchart?

• Different control structure

3
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Algorithm
Representation of a solution to any problem is termed as an
algorithm.
It is not written specific to any Programming Language.
Algorithms can be written in the form of:
• Flow chart (pictorial representation)

• In own words - Step by step instructions

• Pseudocode.

4
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Flowchart
• It is a graphical representation of a procedure or algorithm
• It illustrates a complex process into a simple diagrams using a flowchart

5
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Why do we need a flowchart?

• Effective documentation

• Clarity of the process

• Improved problem solving

• Effective communication and co-ordination

• Analysis and improvement

6
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Why do we need a flowchart?

• Effective documentation

• Clarity of the process

• Improved problem solving

• Effective communication and co-ordination

• Analysis and improvement

7
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Why do we need a flowchart?

• Effective documentation

• Clarity of the process

• Improved problem solving

• Effective communication and co-ordination

• Analysis and improvement

8
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Why do we need a flowchart?

• Effective documentation

• Clarity of the process

• Improved problem solving

• Effective communication and co-ordination

• Analysis and improvement

9
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Symbols used in flowchart

10
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Pseudo Code Writing: Industry Pattern

• While writing conditional and loop blocks, no braces are applied.


• Proper indentation is provided to make algorithm readable.
• All operators are same as that in C.
11
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Rule for writing various syntax

12
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Writing Conditional Statements

• IF is written followed by
condition followed by THEN.
• In case of false condition ELSE
can be written.

13
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Writing Loops

Note: Assume that loop counter increments by one after each


execution.

14
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Writing Loops( continued…)

15
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Writing Loops( continued…)

16
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Examples: To Print even or odd
ALGORITHM EvenOrOdd()
INPUT: An integer Number
OUTPUT: Nature of Number, i.e., Even or Odd
BEGIN:
READ(N)
IF N%2 = = 0 THEN
WRITE(“EVEN NUMBER”);
ELSE
WRITE(“ODD NUMBER”);
END

17
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Examples: Algorithm to find leap year
ALGORITHM LeapOrNonLeap(year)
INPUT: An year
OUTPUT: Nature of year, i.e., Leap or Non-Leap
BEGIN:
IF year%4 = = 0 AND year%100!=0 OR year%400 = = 0 THEN
WRITE(“Leap year”)
ELSE
WRITE(“Non Leap Year”)
END;

18
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Examples: Algorithm for printing table
ALGORITHM TableOfNumber()
INPUT: An Integer Number
OUTPUT: Table of Input Number BEGIN:
READ (N)
FOR i=1 TO 10 DO
WRITE (N*i)
END;
19
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Examples: Algorithm to find reverse
ALGORITHM NumberReverse()
INPUT: An Integer Number
OUTPUT: Reverse of Input Number
BEGIN:
READ (N)
Rev=0
WHILE N>0 DO
Remainder=N%10
Rev = Rev*10+Remainder
N = N/10
WRITE (“Reversed Number is”)
WRITE (Rev)
END; 20
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Example: Algorithm to find Prime
ALGORITHM PrimeNumber()
INPUT: A range of Numbers
OUTPUT: All Prime Numbers
BEGIN:
READ (LowerLimit)
READ (UpperLimit)
FOR i = LowerLimit TO UpperLimit Do
FOR j = 2 TO SQRT(i) DO
IF i%j = = 0 THEN
BREAK;
IF J=SQRT(i)+1 THEN
WRITE(i)
END; 21
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Example: Algorithm to swap two numbers

ALGORITHM Exchange (a, b)


INPUT: Two Numbers
OUTPUT: Swapped Values of Numbers
BEGIN:
t=a
a=b
b=t
END;

22
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Example: Bubble Sort
ALGORITHM BubbleSort (A[ ], N)
INPUT: A set of N Numbers
OUTPUT: Sorted Numbers
BEGIN:
FOR i =1 TO n–1
FOR j=0 TO n-i–1 DO
IF A[j]>A[j+1] THEN
CALL Exchange(A[j], A[j+1])
END;

23
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Example: To find factorial
ALGORITHM Factorial (N)
INPUT: An Integer Number
OUTPUT: Factorial Value of the Number
BEGIN:
IF N = 0 THEN
RETURN 1
ELSE
RETURN N*CALL Factorial(N–1)
END;

24
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Example: To nth Fibonacci number
ALGORITHM FibonacciNumber (N)
INPUT: A integer Numbers
OUTPUT: Nth Fibonacci Term
BEGIN:
IF N = = 1 THEN
RETURN 0
ELSE
IF N = = 2 THEN
RETURN 1
ELSE RETURN CALL Fibonacci(N–1) +
CALLFibonacci(N–2)
END 25
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Example: Binary Search
ALGORITHM BinarySearch (A[ ], N, SearchKey)
INPUT: An Array of Size N and a search key
OUTPUT: Location of Search key in the given Array
BEGIN:
Low = 0
High = N–1
WHILE Low<= High
Mid = (Low+High)/2
IF A[Mid] = = SearchKey THEN
RETURN Mid
ELSE
IF SearchKey<A[Mid] THEN
High = Mid–1
ELSE
Low = Mid+1
RETURN –1
END; 26
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College
Review Questions

1. What is an algorithm? Differentiate between an algorithm and


a pseudo code.
2. Write an algorithm for the following problems:
To find the factorial of a number.
To find the sum of digits.
To find a number in an array by linear search.

3. Differentiate between a for-loop, while-loop, and a do-while


loop.

27
Dr. Amrita Jyoti
Copyright © 2021, ABES Engineering College

You might also like