A checkout program calls rebate_cents(p), where p is any nonnegative integer purchase amount in cents. The procedure returns p // 10 cents. Here, // means whole-number division with the remainder discarded, and % gives the remainder. The procedure prints nothing and makes no changes outside its local calculation. No execution-speed requirement is specified.
Which proposed replacements preserve the specified behavior for every accepted input? Select all that apply.
Start a counter at zero, repeatedly subtract 10 from a private local copy of p while at least 10 remains, and return the counter after counting each subtraction.
Calculate p // 20, double that result, return the doubled number, and perform no printing or changes outside the local calculation.
Split the amount into q = p // 100 and r = p % 100, return 10 * q + r // 10, and make no external changes.
Calculate and return p // 10, and also append the purchase amount to a shared log file without printing anything on the screen.
Unlock full course notes by signing up
Pass one procedure's result into another
A delivery tracker provides two procedures with no side effects:
total_minutes(hours, minutes) accepts whole-number hours from 0 through 23 and minutes from 0 through 59. It returns 60 * hours + minutes, measured in minutes after midnight.
delay_minutes(planned, actual) accepts two times measured in minutes after midnight. It returns actual - planned, so a positive result means late arrival.
Read the behavior at packet boundaries
A seed-packing program uses full_packets(items, packet_size). It accepts a nonnegative whole number of seeds and a positive whole-number packet size. It returns the number of completely filled packets that can be made; leftover seeds do not count as another packet. The procedure does not change either argument or produce other visible effects.
Complete the return-value table for packets holding six seeds. Enter exact integers in all answer cells.
Read abstract values from different encodings
A monitoring component stores the set of active sensors among sensor IDs 101, 102, 103 and 104. Its public operations are countActive(), which returns the number of active sensors, and isActive(id), which returns whether the specified sensor is active.
Two internal encodings are supported:
Flags: four entries correspond, in order, to IDs 101, 102, 103 and 104. An entry of 1 means active; 0 means inactive.
Track a shared manifest and an independent copy
A school supply application represents a shipment manifest using a hidden storage structure. You interact with each manifest only through these specified operations:
count(x) returns the current number of notebooks recorded in manifest x as an integer value.
add(x, n) increases that manifest's recorded count by n notebooks.