'F' → Fullscreen
Today you go diving for sunken gold in a new program named 2-5-treasure-dive. Deeper water holds more gold but burns more air, and a lamp doubles what you find but costs air of its own. Your tank holds 100 air. By the end you will be planning dives to get the most gold out of one tank.
How to read the code blocks. Each task's code block shows your whole program, from line 1, as it should look when that task is finished.
The green lines marked with a + are the ones you add or change for that task — everything else is already in your file. Do not type the + sign.
Haul works out how much gold one dive brings back. It takes three parameters of three different jobs: how deep, how long, and whether you took the lamp. Every 10 m of depth is worth 1 gold a minute, and the lamp doubles the total.Haul(true, 5, 30), and run it. Write down the error code and what each error says. Put it back.Haul(5, 30, false). Write down what it prints, and explain where the number came from. Put it back.Console.WriteLine("Treasure Dive: practice dives");Console.WriteLine($"10 m for 10 minutes, no lamp: {Haul(10, 10, false)} gold");Console.WriteLine($"30 m for 5 minutes, no lamp: {Haul(30, 5, false)} gold");Console.WriteLine($"30 m for 5 minutes, lamp: {Haul(30, 5, true)} gold");
int Haul(int depth, int minutes, bool lamp){ int perMinute = depth / 10; int coins = perMinute * minutes;
if (lamp) { coins = coins * 2; }
return coins;}Haul worked out a variable called coins. Add line 5, which tries to print it from the main program.coins actually lives, and when it exists. Delete line 5 before you move on.Console.WriteLine("Treasure Dive: practice dives");Console.WriteLine($"10 m for 10 minutes, no lamp: {Haul(10, 10, false)} gold");Console.WriteLine($"30 m for 5 minutes, no lamp: {Haul(30, 5, false)} gold");Console.WriteLine($"30 m for 5 minutes, lamp: {Haul(30, 5, true)} gold");Console.WriteLine(coins);
int Haul(int depth, int minutes, bool lamp){ int perMinute = depth / 10; int coins = perMinute * minutes;
if (lamp) { coins = coins * 2; }
return coins;}air, in the main program. Two new methods work with it in different ways.AirAfter takes the air as a parameter named air, subtracts, and returns. Breathe takes only the amount used, and reaches outward to change the main program's air directly.Console.WriteLine("Treasure Dive: practice dives");Console.WriteLine($"10 m for 10 minutes, no lamp: {Haul(10, 10, false)} gold");Console.WriteLine($"30 m for 5 minutes, no lamp: {Haul(30, 5, false)} gold");Console.WriteLine($"30 m for 5 minutes, lamp: {Haul(30, 5, true)} gold");
int air = 100;
Console.WriteLine($"Practice check: {AirAfter(air, 30)} air would be left");Console.WriteLine($"Air in the tank: {air}");
Breathe(30);Console.WriteLine($"After a real dive: {air}");
int Haul(int depth, int minutes, bool lamp){ int perMinute = depth / 10; int coins = perMinute * minutes;
if (lamp) { coins = coins * 2; }
return coins;}
int AirAfter(int air, int used){ air = air - used; return air;}
void Breathe(int used){ air = air - used;}AirAfter and Breathe. They are no longer called, and C# would warn that they are never used.AirUsed, written yourself. It takes the same three parameters as Haul and returns the air a dive uses: each minute costs 1 air plus 1 more for every 10 m of depth, and the lamp adds 10 to the whole dive. A 10-minute dive at 30 m with the lamp uses 50; check yours gives that before playing.AirUsed both have a variable called used. Write one sentence explaining why the two do not collide.int air = 100;int gold = 0;int dive = 1;
Console.WriteLine("Treasure Dive. Your tank holds 100 air.");
while (air > 0){ Console.Write($"Dive {dive}. How deep, 10 to 50 m? "); int depth = int.Parse(Console.ReadLine()); Console.Write("How many minutes? "); int minutes = int.Parse(Console.ReadLine()); Console.Write("Take the lamp? (true/false) "); bool lamp = bool.Parse(Console.ReadLine());
int used = AirUsed(depth, minutes, lamp);
if (used > air) { Console.WriteLine($"That dive needs {used} air. You run out and drop the treasure."); air = 0; } else { int found = Haul(depth, minutes, lamp); air = air - used; gold = gold + found; Console.WriteLine($"Found {found} gold. Air left: {air}."); }
dive = dive + 1;}
Console.WriteLine($"You surface with {gold} gold.");
int Haul(int depth, int minutes, bool lamp){ int perMinute = depth / 10; int coins = perMinute * minutes;
if (lamp) { coins = coins * 2; }
return coins;}
// AirUsed(depth, minutes, lamp) returns how much air a dive uses:// every minute costs 1 air, plus 1 more for each 10 m of depth,// so a minute at 30 m costs 4 air;// and taking the lamp adds 10 air to the whole dive.// Write it here, below Haul, in the same shape as Haul.while loop after the depth question that asks again until the depth is between 10 and 50, the way Activity 1.17 re-asked for input.The reference sheet writes a procedure with several parameters as PROCEDURE procName(parameter1, parameter2, ...), and says arg1 is assigned to parameter1, and so on — matched by position, exactly as in C#.
double BoxCost(double weight, int distance){ return weight * 0.5 + distance * 0.02;}PROCEDURE boxCost(weight, distance){ RETURN(weight * 0.5 + distance * 0.02)}Parameters are separated by commas in both. The exam version has no types: no double, no int, no bool, and no return type in front of the name. So the exam can never catch a swapped argument the way CS1503 did today — every swap is the silent kind.
The reference sheet has no rule written down for where a name can be used, and no way to declare a variable other than assigning to it. That silence is worth knowing. An exam question about scope will describe it in words or show it in ordinary program code. Do not invent a notation for it.
One real game from the finished program, ending with 120 gold. Your dives will be different.
Treasure Dive. Your tank holds 100 air.Dive 1. How deep, 10 to 50 m? 50 [Enter]How many minutes? 10 [Enter]Take the lamp? (true/false) true [Enter]Found 100 gold. Air left: 30.Dive 2. How deep, 10 to 50 m? 20 [Enter]How many minutes? 5 [Enter]Take the lamp? (true/false) true [Enter]Found 20 gold. Air left: 5.Dive 3. How deep, 10 to 50 m? 10 [Enter]How many minutes? 3 [Enter]Take the lamp? (true/false) false [Enter]That dive needs 6 air. You run out and drop the treasure.You surface with 120 gold.Haul and AirUsed each take three parameters, and AirUsed(30, 10, true) gives 50AirAfter and Breathe are gone, and the program builds with no warningsAnswer the following questions before submitting your work.
AirAfter left the tank at 100 and Breathe took it down to 70. Explain the difference, and say which one you would rather call without reading its code first.Submit four things to the dropbox:
AirUsed.used variables, and your three games with the lamp sentence.