Lecture 3: Problem Solving, Algorithms, Pseudocode, and Flowcharts
Problem Solving in Computer Science
- Solving problems is the core of computer science
- Programmers must understand how humans solve problems, then translate solutions into computer-executable algorithms
- Machines may solve problems differently than humans
Stages of Problem Solving
- Analyzing the problem
- Writing algorithm steps (pseudocode & flowcharts)
- Writing the program in a programming language
- Translating to machine code
- Executing, testing, and debugging
- Documenting the software
Algorithms
Definition: A set of rules and procedures for solving a problem in finite steps
Characteristics of a Good Algorithms:
- Each step must be clear and unambiguous
- Steps described using precise language
- Must terminate after finite steps
- Must lead to correct solution
Algorithm Representation Methods
Pseudocode
- Uses English-like phrases to outline programs
- Focuses on solution steps rather than syntax
- Easily convertible to actual programming code
Statement Structures:
text
// Sequential Statements - executed in order
SET x = 5
SET y = 10
SET result = x + y
// Decision Statements
IF temperature > 30 THEN
PRINT "It's hot"
ELSE
PRINT "It's cool"
END IF
// Looping Statements
SET i = 1
WHILE i <= 5 DO
PRINT i
SET i = i + 1
END WHILEFlowcharts
- Graphical representation of process steps using standardized symbols
Key Symbols
- Oval: Start/End points
- Arrow: Direction of flow
- Rectangle: Process steps
- Diamond: Decision points
- Parallelogram: Input/Output (I/O)
- Connector Points: Connect different chart parts
- Text Label: Describe actions/decisions
- Hexagon: For loops (initial condition, increment, termination)
Flowchart Guidelines
- Must be neat, complete, easy to follow, and unambiguous
- Natural direction: left to right, top to bottom
- Clear flow lines with proper connections
Advantages
- Communication: Standard symbols facilitate understanding
- Action Analysis: Effective problem analysis
- Documentation: Important documentation tool
- Efficient Coding: Aids program writing
- Error Detection: Easy to trace and identify errors
- Program Correction: Easier debugging and fixes
Disadvantages
- Complexity: Can become complex for large problems
- Modifications: Changes may require redrawing
- Copying Issues: Difficult to reproduce if symbols are hard to draw
- Detail Overload: Excessive details can obscure main solution
Code Examples
Adding Two Numbers
pseudocode
BEGIN
INPUT first number (N1)
INPUT second number (N2)
SET Sum = N1 + N2
OUTPUT Sum
ENDCircle Area and Perimeter
pseudocode
BEGIN
SET PI = 3.14
INPUT Radius
SET Area = PI * Radius * Radius
SET Perimeter = 2 * PI * Radius
OUTPUT Area, Perimeter
ENDFinding Maximum of Three Numbers
pseudocode
BEGIN
INPUT A, B, C
IF A > B THEN
IF A > C THEN
PRINT "A is largest"
ELSE
PRINT "C is largest"
END IF
ELSE
IF B > C THEN
PRINT "B is largest"
ELSE
PRINT "C is largest"
END IF
END IF
ENDPrinting First Five Natural Numbers
pseudocode
BEGIN
SET i = 1
WHILE i <= 5 DO
PRINT i
SET i = i + 1
END WHILE
ENDRepetition Structures
- Pre-test loop: Condition checked at beginning (while)
- Post-test loop: Condition checked at end (do-while)
- Each loop must have a termination condition