CS: Problem Solving with Algorithms
Design, trace, test, and improve step-by-step solutions
CS: Problem Solving with Algorithms
Design, trace, test, and improve step-by-step solutions
CS - Grade 6-8
- 1
Write an algorithm with 5 to 7 clear steps for brushing your teeth. Each step should be specific enough for someone else to follow.
Think about the exact order of actions from start to finish.
A good algorithm would include clear ordered steps, such as pick up the toothbrush, add toothpaste, wet the brush, brush all areas of the teeth for two minutes, rinse your mouth, rinse the toothbrush, and put the toothbrush away. - 2
The steps below are out of order for finding the largest number in a list. Put them in the correct order: A. Compare each remaining number to largest. B. Report largest. C. Set largest to the first number in the list. D. If a number is greater than largest, replace largest with that number.
The correct order is C, A, D, B. First set largest to the first number, then compare each remaining number, update largest when needed, and finally report the largest value. - 3
Trace this algorithm using the list [3, 6, 2, 8]: Set total to 0. For each number in the list, add the number to total. After the loop, report total. What value is reported?
Keep a running total after each number is added.
The algorithm reports 19. The total starts at 0, then becomes 3, then 9, then 11, and finally 19. - 4
A robot starts at the bottom-left square of a 4 by 4 grid and must reach the top-right square. It can only move up or right. Write one possible algorithm using exactly 6 moves.
One possible algorithm is move up, move up, move up, move right, move right, move right. This reaches the top-right square in exactly 6 moves. - 5
For an algorithm that calculates the average of three quiz scores, identify the input, process, and output.
Inputs are what the algorithm receives, and outputs are what it gives back.
The inputs are the three quiz scores. The process is to add the scores and divide the sum by 3. The output is the average quiz score. - 6
Find the bug in this algorithm for counting the number of students in a line: For each student in the line, add 1 to count. Report count. What important step is missing?
The missing step is to set count to 0 before the loop begins. Without starting count at 0, the algorithm may report an incorrect number. - 7
Write pseudocode for deciding whether to bring an umbrella. The algorithm should use the condition: if the forecast says rain, bring an umbrella; otherwise, do not bring one.
Use an if-else structure.
The pseudocode could be: If the forecast says rain, then bring an umbrella. Otherwise, do not bring an umbrella. This uses a conditional decision to choose between two actions. - 8
A sorted list has 1,000 names. Student A checks each name from the beginning until the target is found. Student B repeatedly cuts the search area in half. Which algorithm is usually more efficient, and why?
A sorted list allows an algorithm to use the order of the data.
Student B's algorithm is usually more efficient because cutting the search area in half removes many possible names after each step. Student A's algorithm may need to check many names one at a time. - 9
Write an algorithm that decides whether a number is even or odd.
A correct algorithm is to divide the number by 2 and check the remainder. If the remainder is 0, report that the number is even. Otherwise, report that the number is odd. - 10
Decompose the problem of planning a class party into at least four smaller tasks that could each have its own algorithm.
Decomposition means breaking a large problem into smaller parts.
The problem can be decomposed into tasks such as choosing a date, making a guest list, planning food, organizing activities, and setting up the room. Each smaller task is easier to solve than the whole problem at once. - 11
Trace this loop: Set count to 0. Repeat 5 times: add 2 to count. Report count. What value is reported?
The algorithm reports 10. The value of count increases by 2 five times: 2, 4, 6, 8, and 10. - 12
A flowchart starts at Start, then asks Is score 70 or higher? If yes, it goes to Report pass. If no, it goes to Report try again. What output happens for a score of 68?
Compare 68 to the decision value 70.
For a score of 68, the output is Report try again. This happens because 68 is not 70 or higher, so the no branch is followed. - 13
Two algorithms solve the same problem of sorting cards from lowest to highest. Algorithm 1 checks the whole hand over and over until all cards are in order. Algorithm 2 finds the lowest remaining card each time and places it next. Which algorithm sounds more organized, and why?
Algorithm 2 sounds more organized because it follows a clear repeated rule: find the lowest remaining card and place it next. Algorithm 1 may work, but it is less specific and may waste steps checking the whole hand many times. - 14
Create three test cases for an algorithm that reports whether a number is positive, negative, or zero. Include one expected output for each test case.
Choose values that test each possible branch of the algorithm.
Good test cases include 5, which should report positive; -3, which should report negative; and 0, which should report zero. These tests check all three possible categories. - 15
A maze-solving algorithm says: Move forward until you hit a wall. Turn right. Repeat. Give one reason this algorithm might fail in some mazes and suggest one improvement.
This algorithm might fail because it could get stuck in a loop and repeat the same path without reaching the exit. One improvement is to mark visited paths so the algorithm can avoid trying the same path again.