2X: Extra Practice

This lesson, about extra practice with the min and max functions, is optional. If you get stuck on a problem, feel free to skip to the next problem and come back later.

Simplifying a complex expression

Multiple Choice Exercise: Simplification
What is a simplification of the following expression?
max(x - 3, min(x + 10, x + 5))
Correct! Since x + 5 is always smaller than x + 10, we can simplify min(x + 10, x + 5) to just x + 5. Then working our way outwards, the entire expression is max(x - 3, x + 5). Since x + 5 is always the maximum of these two numbers, that is the result.

Complicating a simple expression

Coding Exercise: Complication
Assume that the grader defines two variables A and B for you. Write a program which prints out the value
min(A, B)
However, there is a catch: your program is not allowed to use the min function. Instead, use max in a clever way to simulate min. Hint, Method 1Hint, Method 2

Payment Calculator

Coding Exercise: Payment Calculator
A credit card company computes a customer's "minimum payment" according to the following rule. The minimum payment is equal to either $10 or 2.1% of the customer's balance, whichever is greater; but if this exceeds the balance, then the minimum payment is the balance. Write a program to print out the minimum payment using min and max. Assume that the variable balance contains the customer's balance. Your program does not need to print the dollar sign.
Example 1: if your balance is 1000, then your program should print 21.
Example 2: if your balance is 600, then your program should print 12.6.
Example 3: if your balance is 25, then your program should print 10.
Example 4: if your balance is 8, then your program should print 8.
Hint

Sorting Scramble

The final problem is a challenging problem, about sorting numbers in a weird way. There are better, simpler, and faster sorting methods that you can learn about after completing our introductory lessons.

Scramble Exercise: Sorting Scramble
Code scramble: make the program sort the three numbers xy and z into increasing order, so that x has the smallest value, y has the next smallest value, and z has the largest value.
Drag and drop with your mouse to rearrange the lines. Click for a hint.
  • x = min(x, y)
  • y = tmp
  • y = min(y, z)
  • tmp = max(x, y)
  • y = tmp
  • z = tmp
  • x = min(x, y)
  • tmp = max(x, y)
  • tmp = max(y, z)

Continue on to lesson 3!