'F' → Fullscreen
Copy template-console to 1-05-math and write this:
print(10 + 3)print(10 - 3)print(10 * 3)print(10 / 3)print(10 % 3)print(2 ^ 3)137303.333333333333318Copy all six answers onto your sheet. Then change the numbers and predict each answer before you run it. Keep going until you stop being surprised.
Add this to the bottom of the same file:
local a = 80local b = 90local c = 100
print("Average: " .. a + b + c / 3)Average: 203.33333333333Three test scores of 80, 90 and 100, and it reports an average of 203. Work out on paper what it actually calculated, write that on your sheet, then fix the line with parentheses and check you get 90.
Copy the template to 1-05-time. This one asks a question, so it needs tonumber from last session:
io.write("How many seconds? ")local total = tonumber(io.read())
local minutes = math.floor(total / 60)local seconds = total % 60
print(total .. " seconds is " .. minutes .. " minutes and " .. seconds .. " seconds.")How many seconds? 500 [Enter]500 seconds is 8 minutes and 20 seconds.Test it with 500, then 60, then 59, then 3600. Write down what it says for each.
math.floor(total / 60) is how many whole minutes fit, and total % 60 is what did not fit. Together they account for every second.Divide by zero:
print(10 / 0)infNo error. inf is short for infinity, and it is a value like any other — you can store it, print it, and add to it. Try print(10 / 0 + 5) and write down what you get.
Every other language you meet will do something different here. Several of them crash.
Write a program that asks for a number of cents and answers with how many quarters, dimes, nickels and pennies to hand back — largest coins first. 117 cents is 4 quarters, 1 dime, 1 nickel and 2 pennies.
You have everything you need: math.floor for how many fit and % for what is left.
The arithmetic on the exam is the arithmetic you just did. The operators are the same and so is the order of operations.
local total = tonumber(io.read())
local minutes = math.floor(total / 60)local seconds = total % 60
print(minutes)print(seconds)total ← INPUT()
seconds ← total MOD 60minutes ← (total - seconds) / 60
DISPLAY(minutes)DISPLAY(seconds)| Lua | Exam notation | |
|---|---|---|
| Add, subtract, multiply, divide | + - * / | + - * / |
| Remainder | 17 % 5 | 17 MOD 5 |
| Ask the user | io.read() | INPUT() |
| Show a value | print(x) | DISPLAY(x) |
math.floor on the exam, and there is no rounding operator at all. To drop the decimal you subtract the remainder first, then divide — which is what the right hand column above does.^ on the exam. A power has to be written as repeated multiplication.DISPLAY puts a space after what it shows. Two of them in a row give you space-separated output, not two things jammed together. This matters on trace questions.17 / 5 is 3.4 on the exam, exactly as it is in Lua. Division never truncates in either place. If you have used a language where it does, forget that here.1-05-time is correct for 500, 60, 59 and 3600Answer the following questions before submitting your work.
a + b + c / 3 and why it is not a bug in Lua.math.floor(total / 60) and total % 60 answer two different questions about the same number. Say what each one answers, in words, without using the word divide.inf and did not stop the program. Describe a situation where that is worse than crashing.Submit the required files to the appropriate dropbox.