GATE CS 2015 Set 1 — Question 38
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.NAT+2 / -0MediumControl FlowC ProgrammingProgramming & Data StructuresBasic Blocks & Flow GraphsCode OptimizationCompiler Design
Compiler Design → Code Optimization → Basic Blocks & Flow Graphs
Last updated
Question
Consider the following C program segment.
The cyclomatic complexity of the program segment is ________.
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 ________.
Correct answer
5 to 5
Solution
The given C program segment implements a binary search algorithm.Cyclomatic complexity measures the number of linearly independent paths through a program's source code. It can be calculated using the formula:
where:
Let's identify the decision points in the given code segment:
Cyclomatic Complexity = Number of decision points + 1
Cyclomatic Complexity = Thus, the cyclomatic complexity of the program segment is 5.
where:
- is the number of edges in the control flow graph.
- is the number of nodes in the control flow graph.
- is the number of connected components (usually 1 for a single program).
Let's identify the decision points in the given code segment:
1.
while(first <= last): This is a decision point.2.
if (array[middle] < search): This is a decision point.3.
else if (array[middle] == search): This is a decision point.4.
There are 4 decision points in the code segment.Using the simplified formula:if (first > last): This is a decision point.Cyclomatic Complexity = Number of decision points + 1
Cyclomatic Complexity = Thus, the cyclomatic complexity of the program segment is 5.
Continue learning with Success Tracker
A step still unclear? Work through it with support
Use Success Tracker to ask about the reasoning, then try another GATE CS question to check your understanding.
AI-powered practice· Unlimited practice on eligible plans
- PYQs with solutions
- Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
- Practice that adapts
- Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
- AI doubt support
- Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.
Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.
This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.
More questions on Code Optimization
2026 Set 2 Q12The set T represents various traversals over binary tree. The set S represents the order of…2026 Set 2 Q17In C runtime environment, which one of the following is stored in heap?2026 Set 2 Q19Consider the following three ANSI-C programs, P1, P2, and P3. P1 [code] P2 [code] **P3**…2026 Set 1 Q23Let be an odd number greater than 100. Consider a binary minheap with elements stored in an…2026 Set 1 Q24Consider a hash table that is initially empty. The hash table is maintained…