In classBuilt in class, straight after Activity 2.10's deck.
An array drawn as a row of bars, and a search you step through one press of Space at a time. The bar being checked turns yellow, the bars the search has ruled out go dark, and the target turns green when it is found. By the end you will watch linear search and binary search side by side — and watch binary search throw away the very bar it is looking for.

Task 3: binary search finds 67 in two presses
'F' → Fullscreen
Create a new Raylib program named 2-11-search.
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.
This lab runs on the classroom computers, not in Juicemind. Close the window to end the program. Escape works too.
DrawBar takes an index and a color. It works out the bar's x from the index and its height from the value at that index, then draws the bar and its value underneath.values and run it again. Only one bar should change. Put it back.DrawBar has two parameters, but the index ends up deciding two things about the bar. Write down which two, and what the color parameter decides.#:package Raylib-cs@8.0.0
using Raylib_cs;
int[] values = { 8, 15, 21, 30, 37, 44, 52, 60, 67, 75, 83, 91 };
Raylib.InitWindow(800, 450, "Search");Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose()){ Raylib.BeginDrawing(); Raylib.ClearBackground(Color.DarkBlue);
for (int i = 0; i < values.Length; i++) { DrawBar(i, Color.SkyBlue); }
Raylib.EndDrawing();}
Raylib.CloseWindow();
void DrawBar(int index, Color color){ int x = 40 + index * 62; int height = values[index] * 3;
Raylib.DrawRectangle(x, 360 - height, 46, height, color); Raylib.DrawText($"{values[index]}", x + 12, 372, 20, Color.White);}current is the index being checked, starting at -1 (nothing checked yet), and found is where the target turned up. Each press of Space checks the next bar. BarColor decides each bar's color from those two numbers, and the drawing loop hands it to DrawBar.target to 40, which is not in the array, and press through. Record the checks when it says it is not there.#:package Raylib-cs@8.0.0
using Raylib_cs;
int[] values = { 8, 15, 21, 30, 37, 44, 52, 60, 67, 75, 83, 91 };int target = 67;int current = -1;int found = -1;
Raylib.InitWindow(800, 450, "Search");Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose()){ if (Raylib.IsKeyPressed(KeyboardKey.Space) && found == -1 && current < values.Length - 1) { current = current + 1;
if (values[current] == target) { found = current; } }
Raylib.BeginDrawing(); Raylib.ClearBackground(Color.DarkBlue);
for (int i = 0; i < values.Length; i++) { DrawBar(i, BarColor(i)); }
Raylib.DrawText($"Linear search for {target}. Checks: {current + 1}", 40, 20, 24, Color.White);
if (found != -1) { Raylib.DrawText($"Found at index {found}", 40, 410, 20, Color.Green); } else if (current == values.Length - 1) { Raylib.DrawText("Not in the list", 40, 410, 20, Color.Red); } else { Raylib.DrawText("Press Space to check the next bar", 40, 410, 20, Color.LightGray); }
Raylib.EndDrawing();}
Raylib.CloseWindow();
void DrawBar(int index, Color color){ int x = 40 + index * 62; int height = values[index] * 3;
Raylib.DrawRectangle(x, 360 - height, 46, height, color); Raylib.DrawText($"{values[index]}", x + 12, 372, 20, Color.White);}
Color BarColor(int index){ if (index == found) { return Color.Green; }
if (index == current) { return Color.Yellow; }
if (index < current) { return Color.DarkGray; }
return Color.SkyBlue;}low and high mark the bars still possible, and mid is the one being checked. Anything outside the edges is ruled out.target back at 67, and press through. Record the checks, and compare with Task 2.#:package Raylib-cs@8.0.0
using Raylib_cs;
int[] values = { 8, 15, 21, 30, 37, 44, 52, 60, 67, 75, 83, 91 };int target = 67;int low = 0;int high = values.Length - 1;int mid = -1;int found = -1;int checks = 0;
Raylib.InitWindow(800, 450, "Search");Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose()){ if (Raylib.IsKeyPressed(KeyboardKey.Space) && found == -1 && low <= high) { mid = (low + high) / 2; checks = checks + 1;
if (values[mid] == target) { found = mid; } else if (values[mid] < target) { low = mid + 1; } else { high = mid - 1; } }
Raylib.BeginDrawing(); Raylib.ClearBackground(Color.DarkBlue);
for (int i = 0; i < values.Length; i++) { DrawBar(i, BarColor(i)); }
Raylib.DrawText($"Binary search for {target}. Checks: {checks}", 40, 20, 24, Color.White);
if (found != -1) { Raylib.DrawText($"Found at index {found}", 40, 410, 20, Color.Green); } else if (low > high) { Raylib.DrawText("Not in the list", 40, 410, 20, Color.Red); } else { Raylib.DrawText("Press Space to check the middle bar", 40, 410, 20, Color.LightGray); }
Raylib.EndDrawing();}
Raylib.CloseWindow();
void DrawBar(int index, Color color){ int x = 40 + index * 62; int height = values[index] * 3;
Raylib.DrawRectangle(x, 360 - height, 46, height, color); Raylib.DrawText($"{values[index]}", x + 12, 372, 20, Color.White);}
Color BarColor(int index){ if (index == found) { return Color.Green; }
if (index == mid) { return Color.Yellow; }
if (index < low || index > high) { return Color.DarkGray; }
return Color.SkyBlue;}target back to 67.#:package Raylib-cs@8.0.0
using Raylib_cs;
int[] values = { 8, 15, 21, 30, 37, 44, 52, 60, 91, 75, 83, 67 };int target = 67;int low = 0;int high = values.Length - 1;int mid = -1;int found = -1;int checks = 0;
Raylib.InitWindow(800, 450, "Search");Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose()){ if (Raylib.IsKeyPressed(KeyboardKey.Space) && found == -1 && low <= high) { mid = (low + high) / 2; checks = checks + 1;
if (values[mid] == target) { found = mid; } else if (values[mid] < target) { low = mid + 1; } else { high = mid - 1; } }
Raylib.BeginDrawing(); Raylib.ClearBackground(Color.DarkBlue);
for (int i = 0; i < values.Length; i++) { DrawBar(i, BarColor(i)); }
Raylib.DrawText($"Binary search for {target}. Checks: {checks}", 40, 20, 24, Color.White);
if (found != -1) { Raylib.DrawText($"Found at index {found}", 40, 410, 20, Color.Green); } else if (low > high) { Raylib.DrawText("Not in the list", 40, 410, 20, Color.Red); } else { Raylib.DrawText("Press Space to check the middle bar", 40, 410, 20, Color.LightGray); }
Raylib.EndDrawing();}
Raylib.CloseWindow();
void DrawBar(int index, Color color){ int x = 40 + index * 62; int height = values[index] * 3;
Raylib.DrawRectangle(x, 360 - height, 46, height, color); Raylib.DrawText($"{values[index]}", x + 12, 372, 20, Color.White);}
Color BarColor(int index){ if (index == found) { return Color.Green; }
if (index == mid) { return Color.Yellow; }
if (index < low || index > high) { return Color.DarkGray; }
return Color.SkyBlue;}target by 1 before a search starts, and show the target on screen.Every picture here is a screenshot of this page's own programs, with the key presses played back automatically.

Task 2, linear search finds 67

Task 4, 67 and 91 swapped
DrawBar draws every bar, and BarColor decides every colorAnswer the following questions before submitting your work.
DrawBar and BarColor are yours; the key check for Space is not. Explain which part of the Create task's rules decides that.Submit four things to the dropbox: