'F' → Fullscreen
Tonight you search a haunted house, in a new program named 2-9-haunted-house. Eight rooms, each holding something — dust, cobwebs, bats, and one ghost. You open doors in order until you find what you are looking for, and every door you open is one check.
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.
rooms names each room, and contents says what is in it. SearchHouse opens doors in order, counts them, and returns the room's index — or -1 if it never finds the target.string[] rooms = { "Hall", "Kitchen", "Library", "Parlor", "Cellar", "Nursery", "Study", "Tower" };string[] contents = { "dust", "cobwebs", "dust", "ghost", "rats", "dust", "cobwebs", "bats" };
SearchHouse("ghost");
int SearchHouse(string target){ int doors = 0;
for (int i = 0; i < rooms.Length; i++) { doors = doors + 1; Console.WriteLine($"Opening the {rooms[i]}... {contents[i]}");
if (contents[i] == target) { Console.WriteLine($"Found the {target} in the {rooms[i]}. Doors opened: {doors}."); return i; } }
Console.WriteLine($"No {target} anywhere. Doors opened: {doors}."); return -1;}string[] rooms = { "Hall", "Kitchen", "Library", "Parlor", "Cellar", "Nursery", "Study", "Tower" };string[] contents = { "dust", "cobwebs", "dust", "ghost", "rats", "dust", "cobwebs", "bats" };
SearchHouse("dust");SearchHouse("bats");SearchHouse("pirate");
int SearchHouse(string target){ int doors = 0;
for (int i = 0; i < rooms.Length; i++) { doors = doors + 1; Console.WriteLine($"Opening the {rooms[i]}... {contents[i]}");
if (contents[i] == target) { Console.WriteLine($"Found the {target} in the {rooms[i]}. Doors opened: {doors}."); return i; } }
Console.WriteLine($"No {target} anywhere. Doors opened: {doors}."); return -1;}Next.string[] rooms = { "Hall", "Kitchen", "Library", "Parlor", "Cellar", "Nursery", "Study", "Tower" };string[] contents = { "dust", "cobwebs", "dust", "candles", "rats", "dust", "cobwebs", "bats" };
int hiding = Random.Shared.Next(0, rooms.Length);contents[hiding] = "ghost";
SearchHouse("ghost");
int SearchHouse(string target){ int doors = 0;
for (int i = 0; i < rooms.Length; i++) { doors = doors + 1; Console.WriteLine($"Opening the {rooms[i]}... {contents[i]}");
if (contents[i] == target) { Console.WriteLine($"Found the {target} in the {rooms[i]}. Doors opened: {doors}."); return i; } }
Console.WriteLine($"No {target} anywhere. Doors opened: {doors}."); return -1;}CountInHouse, written yourself. It returns how many rooms hold the target. Line 8 calls it for dust.SearchHouse stops at the first match. Explain why CountInHouse cannot, and what that does to its best case.string[] rooms = { "Hall", "Kitchen", "Library", "Parlor", "Cellar", "Nursery", "Study", "Tower" };string[] contents = { "dust", "cobwebs", "dust", "candles", "rats", "dust", "cobwebs", "bats" };
int hiding = Random.Shared.Next(0, rooms.Length);contents[hiding] = "ghost";
SearchHouse("ghost");Console.WriteLine($"Rooms with dust: {CountInHouse("dust")}");
int SearchHouse(string target){ int doors = 0;
for (int i = 0; i < rooms.Length; i++) { doors = doors + 1; Console.WriteLine($"Opening the {rooms[i]}... {contents[i]}");
if (contents[i] == target) { Console.WriteLine($"Found the {target} in the {rooms[i]}. Doors opened: {doors}."); return i; } }
Console.WriteLine($"No {target} anywhere. Doors opened: {doors}."); return -1;}
// CountInHouse(target) returns how many rooms hold target.// Check every room, and add 1 to a counter each time the// room's contents match. Return the counter at the end.// Write it here, below SearchHouse.SearchFromTop, which opens the doors in the opposite order: Tower first, Hall last. A for loop can count down as well as up.The deck's Find, written the way the AP exam would write it.
int Find(string name){ for (int i = 0; i < crew.Length; i++) { if (crew[i] == name) { return i; } }
return -1;}PROCEDURE find(aList, name){ i ← 1 REPEAT UNTIL(i > LENGTH(aList)) { IF(aList[i] = name) { RETURN(i) } i ← i + 1 } RETURN(-1)}The reference sheet has no for. You keep the counter yourself: set i before the loop and add 1 to it at the bottom of the loop's body. Forgetting the i ← i + 1 makes a loop that never ends, exactly as a while without its update did in Activity 1.17.
C# repeats while there is more array to check. The exam repeats until it has gone past the end, so the condition is flipped: i > LENGTH(aList).
This is the one that costs points. Lyra is at index 2 in C# and index 3 on the exam. Both are correct in their own notation. Before you answer a question about a position, check which notation you are reading.
Task 2 prints exactly this.
Opening the Hall... dustFound the dust in the Hall. Doors opened: 1.Opening the Hall... dustOpening the Kitchen... cobwebsOpening the Library... dustOpening the Parlor... ghostOpening the Cellar... ratsOpening the Nursery... dustOpening the Study... cobwebsOpening the Tower... batsFound the bats in the Tower. Doors opened: 8.Opening the Hall... dustOpening the Kitchen... cobwebsOpening the Library... dustOpening the Parlor... ghostOpening the Cellar... ratsOpening the Nursery... dustOpening the Study... cobwebsOpening the Tower... batsNo pirate anywhere. Doors opened: 8.SearchHouse returns the index it found, or -1, and counts every door it opensCountInHouse returns 3 for dust unless the ghost lands in a dusty room, and you explained why it is then 2Answer the following questions before submitting your work.
SearchHouse returns -1 for not found. What would go wrong if it returned 0 instead?Submit four things to the dropbox:
CountInHouse.