🔢

Mod and Div Operators in Pseudocode

Jul 27, 2025

Overview

This lecture covers the use of the mod and div operators, validation techniques, and method (function) syntax in IB Computer Science pseudocode, including practical coding examples and related exam questions.

Mod and Div Operators

  • The mod operator returns the remainder when dividing two integers (e.g., 25 mod 6 = 1).
  • Mod is often used to test for divisibility (e.g., if n mod 3 = 0, then n is divisible by 3).
  • The div operator returns the integer quotient of division, ignoring any remainder (e.g., 28 div 3 = 9).
  • If the left operand of mod is smaller than the right, the result is the left operand (e.g., 3 mod 28 = 3).
  • Mod and div are frequently used in IB exam trace tables and coding problems.

Coin Change Pseudocode Example

  • Problem: Given up to 99 cents, calculate the minimum number of US coins (quarters, dimes, nickels, pennies) to make that amount.
  • Use div to determine the quantity of each coin type, then mod to find the remaining cents.
  • Input validation ensures the amount is between 0 and 99 inclusive using a while loop.

Input Validation

  • Validation checks that user input fits required parameters (e.g., value range).
  • Typically implemented using a while loop to repeatedly prompt for input until criteria are met.
  • Example: while cents > 99 or cents < 0, prompt user to re-enter cents.

Methods and Functions in Pseudocode

  • Methods/functions are used to organize code and simplify complex problems.
  • Function definition syntax: functionName(parameters)... end functionName.
  • To return a value, use a return statement; to output, use output statement.
  • Example: circle_area(radius) calculates and returns Ï€ × radius².

IB Function Examples

  • Example: calc_BMI(height, weight) returns weight / (height × height).
  • To categorize BMI, write a function using conditional (if/else if) statements to output categories based on BMI value.

Working with Arrays and Search Algorithms

  • Data may be stored in parallel arrays (e.g., name, weight, height) with corresponding indices.
  • To find a value by attribute (e.g., height), loop through the relevant array and use the index to reference related data.
  • Binary search is not suitable unless the array is sorted.

Outputting Data Based on Calculations

  • To find members with BMI above average, calculate group average, then loop to compare each member’s BMI with the average.
  • Use calc_BMI in a loop and output names where individual BMI > average BMI.

Key Terms & Definitions

  • mod — Returns the remainder of division between two integers.
  • div — Returns the integer quotient of division, discarding any remainder.
  • Validation — Checking if input meets specified requirements.
  • Method/Function — A reusable block of code that performs a specific task.
  • Trace Table — A tool for tracking variable values through code execution.

Action Items / Next Steps

  • Practice writing pseudocode using mod, div, validation, and methods.
  • Complete the provided slideshow practice problems on your own.
  • Review IB past exam coding questions for additional practice.