Algorithm CS Thinking Example 3

Follow the full solution, then compare it with the other examples linked below.

Example 3

easy
Write an algorithm to find the largest number in a list of three numbers: A, B, C.

Solution

  1. 1
    Step 1: Set largest = A.
  2. 2
    Step 2: If B > largest, set largest = B. If C > largest, set largest = C. Output largest.

Answer

Compare each number against the current largest, updating as needed, then output the result.
Finding the maximum is a common algorithm pattern. It uses a running variable that tracks the best answer seen so far.

About Algorithm

A step-by-step set of instructions for solving a problem or accomplishing a specific task. An algorithm must be precise (every step is unambiguous), finite (it terminates after a bounded number of steps), and effective (each step can actually be carried out).

Learn more about Algorithm โ†’

More Algorithm Examples