Exercise on Algorithms, Flowcharts & Structure Diagrams
1. Making a Cup of Tea
Structure Diagram:
MAKE_TEA
├── PREPARE
│ ├── Boil water
│ ├── Get cup
│ └── Get tea bag
├── BREW
│ ├── Place tea bag in cup
│ ├── Pour hot water
│ └── Steep for 3 minutes
├── CUSTOMIZE
│ ├── Check sugar preference
│ └── Add sugar if needed
└── SERVE
└── Tea ready
Algorithm:
1. START
2. Boil water in kettle
3. Place tea bag in cup
4. Pour boiled water into cup
5. Wait 3 minutes for steeping
6. Remove tea bag
7. IF user wants sugar THEN
8. Add sugar
9. Stir
10. END IF
11. Tea is ready
12. STOP
```
Flowchart:
2. Finding the Larger of Two Numbers
Structure Diagram:
FIND_LARGER
├── INPUT
│ ├── Read Number A
│ └── Read Number B
├── COMPARE
│ ├── Check A > B
│ ├── Check B > A
│ └── Check equality
└── OUTPUT
├── Display larger number
└── Or display equal message
Algorithm:
1. START
2. INPUT Number A
3. INPUT Number B
4. IF A > B THEN
5. PRINT "Number A is larger: " + A
6. ELSE IF B > A THEN
7. PRINT "Number B is larger: " + B
8. ELSE
9. PRINT "Numbers are equal"
10. END IF
11. STOP
Flowchart:
3. Summing the First 5 Natural Numbers
Structure Diagram:
SUM_NUMBERS
├── INITIALIZE
│ ├── Set counter = 1
│ └── Set sum = 0
├── LOOP_PROCESS
│ ├── WHILE counter <= 5
│ │ ├── Add counter to sum
│ │ └── Increment counter
│ └── END WHILE
└── OUTPUT
└── Display sum
Algorithm:
1. START
2. SET counter = 1
3. SET total_sum = 0
4. WHILE counter <= 5 DO
5. total_sum = total_sum + counter
6. counter = counter + 1
7. END WHILE
8. PRINT "Sum of first 5 natural numbers: " + total_sum
9. STOP
Flowchart:
4. Checking if a Number is Positive or Negative
Structure Diagram:
CHECK_SIGN
├── INPUT
│ └── Read number
├── CLASSIFY
│ ├── Check if positive >= 0
│ └── Check if negative < 0
└── OUTPUT
├── Display "Positive"
└── Display "Negative"
Algorithm:
1. START
2. INPUT number
3. IF number >= 0 THEN
4. PRINT "The number is Positive"
5. ELSE
6. PRINT "The number is Negative"
7. END IF
8. STOP
Flowchart:
5. Calculating the Area of a Rectangle
Structure Diagram:
CALCULATE_AREA
├── INPUT
│ ├── Read length
│ └── Read width
├── CALCULATION
│ └── Area = length × width
└── OUTPUT
└── Display area
Algorithm:
1. START
2. INPUT length
3. INPUT width
4. area = length × width
5. PRINT "Area of rectangle: " + area
6. STOP
Flowchart:
Summary of Programming Structures
Structure Diagrams:
- Hierarchical representation showing program organization
- Top-down approach from main goal to sub-tasks
- Visual overview of program components and relationships
Algorithms:
- Step-by-step instructions in plain language
- Sequential flow with clear logic
- Easy to understand and translate to code
Flowcharts:
- Graphical representation of program flow
- Standard symbols for different operations
- Visual logic showing decision points and loops
Key Benefits:
1. Planning: Think through logic before coding
2. Communication: Share ideas with team members
3. Debugging: Identify logical errors early
4. Documentation: Explain how program works
5. Learning: Understand programming concepts visually
These three representations work together to provide a complete picture of program design,
from high-level structure to detailed implementation steps.
Flowchat Diagrams