[go: up one dir, main page]

100% found this document useful (1 vote)
877 views21 pages

EOC - Unit 3

Uploaded by

srinivas001srinu
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
100% found this document useful (1 vote)
877 views21 pages

EOC - Unit 3

Uploaded by

srinivas001srinu
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

⚠️

CS25C03- Essentials of
Computing
Unit - 3 Problem Solving Basics
3.1 Problem Definition

Introduction:
In computing, problem solving is the process of designing solutions for
real-world problems using computers.

The first and most important step = Problem Definition.

If the problem is not defined correctly → the solution will fail (just like
wrong diagnosis → wrong treatment).

Definition:
Problem Definition is the process of clearly understanding, analyzing, and
stating the problem to be solved, including its objectives, inputs, processes,
and expected outputs.

Key Elements of Problem Definition:

CS25C03- Essentials of Computing 1


1. Identify the Problem

What exactly needs to be solved?

Example: “Students are not able to check their marks online.”

2. Define the Objective

What should the solution achieve?

Example: “Develop a web system to show student marks.”

3. Specify Inputs

What data is needed to solve the problem?

Example: “Student ID, Subject marks.”

4. Specify Outputs

What results should come out?

Example: “Display marks, calculate total and grade.”

5. Constraints

Any limits on time, resources, or technology.

Example: “System must work on mobile.”

6. Success Criteria

How will we know the problem is solved?

Example: “If students can log in and see their marks without errors.”

Example:
Problem: College library books are often misplaced.

Objective: Create a computer system to track books.

Input: Book ID, Student ID.

Process: Update issue/return records.

Output: Show availability of books.

Constraint: Must handle 1000+ books.

CS25C03- Essentials of Computing 2


Success: Students always find accurate book status.

3.2 Logical Reasoning

Introduction :
Computers follow logic to make decisions.

Logical Reasoning means applying systematic rules of logic to analyze a


problem and arrive at a valid conclusion.

It is a core part of problem solving, programming, and algorithm design.

Definition :
Logical Reasoning is the process of using structured, rule-based thinking to
evaluate situations, identify relationships, and draw conclusions.

Types of Logical Reasoning :


1. Deductive Reasoning

Start with a general rule and apply it to specific cases.

Example:

Rule: All humans are mortal.

Case: Socrates is a human.

Conclusion: Socrates is mortal.

2. Inductive Reasoning

Draw a general conclusion from specific observations.

Example:

Case: The sun rose in the east today, yesterday, and every day
before.

Conclusion: The sun always rises in the east.

3. Abductive Reasoning

Choosing the most likely explanation from incomplete data.

CS25C03- Essentials of Computing 3


Example:

Case: Ground is wet.

Conclusion: It probably rained.

Logical Reasoning in Problem Solving:


Used in Algorithms:

Example: If number is divisible by 2 → print EVEN, else ODD.

Used in Programming:

Example:

if marks >= 50:


print("Pass")
else:
print("Fail")

Used in Daily Life:

Example: If bus is late, then take auto; otherwise, take bus

3.3 Decomposition

Introduction :
Some problems are too complex to solve in one step.

Computers (and humans) solve such problems by breaking them into


smaller, manageable subproblems.

This process is called Decomposition.

Definition :
Decomposition is the process of breaking a complex problem into smaller,
simpler, and more manageable parts, so that each part can be solved
individually.

Why Decomposition is Important ?

CS25C03- Essentials of Computing 4


Simplifies complex problems.

Makes debugging and testing easier.

Allows teamwork (different people can solve different subproblems).

Saves time and reduces errors.

Real-Life Example:
Problem: Planning a College Cultural Fest

Break into subproblems:

Stage decoration

Food arrangements

Guest invitations

Events scheduling

Security arrangements

👉 Each team can handle one subproblem → whole event runs smoothly.
Computer Example :

Problem: ATM Machine System

Subproblems:

Card authentication

PIN verification

Balance check

Cash withdrawal

Print receipt

3.4 Software Design Concept of an Algorithm

Introduction :
Software design is the process of planning and structuring a solution
before coding.

CS25C03- Essentials of Computing 5


Algorithms are central to this process because they describe step-by-step
procedures for solving a problem logically.

A good software design uses algorithms to ensure efficiency, clarity, and


correctness.

Concept of an Algorithm :
An algorithm is:

A finite sequence of well-defined steps that solves a problem.

Each step must be clear, unambiguous, and executable.

Designed to achieve the desired output from a given input.

Problem Understanding → Algorithms provide clarity on what needs


to be done.

Features of an Algorithm in Software Design :


Finiteness → Must terminate after a finite number of steps.

Definiteness → Each step should be precisely defined.

Input → Accepts zero or more inputs.

Output → Produces at least one result.

Effectiveness → Steps should be simple and basic enough to be


executed.

Role of Algorithms in Software Design:


Systematic Approach → Breaks the problem into logical steps.

Efficiency → Good algorithms minimize time and memory usage.

Maintainability → Structured design makes debugging and future


modifications easier.

Reusability → Algorithms can be applied to similar problems in different


software projects.

Representation of an Algorithm :

CS25C03- Essentials of Computing 6


Natural Language → Step-by-step written description.

Flowcharts → Graphical representation using symbols.

Pseudocode → Structured, programming-like notation.

Decision Tables/Trees → Useful for conditional logic.

Example :
Problem: Find the largest of two numbers.

Algorithm:

1. Start

2. Read two numbers A and B

3. If A > B → Print A is largest

4. Else → Print B is largest

5. Stop

3.5 Algorithm Representation

Introduction :
Once an algorithm is designed, it must be represented in a clear and
structured way so that developers (and sometimes even non-technical people)
can understand and implement it.

Algorithm representation is how we express or document the logic of an


algorithm before coding.

Good representation improves clarity, debugging, maintainability, and


communication between team members.

Common Methods of Algorithm Representation:

1. Natural Language
Algorithm written in plain English.

Example:

1. Start

CS25C03- Essentials of Computing 7


2. Read A, B

3. If A > B then print A else print B

4. Stop

Advantage: Easy to understand.


Limitation: Ambiguity in meaning.

2. Flowcharts
A graphical representation using symbols (rectangles, diamonds, arrows).

Example symbols:

Oval → Start/Stop

Rectangle → Process

Diamond → Decision (Yes/No)

Arrow → Flow of control

CS25C03- Essentials of Computing 8


Advantage: Easy visualization.
Limitation: Becomes complex for large problems.

3. Pseudocode
A structured, programming-like way to represent an algorithm without
actual syntax.

Example:

Algorithm FindLargest
Input: A, B
If A > B then

CS25C03- Essentials of Computing 9


Print A
Else
Print B
EndIf
End

Advantage: Close to programming, easy to implement.


Limitation: Still not executable directly.

4. Decision Tables
A tabular form representing conditions and actions.

Example:

Condition Action

A>B Print A largest

A≤B Print B largest

Advantage: Best for problems with many conditional rules.

5. Decision Trees
A tree-like structure showing decisions and their possible outcomes.

Example: Root node (A > B?) → Left branch (Yes → A largest), Right
branch (No → B largest).

Importance of Algorithm Representation :


Provides clarity before coding.

Helps in debugging and validation of logic.

Makes algorithms easier to communicate across teams.

Useful for teaching and documentation.

3.6 Algorithm Discovery

Introduction :

CS25C03- Essentials of Computing 10


Algorithm discovery is the process of finding or creating an algorithm to
solve a problem when no existing algorithm is available.

It focuses on creativity, logical reasoning, and systematic analysis to


arrive at a new solution.

Steps in Algorithm Discovery :

1. Problem Understanding
Clearly define the problem and constraints.

Example: “How to sort a list of numbers in increasing order efficiently?”

2. Analyze the Problem


Break it down into smaller sub-problems.

Example: Sorting can be divided into: Compare → Swap → Repeat.

3. Brainstorm Possible Approaches


Think of multiple strategies.

Example for sorting: Bubble Sort, Selection Sort, Insertion Sort.

4. Experiment and Refine


Test possible methods with sample data.

Identify mistakes and refine the approach.

5. Validate the Algorithm


Check correctness (does it always give the right result?).

Check efficiency (is it fast enough?).

Techniques for Algorithm Discovery :


Trial and Error – Trying simple steps until a working pattern is found.

Analogy – Using an existing algorithm as inspiration and modifying it.

Example: Binary Search inspired by "guess the number" games.

CS25C03- Essentials of Computing 11


Decomposition – Breaking a big problem into smaller manageable tasks.

Mathematical Reasoning – Using equations or formulas to derive steps.

Heuristics – Using rules of thumb when an exact algorithm is too complex.

Example for Algorithm Discovery :


Problem: Find the largest number in a list.

Step 1: Understand → Need maximum value.

Step 2: Break → Compare each element one by one.

Step 3: Try Approach → Assume first number is largest, then compare with
others.

Step 4: Refine → Update “largest” whenever a bigger number is found.

Step 5: Validate → Works for any list.

Importance of Algorithm Discovery :


Encourages innovation in solving new problems.

Helps in optimizing existing solutions.

Forms the foundation of computer science research (new algorithms are


discovered for AI, cryptography, data science, etc.).

3.7 Iterative Structures

Introduction :
Iteration means repeating a set of instructions until a condition is met.

Iterative structures (also known as loops) are essential in algorithm


design and programming because they allow repeated execution of
statements without writing the same code multiple times.

Types of Iterative Structures :


There are three main types of loops:

1. While Loop

CS25C03- Essentials of Computing 12


Checks the condition before executing the block.

Used when the number of iterations is unknown.

Syntax:

while (condition):
statements

Example:

count = 1
while count <= 5:
print(count)
count += 1

2. For Loop
Repeats a block a specific number of times.

Used when the number of iterations is known.

Syntax:

for variable in range(start, end):


statements

Example:

for i in range(1, 6):


print(i)

3. Do-While Loop (available in C, Java — not in Python


directly)
Executes the block at least once, even if the condition is false.

Syntax (C example):

CS25C03- Essentials of Computing 13


do {
statements;
} while (condition);

Example:

int i = 1;
do {
printf("%d", i);
i++;
} while (i <= 5);

Characteristics of Iterative Structures :


Repetition: Executes a block multiple times.

Condition Control: Each iteration depends on a logical condition


(True/False).

Variable Update: Loop variable changes in every iteration.

Termination: Stops when the condition becomes False.

Advantages :
Reduces code redundancy.

Simplifies algorithm design.

Makes programs easier to modify and debug.

Enables automation of repetitive tasks.

3.8 Recursive Structure

Introduction :
Recursion is a process in which a function calls itself to solve smaller
versions of the same problem.

CS25C03- Essentials of Computing 14


A recursive structure allows repetition through self-reference instead of
loops.

👉 In short: A recursive function solves a big problem by breaking it into


smaller, similar subproblems.

Working Principle of Recursion :


A recursive structure generally has two parts:

1. Base Case – The stopping condition (prevents infinite calling).

2. Recursive Case – The part where the function calls itself.

🔁 Example: Factorial of a Number


Mathematical Definition:

n! = n × (n−1) × (n−2) × ... × 1


Or, in recursive form:
n! = n × (n−1)!
Algorithm:

1. Start

2. If n == 0 → return 1

3. Else → return n × factorial(n−1)

4. Stop

Python Example:

def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)

print(factorial(5)) # Output: 120

CS25C03- Essentials of Computing 15


Types of Recursion
1. Direct Recursion – A function calls itself directly.

def fun():
fun()

2. Indirect Recursion – A function calls another function that calls it back.

def A():
B()

def B():
A()

Difference Between Iteration and Recursion


Feature Iteration Recursion

Repetition Type Loop-based Function calls itself

Termination Based on loop condition Based on base case

Memory Use Less (same variables reused) More (function call stack used)

Speed Usually faster Slower (due to overhead)

Example for , while loops factorial(n) calling itself

3.9 Efficiency and correctness of an Algorithm

Correctness of an Algorithm

Definition
An algorithm is said to be correct if it produces the expected output for every
possible valid input within a finite amount of time.

Two aspects of correctness:


Partial Correctness:

CS25C03- Essentials of Computing 16


If the algorithm terminates, the output must be correct.

Example: Factorial algorithm — gives correct factorial if the input is


positive.

Total Correctness:

The algorithm must terminate and give the correct output for all valid
inputs.

Example: A well-structured search algorithm that works for every list


size.

Efficiency of an Algorithm

Definition:
Efficiency of an algorithm refers to how well it utilizes resources (like time and
memory) to solve a problem.

Types of Efficiency :
Time Efficiency (Time Complexity)

Measures the execution time of the algorithm.

Faster algorithms are preferred in real-world applications.

Example: Binary Search is more time-efficient than Linear Search.

Space Efficiency (Space Complexity)

Measures how much memory the algorithm uses during execution.

Less space = more efficient

3.10 Fundamental Algorithm

Introduction:
Fundamental Algorithms are the building blocks for all programming logic.

They perform simple, commonly used operations such as counting, summing,


exchanging values, computing factorials, etc.

CS25C03- Essentials of Computing 17


Need for Fundamental Algorithm
To build a strong foundation in algorithmic thinking.

To understand flow of logic before using syntax-based programming.

To improve problem-solving efficiency and logical reasoning.

Exchanging the Values of Two Variables


Aim: To swap the values of two variables using a temporary variable.
Algorithm:

1. Start

2. Read A and B

3. TEMP ← A

4. A ← B

5. B ← TEMP

6. Print A, B

7. Stop

Example:
If A = 5, B = 10

After swapping → A = 10, B = 5

Counting:
Aim: To count a series of numbers.
Algorithm:

1. Start

2. Initialize COUNT = 0

3. For each number in the list → COUNT = COUNT + 1

4. Print COUNT

CS25C03- Essentials of Computing 18


5. Stop

Example:
For list [2, 4, 6, 8], COUNT = 4

Summation of a Set of Numbers:

Aim: To find the sum of ‘n’ numbers.


Algorithm:

1. Start

2. Read N

3. SUM ← 0

4. Repeat for i = 1 to N

Read number
SUM ← SUM + number

5. Print SUM

6. Stop

Example:
Input: 5 numbers → 2, 3, 4, 5, 6
Output: SUM = 20

Factorial Computation
Aim: To find the factorial of a number (n!).
Algorithm:

1. Start

2. Read N

3. FACT ← 1

4. For i = 1 to N

CS25C03- Essentials of Computing 19


FACT ← FACT × i

5. Print FACT

6. Stop

Example:
N = 5 → 5! = 120

Reversing the Digits of an Integer

Aim: To reverse a given number.


Algorithm:

1. Start

2. Read N

3. REV ← 0

4. Repeat while N > 0


REM ← N mod 10

REV ← REV × 10 + REM


N ← N div 10

5. Print REV

6. Stop

Example:
Input = 1234 → Output = 4321

Base Conversion
Aim: To convert a decimal number to binary.
Algorithm:

1. Start

2. Read DECIMAL number N

CS25C03- Essentials of Computing 20


3. Repeat while N > 0
REM ← N mod 2
Store REM in array
N ← N div 2

4. Print array in reverse order

5. Stop

Example:

Decimal = 10 → Binary = 1010

CS25C03- Essentials of Computing 21

You might also like