'F' → Fullscreen
Two short programs today. First the go-kart race is over, in a new program named 2-12-race-results: the results board is in the order people crossed the timing mat, not the order they finished. You sort it, then find out how much work sorting a bigger race would take. Then you plan a route around a theme park, where the best answer is out of reach and a heuristic has to do.
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.
racers and their lap times in seconds. SortByTime is selection sort on the times, and it counts every comparison. Its bool parameter decides whether it prints the times after each pass.string[] racers = { "Zippy", "Blaze", "Comet", "Rocket", "Nova", "Turbo" };int[] times = { 94, 87, 102, 79, 91, 85 };
int comparisons = SortByTime(true);Console.WriteLine($"Comparisons: {comparisons}");Console.WriteLine($"Winner: {racers[0]} in {times[0]} seconds");
int SortByTime(bool show){ int comparisons = 0;
for (int i = 0; i < times.Length - 1; i++) { int fastest = i;
for (int j = i + 1; j < times.Length; j++) { comparisons = comparisons + 1;
if (times[j] < times[fastest]) { fastest = j; } }
int tempTime = times[i]; times[i] = times[fastest]; times[fastest] = tempTime;
string tempName = racers[i]; racers[i] = racers[fastest]; racers[fastest] = tempName;
if (show) { Console.Write($"After pass {i + 1}:");
for (int k = 0; k < times.Length; k++) { Console.Write($" {times[k]}"); }
Console.WriteLine(); } }
return comparisons;}show set to false. It works because SortByTime reads times and racers from the main program, and lines 15 and 16 point those names at new, bigger arrays (Activity 2.5: a method sees outward).string[] racers = { "Zippy", "Blaze", "Comet", "Rocket", "Nova", "Turbo" };int[] times = { 94, 87, 102, 79, 91, 85 };
int comparisons = SortByTime(true);Console.WriteLine($"Comparisons: {comparisons}");Console.WriteLine($"Winner: {racers[0]} in {times[0]} seconds");
Console.WriteLine("Sorting the sorted results again:");Console.WriteLine($"Comparisons: {SortByTime(true)}");
int[] sizes = { 8, 16, 32, 64 };
for (int s = 0; s < sizes.Length; s++){ times = new int[sizes[s]]; racers = new string[sizes[s]];
for (int k = 0; k < times.Length; k++) { times[k] = Random.Shared.Next(60, 181); racers[k] = $"Racer {k + 1}"; }
Console.WriteLine($"{sizes[s]} racers: {SortByTime(false)} comparisons");}
int SortByTime(bool show){ int comparisons = 0;
for (int i = 0; i < times.Length - 1; i++) { int fastest = i;
for (int j = i + 1; j < times.Length; j++) { comparisons = comparisons + 1;
if (times[j] < times[fastest]) { fastest = j; } }
int tempTime = times[i]; times[i] = times[fastest]; times[fastest] = tempTime;
string tempName = racers[i]; racers[i] = racers[fastest]; racers[fastest] = tempName;
if (show) { Console.Write($"After pass {i + 1}:");
for (int k = 0; k < times.Length; k++) { Console.Write($" {times[k]}"); }
Console.WriteLine(); } }
return comparisons;}Doubling and the four lines that print it. Doubling returns a long: a whole number like int, but stored in 64 bits instead of 32, so it holds numbers about four billion times bigger. Compare Doubling(64) with the deck's table. It is not even close. Record all four results and explain what happened, using what Activity 1.2 showed about a fixed number of bits.string[] racers = { "Zippy", "Blaze", "Comet", "Rocket", "Nova", "Turbo" };int[] times = { 94, 87, 102, 79, 91, 85 };
int comparisons = SortByTime(true);Console.WriteLine($"Comparisons: {comparisons}");Console.WriteLine($"Winner: {racers[0]} in {times[0]} seconds");
Console.WriteLine("Sorting the sorted results again:");Console.WriteLine($"Comparisons: {SortByTime(true)}");
int[] sizes = { 8, 16, 32, 64 };
for (int s = 0; s < sizes.Length; s++){ times = new int[sizes[s]]; racers = new string[sizes[s]];
for (int k = 0; k < times.Length; k++) { times[k] = Random.Shared.Next(60, 181); racers[k] = $"Racer {k + 1}"; }
Console.WriteLine($"{sizes[s]} racers: {SortByTime(false)} comparisons");}
Console.WriteLine($"Doubling(32): {Doubling(32)}");Console.WriteLine($"Doubling(62): {Doubling(62)}");Console.WriteLine($"Doubling(63): {Doubling(63)}");Console.WriteLine($"Doubling(64): {Doubling(64)}");
int SortByTime(bool show){ int comparisons = 0;
for (int i = 0; i < times.Length - 1; i++) { int fastest = i;
for (int j = i + 1; j < times.Length; j++) { comparisons = comparisons + 1;
if (times[j] < times[fastest]) { fastest = j; } }
int tempTime = times[i]; times[i] = times[fastest]; times[fastest] = tempTime;
string tempName = racers[i]; racers[i] = racers[fastest]; racers[fastest] = tempName;
if (show) { Console.Write($"After pass {i + 1}:");
for (int k = 0; k < times.Length; k++) { Console.Write($" {times[k]}"); }
Console.WriteLine(); } }
return comparisons;}
long Doubling(int n){ long total = 1;
for (int i = 0; i < n; i++) { total = total * 2; }
return total;}Steps counts the steps between two places along the paths, across plus up or down, never diagonally. NearestRoute always walks to the nearest ride it has not ridden yet, using a bool array the way Raylib Lab 6 remembered which invaders were hit.string[] rides = { "Gate", "Carousel", "Ferris Wheel", "Coaster", "Log Flume", "Bumper Cars", "Haunted Mine" };int[] xs = { 0, 0, 0, 3, 7, 4, 9 };int[] ys = { 0, 4, 7, 0, 2, 7, 9 };
NearestRoute();
int Steps(int a, int b){ int dx = xs[a] - xs[b]; int dy = ys[a] - ys[b];
if (dx < 0) { dx = -dx; }
if (dy < 0) { dy = -dy; }
return dx + dy;}
int NearestRoute(){ bool[] visited = new bool[rides.Length]; visited[0] = true; int current = 0; int total = 0;
for (int stop = 1; stop < rides.Length; stop++) { int best = -1;
for (int i = 0; i < rides.Length; i++) { if (!visited[i] && (best == -1 || Steps(current, i) < Steps(current, best))) { best = i; } }
Console.WriteLine($" to the {rides[best]}: {Steps(current, best)} steps"); total = total + Steps(current, best); visited[best] = true; current = best; }
Console.WriteLine($" back to the Gate: {Steps(current, 0)} steps"); total = total + Steps(current, 0); Console.WriteLine($"Total: {total} steps"); return total;}NearestRoute a parameter for the ride it starts from, and run the rule from all seven places. Print each total and the best.int temp = list[i];list[i] = list[smallest];list[smallest] = temp;temp ← aList[i]aList[i] ← aList[smallest]aList[smallest] ← tempAssigning one list element to another is written the same way in both notations, with ← in place of =. The exam expects you to read a three-line swap and know why it needs the third variable.
On the reference sheet, aList ← bList assigns a copy of the list. In C#, int[] a = b; makes a second name for the same array — Activity 1.23's copy pair. Sort through one name, and the other sees the change.
So an exam question can hand you two list names, sort one, and expect the other to be untouched. The same question in C# would have a different answer.
i ← 1REPEAT UNTIL(i > n){ j ← 1 REPEAT UNTIL(j > n) { <one step> j ← j + 1 } i ← i + 1}The question is almost always the same: how many times does the inner statement run? Read the bounds, not the shape. Two loops that each run n times give n × n steps. If the inner loop ran 3 times instead, it would be 3 × n, which grows like n, not like n × n.
And one thing you will not be asked for. There is a formal notation for efficiency, and the course framework puts it outside the exam. Twice the input, four times the work is the level of answer the exam wants.
Task 1 prints exactly this.
After pass 1: 79 87 102 94 91 85After pass 2: 79 85 102 94 91 87After pass 3: 79 85 87 94 91 102After pass 4: 79 85 87 91 94 102After pass 5: 79 85 87 91 94 102Comparisons: 15Winner: Rocket in 79 secondsAnd Task 4 prints exactly this.
to the Coaster: 3 steps to the Log Flume: 6 steps to the Bumper Cars: 8 steps to the Ferris Wheel: 4 steps to the Carousel: 3 steps to the Haunted Mine: 14 steps back to the Gate: 18 stepsTotal: 56 steps56 is the right answer for this heuristic. It is also the wrong answer for the problem, and knowing the difference is the point.
Doubling(64) printedAnswer the following questions before submitting your work.
Submit four things to the dropbox:
Doubling printed with your explanation, and your Task 4 answers.