CT-2 Notes
Name:_________________
Subject: Computer Science
Algorithm and Data
What is an Algorithm | Introduction to Algorithms
An algorithm is a step-by-step procedure or set of rules designed to solve a specific
problem or perform a specific task.
It takes some input, processes it through a sequence of well-defined instructions, and
produces an output.
Example:
To add two numbers:
1. Start
2. Input number A
3. Input number B
4. Add A and B → Store result in C
5. Output C
6. End
This is a simple algorithm for addition.
What is Data?
Data refers to raw facts or information that algorithms use as input or produce as output.
Example: Numbers like 5, 10, or a list like [3, 8, 1, 7] are data. In real-world terms, it could
be customer names, sales numbers, temperature readings, etc.
Relationship Between Algorithm and Data
Algorithm Data
Tells how to do
Tells what to do it on
something
A set of The input/output of those
rules/instructions rules
Is processed by
Operates on data
algorithms
Analogy:
Think of an algorithm as a machine and data as the fuel. The machine (algorithm) processes
the fuel (data) to give you a result.
"An algorithm is like a machine, and data is the fuel."
Machine = Algorithm (it does the work)
Fuel = Data (it powers the process)
This analogy helps you understand how algorithms and data work together.
Predicting the Outcome of an Algorithm
Predicting the outcome of an algorithm means figuring out what result or output the
algorithm will produce when given a specific input.
Why It's Important:
Helps in debugging and understanding code.
Essential for testing and analyzing algorithms.
Key in exams and interviews when you're asked, "What will this algorithm return?"
General Steps to Predict the Outcome:
1. Understand the Algorithm
Know what task it's supposed to do (e.g., sort, search, add).
2. Follow the Steps
Apply the algorithm to the given input, step by step.
3. Track Values
Use a table or list to keep track of changing variables or data.
4. Determine the Output
Look at the result after the algorithm finishes processing.
5. Example Algorithm: Find Greater of Two Numbers
Algorithm Greater Number
Input: Two numbers A and B
Output: The greater number
1. If A > B:
Return A
2. Else:
Return B
Example 1:
Input: A = 15, B = 10
Step: 15 > 10 → return 15
Output: 15
Example 2:
Input: A = 7, B = 20
Step: 7 > 20 → false → return 20
Output: 20
Example 3 (Equal numbers):
Input: A = 12, B = 12
Step: 12 > 12 → false → return 12
Output: 12 (In this version, if both are equal, it returns B)
To predict outcomes correctly:
Know basic logic and flow control .
Practice with different inputs.
Simulate the steps as if you're the computer.
What is a Flowchart?
A flowchart is a visual representation of a process, algorithm, or system using
symbols and arrows.
It shows the logical flow of steps in a process from start to end.
It helps in planning, designing, analyzing, and communicating how a program or
system works.
Why Use Flowcharts?
Uses:
Design algorithms
Document business processes
Explain program logic
Debug logical errors
Teach programming or process design
Key Flowchart Symbols (Expanded)
Symbol Name Function
Represents the beginning or end of
Oval Start/End
a flow
Parallelogram Input/Output Shows data input or output
Rectangle Process Indicates an action or operation
Diamond Decision A condition that branches (Yes/No)
Arrow Flowline Shows direction of process flow
Rules for Making Flowcharts
Start and End with an oval symbol.
Keep flow top to bottom or left to right.
Use arrows to show the correct direction.
Only one entry and exit point per symbol.
Label decision branches (Yes/No or True/False).
Keep it clear and simple — avoid too much clutter.
Use meaningful names for processes or variables.
Advantages of Flowcharts
✔ Makes logic easy to understand
✔ Useful for problem-solving
✔ Helps in debugging
✔ Aids in documentation
✔ Useful for team communication
Limitations of Flowcharts
❗ Can become complex for large problems
❗ Not suitable for minor changes (redrawing needed)
❗ Takes time to draw and update
Real-life Use Cases
ATM withdrawal process
Online shopping checkout
Login validation system
Traffic light control system
Calculator logic
Example-1 Flowchart for adding two numbers
Example 2 Flowchart for Greater in two numbers
What Are Conditional Statements?
Conditional statements allow a computer program to make decisions.
They tell the computer:
🔹 “If something is true, do this.”
🔹 “Otherwise, do something else.”
They help the program choose different actions depending on different inputs or
situations.
📚 Real-Life Example:
If it’s raining, then take an umbrella.
Else, wear sunglasses.
🔠 Common Conditional Statement Keywords (in pseudocode):
IF
THEN
ELSE
ELSE IF (or ELIF in Python)
END IF
🧠 Basic Forms of Conditional Statements
1. Simple IF Statement
Used to run code only if a condition is true.
Pseudocode Example:
INPUT number
IF number > 0 THEN
OUTPUT "Number is positive"
END IF
Explanation: If the user enters a number greater than 0, it prints “Number is positive.”
2. IF-ELSE Statement
Used to choose between two options.
Pseudocode Example:
INPUT age
IF age >= 18 THEN
OUTPUT "You are an adult"
ELSE
OUTPUT “You are Minor”
END IF
Explanation: If age is 18 or more, it says "adult". Otherwise, it says "minor".
3. IF - ELSE IF - ELSE Statement
Used to check multiple conditions in order.
Pseudocode Example:
INPUT marks
IF marks >= 90 THEN
OUTPUT "Grade A"
ELSE IF marks >= 75 THEN
OUTPUT "Grade B"
ELSE
OUTPUT "Grade C"
END IF
Explanation:
If marks ≥ 90 → Grade A
If marks ≥ 75 but < 90 → Grade B
Otherwise → Grade C
✅ Summary Table
Type Use
Run something only if condition
IF
is true
IF...ELSE Choose between two options
IF...ELSE Choose between multiple
Type Use
IF...ELSE options