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

  1. Oval (Start/Stop): The beginning and end.
  2. Parallelogram (Input/Output): Reading a card, Printing a value.
  3. Rectangle (Process): Calculations ().
  4. 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):

  1. Draw Columns: One for every variable () and the Input ().
  2. Row by Row: Go through each data item.
  3. Update: Cross out old values, write new ones.

Example (Detailed Solution)

Data: 5, 12, 8. Trace:

StepInput (X)Condition (X > 10?)AB
Init--00
Card 15No01
Card 212Yes11
Card 38No12

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:

  1. It checks for “Chennai”.
  2. It adds X.Marks (a value), not 1. Answer: Sum of marks for students from Chennai.

Pattern 3: The “Min/Max” Logic

Context: “Find the highest mark.”

TIP

Mental Algorithm:

  1. Init: Set Max = 0 (or a very small number).
  2. Loop: Compare every card X with Max.
  3. 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

  1. Trace: . Data: 2, 4, 6. Loop: . Final A?
    • Hint: .
  2. Logic: If A > B then C = 1 else C = 0. What does C=1 mean?
    • Hint: A is strictly greater than B.
  3. 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 > 500 increment count.
  • Step 6: Move to Pile 2 and repeat from Step 2. Bug: Repeating from Step 2 resets count to 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 Date in Jan-Jun, increment A.
  • Step 6: If Date in Jul-Dec, increment B.
  • Step 8: If A < B then set C = 1. Meaning: C becomes 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.