The GATE Grind

GATE 2015 CS – Question 38

Programming and Data Structures · Programming in C · 2 marks · Numerical answer

Consider the following C program segment.

while (first <= last)
{
    if (array[middle] < search)
        first = middle + 1;
    else if (array[middle] == search)
        found = TRUE;
    else last = middle - 1;
    middle = (first + last) / 2;
}
if (first > last) notPresent = TRUE;

The cyclomatic complexity of the program segment is ________.

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 5

Explanation

Cyclomatic complexity equals the number of decision points plus one. The decisions are the `while` condition, the `if` on `array[middle] < search`, the `else if` on equality, and the final `if (first > last)`. That is 4 decisions, so the complexity is $4 + 1 = 5$.