Skip to study material
← Question index

QUESTION 34 · Worked algorithms

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

  1. Read n: The count must be at least 1.
  2. Read the first value: Set maximum to this value.
  3. Read each remaining value: If it is greater than maximum, replace maximum.
  4. 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.

Reference table

↔ Scroll horizontally on a small screen
Boundary checks
CaseExpected behaviour
One valueIt is the maximum.
All values negativeInitialisation from input still works.
Repeated equal valuesThe repeated value is retained.
Count zeroReject because the set must be non-empty.

Diagram or flowchart

Read the first value, initialise the candidate values, scan each remaining input and output the result.
Scan a set for its maximum · Download diagram

C program and output

/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
int main(void)
{
    int n, value;
    if (scanf("%d", &n) != 1 || n < 1 || n > 100) {
        puts("Unsupported count"); return 1;
    }
    if (scanf("%d", &value) != 1) { puts("Invalid value"); return 1; }
    int maximum = value;
    
    for (int i = 1; i < n; ++i) {
        if (scanf("%d", &value) != 1) { puts("Invalid value"); return 1; }
        if (value > maximum) { maximum = value; }
        
    }
    printf("Maximum = %d\n", maximum);
    return 0;
}

Sample input and output

Input

4
-8 3 3 -2

Output

Maximum = 3

Key points

  • Initialise the maximum from the first actual input, then update the candidate as the remaining values are read.
  • Do not initialise a candidate to zero unless the input domain justifies it.

Related exam questions

Topic matches from the supplied JNTUH papers. Marks below are the printed allocations for those questions.

  • March/April 2023 · Q2(a) · 4 marks

    Write pseudocode to find the biggest number in a given set.

    Paper P05 · page 1
View all matching exam references →