Module 2
Algorithmic Thinking
Algorithmic Thinking is the process of creating a step-by-step solution to a problem that
can be implemented by a computer.
It focuses on:
• Logical sequence
• Clear instructions
• Correct order of execution
• Efficiency
Characteristics of Algorithmic Thinking
• Logical reasoning
• Clear step-by-step procedures
• Finite number of steps
• Precise instructions
• Deterministic results (same input → same output)
Example
Problem: Find the largest of two numbers.
Steps:
1. Start
2. Read two numbers A and B
3. If A > B, display A
4. Otherwise, display B
5. Stop
This is algorithmic thinking — converting a problem into logical steps.
Algorithm
An Algorithm is a finite sequence of well-defined instructions used to solve a problem.
Properties of an Algorithm
1. Input – Takes zero or more inputs
2. Output – Produces at least one output
3. Definiteness – Steps must be clear and unambiguous
4. Finiteness – Must terminate after finite steps
5. Effectiveness – Each step must be simple and executable
Example: Algorithm to Calculate Factorial of a Number
1. Start
2. Read number N
3. Set FACT = 1
4. For i = 1 to N
5. FACT = FACT × i
6. Display FACT
7. Stop
Flowchart
A Flowchart is a graphical representation of an algorithm using symbols.
It helps visualize:
• Control flow
• Sequence
• Decisions
• Loops
Common Flowchart Symbols
Advantages of Flowcharts
• Easy to understand
• Good for documentation
• Helps in debugging
• Visual clarity
Name Binding
Name Binding is the process of associating a name with a memory location or value.
In programming:
• Variables are names.
• Values are stored in memory.
• Binding connects variable names to memory locations.
Types of Binding
1. Static Binding
• Binding occurs at compile time.
• Example: Fixed variable type in C.
2. Dynamic Binding
• Binding occurs at run time.
• Example: Python variable type changes during execution.
Example
int x = 10;
• x → variable name
• 10 → value
• Memory allocated for x
• Binding connects x to that memory location.
Binding Time
Binding may occur at different times:
1. Language design time
2. Language implementation time
3. Compile time
4. Link time
5. Run time
Selection (Decision Making)
Selection is a control structure that allows a program to choose between different paths
based on a condition.
Types of Selection
1. Simple If
if (condition)
statement;
2. If-Else
if (condition)
statement1;
else
statement2;
3. Nested If
if (condition1)
{
if (condition2)
statement;
}
4. Switch Case (Multiple Selection)
switch(expression)
{
case 1: statement;
break;
case 2: statement;
break;
}
Flow of Selection
1. Condition evaluated
2. If True → execute block
3. If False → skip or execute else
Example: Check Even or Odd
1. Start
2. Read number N
3. If N % 2 == 0
4. Display "Even"
5. Else Display "Odd"
6. Stop
Repetition (Looping)
Repetition allows a set of instructions to be executed multiple times until a condition is met.
Also called Iteration or Looping.
Types of Loops
1. For Loop (Counter Controlled Loop)
Used when number of iterations is known.
for(i=1; i<=10; i++)
{
print(i);
}
2. While Loop (Entry Controlled Loop)
Condition checked before execution.
while(condition)
{
statements;
}
3. Do-While Loop (Exit Controlled Loop)
Condition checked after execution.
do
{
statements;
}
while(condition);
Example: Print Numbers from 1 to 5
Algorithm:
1. Start
2. Set i = 1
3. While i ≤ 5
4. Print i
5. i=i+1
6. Stop
Types of Repetition Based on Condition
1. Counter Controlled Loop – Known number of repetitions
2. Condition Controlled Loop – Based on logical condition
3. Infinite Loop – No terminating condition
Difference Between Selection and Repetition
Selection Repetition
Chooses between alternatives Repeats statements
Uses if, switch Uses for, while
Executes once Executes multiple times
Decision making Looping
Data Organization
Data organization refers to the way data is structured, stored, and accessed in a program to
ensure efficiency and clarity.
Importance of Data Organization
• Improves program readability
• Enhances memory management
• Enables faster data processing
• Supports modular and reusable code
Lists and Arrays
Arrays
An array is a collection of elements of the same data type stored in contiguous memory
locations.
Characteristics:
• Fixed size
• Indexed access
• Efficient for random access
Example:
int marks[5] = {80, 75, 90, 85, 88};
Advantages:
• Fast access using index
• Easy to implement
Limitations:
• Fixed size
• Insertion and deletion are costly
Lists
A list is a dynamic collection of elements that can grow or shrink during program execution.
Types of Lists:
• Linear list
• Linked list (singly, doubly, circular)
Characteristics:
• Dynamic size
• Elements need not be stored contiguously
Example (Linked List Concept):
struct node {
int data;
struct node *next;
};
Advantages:
• Efficient insertion and deletion
• Dynamic memory usage
Limitations:
• Extra memory for pointers
• Slower access compared to arrays
Modularization
Modularization is the process of dividing a large program into smaller, manageable
modules or functions, each performing a specific task.
Features of Modularization
• Each module has single responsibility
• Modules can be tested independently
• Enhances code reuse
Example:
int add(int a, int b) {
return a + b;
}
Benefits
• Reduces complexity
• Simplifies debugging
• Improves teamwork in software development
Problem Solving Techniques
Problem solving in programming involves applying systematic approaches to develop
solutions.
Factoring Technique
Factoring is the process of breaking a problem into smaller sub-problems, solving them
individually, and combining their solutions.
Steps:
1. Identify the main problem
2. Divide into sub-tasks
3. Solve each sub-task
4. Integrate results
Example:
Problem: Calculate total and average marks
Sub-tasks:
• Read marks
• Compute total
• Compute average
• Display result
Recursion Technique
Recursion is a technique where a function calls itself to solve a problem by reducing it into
smaller instances of the same problem.
Key Components:
• Base case: Stops recursion
• Recursive case: Function calls itself
Example:
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
Advantages:
• Simplifies complex problems
• Cleaner and shorter code
Disadvantages:
• Higher memory usage
• Risk of stack overflow if base case is missing
Comparison: Factoring vs Recursion
Aspect Factoring Recursion
Divide problem into
Approach Function calls itself
modules
Usage Program design Algorithm implementation
Control Flow Sequential Repetitive function calls
Example Modular program Factorial, Fibonacci