Comprehensive Study Guide: Algorithms and Flowcharts

Course Overview and Academic Context

  • Course: BSc - IT, Semester-1

  • Chapter: Chapter-1: Algorithms & Flowcharts

  • Instructor: Dr. Sivakumar, Professor / IT

  • Institution: BlueCrest University, Liberia

Fundamentals of Algorithms

  • Definition: An algorithm is defined as a finite set of clear, unambiguous, and ordered steps designed to solve a specific problem.

  • Core Functions:

    • Specifies WHAT actions must be performed.

    • Dictates IN WHAT ORDER those actions must be executed.

  • Representations:

    • Algorithms can be expressed in simple English prose, pseudocode, or other structured formatting styles.

    • Programming languages serve to translate these algorithmic solutions into directly executable machine/computer code.

  • Characteristics of a Good Algorithm:

    • Input: Accepts zero or more clearly defined input values.

    • Output: Produces at least one output result.

    • Definiteness: Every individual step must be clear, precise, and unambiguous in meaning.

    • Finiteness: The algorithm must terminate after executing a finite number of steps.

    • Effectiveness: Every step must be basic enough to be feasibly executed in practice.

    • Correctness: Consistently yields the correct and intended output for valid inputs.

  • Example: Add Two Numbers

    • Problem Statement: Read two numbers and display their sum.

    • Algorithm Steps:

    1. Start

    2. Read AA and BB

    3. Calculate SUM=A+BSUM = A + B

    4. Display SUMSUM

    5. Stop

Fundamentals of Flowcharts

  • Definition: A flowchart is a visual or graphical representation of an algorithm.

  • Core Characteristics and Benefits:

    • Utilizes standardized geometric symbols interconnected by directional flow arrows.

    • Simplifies the visual comprehension, analysis, and discussion of program logic.

    • Serves as a fundamental tool for program planning, instruction, debugging, and comprehensive documentation.

  • Basic Diagrammatic Connection:

    • Illustrates sequential control handoff between phases, such as moving from a terminal Start state to an Input block.      

      Flowchart Start and Input representation
  • Standard Flowchart Symbols:

    • Oval: Represents Terminal points (Start / End / Stop).

    • Rectangle: Represents Process / Calculation operations.

    • Parallelogram: Represents Input / Output operations.

    • Diamond: Represents Decision nodes (evaluating conditional branches).

    • Arrows / Lines: Indicate the direction of execution flow.

Basic Program Control Structures

  • Overview: Complex software systems and algorithms are synthesized by combining three primary structural patterns:

    • Sequence: Instructions are executed sequentially, one after another.

    • Selection: Decision criteria branch execution into alternative paths.

    • Iteration (Repetition): A specific sequence of steps is repeatedly executed as long as a condition is satisfied.

Sequence Control Structure

  • Definition: Execution flows linearly in a continuous line from start to finish without conditional branches or repeating loops.

  • Sequence Example 1: Add Two Numbers and Print Sum

    • Logic Flow:

    • Start

    • Input numbers: a,ba, b

    • Calculate Sum=a+bSum = a + b

    • Display SumSum

    • Stop      

      Flowchart to add two numbers and print the sum
  • Sequence Example 2: Find Percentage of Five Subject Marks

    • Logic Flow:

    • Start (START)

    • Input subject marks: S1,S2,S3,S4,S5S_1, S_2, S_3, S_4, S_5

    • Calculate total marks: Total=S1+S2+S3+S4+S5Total = S_1 + S_2 + S_3 + S_4 + S_5

    • Calculate percentage: Percentage=TotalPercentage = Total % 5

    • Display output: Total MarksTotal\text{ Marks} & PercentagePercentage

    • Stop (STOP)      

      Flowchart to find percentage of five subject marks

Selection Control Structure

  • Definition: Implements choice and conditional logic (such as IF-ELSE structures) where decision nodes direct the flow down distinct operational branches.

  • Selection Example: Determine if Given Number NN is Positive or Negative

    • Logic Flow:

    • Start (START)

    • Input value NN

    • Decision node: Check if N>0N > 0

      • Yes Path: Execute process Positive

      • No Path: Execute process Negative

    • End state (END)      

      Flowchart to check if N is positive or negative

Iteration Control Structure

  • Definition: Continuously repeats a block of instructions while a controlling boolean condition remains true.

  • Common Code Implementations: FOR loops, WHILE loops, and DO-WHILE loops.

  • Iteration Example 1: Print Numbers from 1 to 10

    • Textual Algorithm Steps:

    • Step 1: Start

    • Step 2: Initialize loop variable ii to 11

    • Step 3: Repeat while i≤10i \le 10

      • a. Print the value of ii

      • b. Increment ii by 11 (i=i+1i = i + 1

    • Step 4: End

    • Flowchart Logic:

    • Start (Start)

    • Process node: Initialize counter variable c=1c = 1

    • Decision node: Evaluate is c<11c < 11 ?

      • Yes Path:

      • Output node: Print cc

      • Process node: Increment c=c+1c = c + 1

      • Loop back to decision node is c<11c < 11

      • No Path:

      • Terminal node: Stop      

        Flowchart to print numbers from 1 to 10
  • Iteration Example 2: Print Sum of Numbers from 1 to NN

    • Flowchart Logic:

    • Start (START)

    • Input node: READ NN

    • Initialization process node: SUM=0SUM = 0, COUNT=1COUNT = 1

    • Loop processes:

      • SUM=SUM+COUNTSUM = SUM + COUNT

      • COUNT=COUNT+1COUNT = COUNT + 1

    • Decision node: Evaluate IS COUNT>NCOUNT > N ?

      • NO Path: Re-entry loop back to SUM=SUM+COUNTSUM = SUM + COUNT

      • YES Path: Proceed to output

    • Output node: OUTPUT SUMSUM

    • Terminal node: STOP      W

      Flowchart to print sum of numbers from 1 to N

      Comparative Analysis: Algorithm vs. Flowchart

  • Algorithm:

    • Formatted as plain text.

    • Quick and simple to write initially.

    • Expressed through written statements and numbered steps.

    • Ideal for describing fine-grained, detailed logic.

  • Flowchart:

    • Formatted graphically.

    • Highly effective for visual logic tracking.

    • Expressed through standard geometric symbols and connecting lines.

    • Ideal for gaining a comprehensive overview of program architecture and control flow.

Worked Examples

  • Worked Example 1: Adding Three Numbers

    • Logic Steps:

    • START

    • Input node: READ A,B,CA, B, C

    • Process node: S=A+B+CS = A + B + C

    • Output node: OUTPUT SS

    • STOP      

      Flowchart to add three numbers
  • Worked Example 2: Find the Larger of Two Numbers

    • Logic Steps:

    • START

    • Input node: READ X,YX, Y

    • Decision node: Evaluate IS X>YX > Y ?

      • YES Branch: Output node OUTPUT XX →\rightarrow STOP

      • NO Branch: Output node OUTPUT YY →\rightarrow STOP      

        Flowchart to find larger of two numbers

Classroom Practice and Application Problems

  • Problem 1: Write an algorithm to calculate the area of a rectangle.

  • Problem 2: Draw a flowchart to check whether a given number is positive, negative, or zero.

  • Problem 3: Write an algorithm to find the average of three numbers.

  • Problem 4: Draw a flowchart to display numbers in descending order from 1010 down to 11

  • Challenge Problem: Construct an algorithm and corresponding flowchart to find the largest among three numbers.