Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A stopwatch app stores a time as a number of seconds. This pseudocode turns it into minutes and seconds.
DECLARE TotalSeconds : INTEGER
DECLARE Minutes : INTEGER
DECLARE Seconds : INTEGER
INPUT TotalSeconds
Minutes ← DIV(TotalSeconds, 60)
Seconds ← MOD(TotalSeconds, 60)
OUTPUT Minutes, " minutes ", Seconds, " seconds"Dry-run the program for the input 500. What are the values of Minutes and Seconds? Then find the input that gives 3 minutes 0 seconds and is larger than every other input that does.
A variable is declared with its type: DECLARE Name : INTEGER. The five types are INTEGER, REAL, CHAR, STRING and BOOLEAN. A constant takes a literal: CONSTANT Name ← 5. Assignment is ←; = compares. DIV(A, B) is the whole part of , and MOD(A, B) is the remainder.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Step 1. The minutes. fits into eight whole times, because . So Minutes is 8.
Step 2. The seconds. . So Seconds is 20. The output is 8 minutes 20 seconds.
Step 3. The second question. We need DIV(T, 60) = 3 and MOD(T, 60) = 0. Only one input does that: . So the largest such input, and the only one, is 180.
Check. . The two parts put back together give the input.
Answer: Minutes is 8 and Seconds is 20; the input is 180.
Log in to join the discussion.
Loading…