Assignment 1 (40 points)
1. Average Speed (9 points)
Write an algorithm that calculates and displays the average speed traveled by a certain object. The program reads (in this order), the starting position of the object (use variable d1) in meters, then the final position (use variable d2) in meters, and the time taken to travel from start to finish (use variable t) in seconds.
Average speed is calculated by the following formula: v=(d2-d1)/t
Preconditions:
- d1 and d2 are values greater than or equal to zero
- d2 is larger than or equal to d1
Note:
- DO NOT use loops for this program
- Display the word “error” if there are error cases
Test cases
Input | Expected Output |
[5,25,30] | [0.67] |
[1,4,-1] | [error] |
[8,12,0] | [error] |
Q1.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)
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, 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 will be displayed if n = 3? (3 points)
Q5.2 What is the total number of operations if n = 3? (3 points)
Q5.3 What is the total number of operations for any n? Your answer must be a function of n. (4 points)
Adapted by: Mary Buist
