Assignment 1 (40 points)
1. Video Game Character Health (9 points)
Write an algorithm that calculates and displays the percentage of health remaining for a video game character. The program reads (in this order) the character’s current health (use variable current) and maximum health (use variable max).
The percentage of health remaining is calculated using:
percent = (current / max) × 100
Preconditions:
- The maximum health must be greater than 0.
- current must be less than or equal to max.
Note:
- DO NOT use loops for this program
- Display the word “error” if there are error cases
Test cases
Input | Expected Output |
[5,50] | [10] |
[50, 5] | [error] |
[35, -5] | [error] |
Q1.1 Identify the inputs, outputs, and error conditions
(1.5 points)
Q1.2 Write the algorithm in Pseudocode. Use the reference sheet provided on the course website and do not write comments. (4.5 points)
Q1.3 Write 3 additional test cases in the format [inputs][output] that result in at least 1 positive value and 1 error. (3 points)
2. Multiples (7.5 points)
Write an algorithm, without using conditionals (no IF-THEN-ELSE), that reads 2 whole numbers and displays TRUE if:
- the first number is 0
OR
- the second number is 0
OR
- the first number is a multiple of the second number
It displays FALSE otherwise.
Preconditions:
- Inputs are both greater than or equal to 0, and there will always be exactly two numbers
Hint:
- num1 (first number) is a multiple of num2 (second number) if num1 = num2 * x, where x is any integer value
- Use boolean variables instead of conditionals
Example
Inputs | Expected Outputs |
[5,6] | FALSE |
[8,4] | TRUE |
[8,0] | TRUE |
Q2.1 First, identify the inputs and outputs of the program. Then identify any error conditions, or write “None” if there aren’t any present. (1.5 points)
Q2.2 Write the algorithm is Pseudocode. Use the reference sheet provided on the course website and do not write comments. (4.5 points)
Q2.3 Write 3 additional test cases in the format [inputs][output] that result in at least true boolean and false boolean (1.5 points)
3. Road Trip (10.5 points)
Write a pseudocode algorithm that displays how far the vehicle can travel (d). The program reads, in this order, how many gallons of fuel the vehicle starts with (g), the estimated of number of miles per gallon the vehicle can travel (mpg), the boolean of whether it’s raining (r), the boolean of whether they travel through mountains (m), the number of passengers (p) and the boolean whether it’s the weekend (w).
This is how the mpg is impacted:
- If w is true, traffic is heavy and you drive slowly, multiplying the mpg by 0.70.
- If r is true, the wet roads slow down the trip, multiplying the current mpg by 0.85.
- If m is true, multiply the current mpg by 0.90. The detour leads to less efficiency.
- If p ≥ 3, multiply the current mpg by 1.05 due to the ability to use HOV lanes.
The distance that the vehicle can travel is calculated by:
d = g * mpg
where mpg is the updated miles per gallon value.
Preconditions:
- The mpg will always be >0
- The number of passengers will always be >0 and not exceed the capacity of the vehicle
Note:
- Do not use loops for this program
- Display error if there are error cases
- Use the same variable names as provided above
- The value of mpg may be modified multiple times in the same iteration of the program
Example
Inputs | Expected Outputs |
[12.0 30.0 true false 4 true] | [225.0] |
[-40.0 35.0 true false 2 true] | [error] |
[18.0 45.0 false true 1 false] | [729.0] |
Q3.1 First, identify the inputs and outputs of the program. Then identify any error conditions, or write “None” if there aren’t any present. (1.5 points)
Q3.2 Write the algorithm is pseudocode. Use the reference sheet provided on the course website and do not write comments. (7.5 points)
Q3.3 Write 3 additional test cases in the format [inputs][output] that result in at least 1 positive value and 1 error. (1.5 points)
4. Counting Operations (3 points)
Q4.1 What is the minimum number of operations? (1.5 points)
Q4.2 What is the maximum number of operations? (1.5 points)
5. Evaluating and Counting Operations (10 points)
Q5.1 What is displayed by the algorithm when n has a value of 3? (3 points)
Q5.2 How many operations are executed when n has a value of 3? (3 points)
Q5.3 How many operations are executed when the algorithm is run with any value of n? Write your answer in terms of n. (4 points)
Adapted by: Apoorva Goel

