CT Week 1: Flowcharts & Pseudocode
0. Prerequisites
NOTE
What you need to know:
- Logic: Basic “If this, then that” reasoning.
- Arithmetic: Basic addition, subtraction, comparison ().
Quick Refresher
- Algorithm: A step-by-step recipe to solve a problem.
- Pseudocode: Code written in plain English (not a specific language like Python).
- Flowchart: Visual diagram of an algorithm.
1. Core Concepts
1.1 Flowchart Symbols
- Oval (Start/Stop): The beginning and end.
- Parallelogram (Input/Output): Reading a card, Printing a value.
- Rectangle (Process): Calculations ().
- Diamond (Decision): Questions with Yes/No answers ().
- Crucial: Always has 2 arrows coming out (True/False).
1.2 The “Pile” Metaphor (Dataset)
- Pile 1: The input stack of cards (Data).
- Pile 2: The processed stack (Done).
- Iteration: “Pick card from Pile 1 Process Move to Pile 2 Repeat”.
- Stop Condition: “If Pile 1 is empty, stop.”
1.3 Variables
- Containers: Boxes that hold one value at a time.
- Initialization: Setting a starting value ().
- Assignment: Updating the value ().
- Note: The old value is erased!
2. Pattern Analysis & Goated Solutions
Pattern 1: The “Dry Run” Table (Master Skill)
Context: “What is the value of A at the end?” Code:
Step 1: A = 0, B = 0
Step 2: Read Card X
Step 3: If X > 10, A = A + 1
Step 4: Else, B = B + 1
Data: 5, 12, 8.
TIP
Mental Algorithm (The Trace Table):
- Draw Columns: One for every variable () and the Input ().
- Row by Row: Go through each data item.
- Update: Cross out old values, write new ones.
Example (Detailed Solution)
Data: 5, 12, 8. Trace:
| Step | Input (X) | Condition (X > 10?) | A | B |
|---|---|---|---|---|
| Init | - | - | 0 | 0 |
| Card 1 | 5 | No | 0 | 1 |
| Card 2 | 12 | Yes | 1 | 1 |
| Card 3 | 8 | No | 1 | 2 |
Answer: A = 1, B = 2.
Pattern 2: Counting vs Summing
Context: “Does variable A count the cards or sum the values?”
TIP
Mental Algorithm:
- Counting: Look for
A = A + 1. (Adds 1 for every match).- Summing: Look for
A = A + X.Value. (Adds the content of the card).
Example (Detailed Solution)
Code: If X.City == "Chennai" then A = A + X.Marks
Analysis:
- It checks for “Chennai”.
- It adds
X.Marks(a value), not1. Answer: Sum of marks for students from Chennai.
Pattern 3: The “Min/Max” Logic
Context: “Find the highest mark.”
TIP
Mental Algorithm:
- Init: Set
Max = 0(or a very small number).- Loop: Compare every card
XwithMax.- Update:
If X > Max then Max = X.
- Logic: If I see something bigger than my current champion, the new guy becomes the champion.
3. Practice Exercises
- Trace: . Data: 2, 4, 6. Loop: . Final A?
- Hint: .
- Logic:
If A > B then C = 1 else C = 0. What does C=1 mean?- Hint: A is strictly greater than B.
- Flowchart: What shape is “Is A > 10?”
- Hint: Diamond (Decision).
đź§ Level Up: Advanced Practice
Question 1: The Initialization Trap
Problem: A procedure counts bills > 500.
- Step 2: Initialize
count = 0. - Step 5: If
Bill > 500incrementcount. - Step 6: Move to Pile 2 and repeat from Step 2.
Bug: Repeating from Step 2 resets
countto 0 for every card. The final result will only be 0 or 1 (based on the last card). Fix: Repeat from Step 3 (Check if Pile 1 empty).
Question 2: Nested Logic Flow
Problem:
- Step 5: If
Datein Jan-Jun, incrementA. - Step 6: If
Datein Jul-Dec, incrementB. - Step 8: If
A < Bthen setC = 1. Meaning:Cbecomes 1 only if there are more students born in the second half (Jul-Dec) than the first half.
Question 3: Data Sanity
Problem: Identifying “Bad Data” in a dataset.
- Row 1: Card Number is “Twelve” (String instead of Int). → Incorrect Data Type.
- Row 4: Letter Count is -5. → Invalid Value (Count cannot be negative). Takeaway: Always check if data matches the expected type and logical constraints.