Develop pseudocode to find the largest value in a given set.
5 marks
Answer in brief
The maximum of a non-empty set can be found by reading each value and updating the current result.
Algorithm
Read n: The count must be at least 1.
Read the first value: Set maximum to this value.
Read each remaining value: If it is greater than maximum, replace maximum.
Display the result: After all values have been checked, output the maximum.
Explanation
The starting value must come from the input. Suppose all values are negative. Starting maximum at zero would give a value that is not even in the input. Starting from the first actual value avoids that mistake. After each new value is checked, the stored result is correct for all values read so far.
Example
For the inputs −8, 3, 3 and −2, maximum starts at −8. Reading 3 changes it to 3. The next 3 and −2 do not change it. Thus the maximum is 3. A repeated maximum is allowed; keeping the current value is sufficient.
Boundary cases
With only one input, that value is the maximum. Equal values do not cause a problem. An empty input is rejected because there is no first value to use. The program accepts 1–100 integers. Each input must fit int. It reads values one at a time, so it does not need an array.
Time and space complexity
Each of n values is read once, with a fixed number of comparisons for each new value. Time is O(n). The program stores only a few counters and result variables, so extra space is O(1). In the flowchart, max and min mean choosing the larger or smaller value; they are mathematical operations, not C library calls used by this program.