• retagged by
21,576 views
14 14 votes

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_______________.

2 Answers

Best answer
22 22 votes
Number of predicates $= 4,$ if, else if, while and if.

Cyclomatic complexity $=$ No. of predicates $+ 1 = 5.$
• edited by
1 1 vote

The cyclomatic complexity of a structured program[a] is defined with reference to the control flow graph of the program, a directed graph containing the basic blocks of the program, with an edge between two basic blocks if control may pass from the first to the second. The complexity M is then defined as

    M = E − N + 2P,
where
    E = the number of edges of the graph.
    N = the number of nodes of the graph.
    P = the number of connected components.  

Source: http://en.wikipedia.org/wiki/Cyclomatic_complexity For a single program (or subroutine or method), P is always equal to 1. So a simpler formula for a single subroutine is

    M = E − N + 2 

For the given program, the control flow graph is:q100

 E = 13, N = 10.

Therefore, E - N + 2 = 5. 

Answer:
Position:
Show:

Related questions

9 9 votes
5 answers 5 answers
8.9k
8.9k views
go_editor asked Sep 28, 2014
8,889 views
In the context of modular software design, which one of the following combinations is desirable?High cohesion and high couplingHigh cohesion and low couplingLow cohesion ...
15 15 votes
4 answers 4 answers
18.9k
18.9k views
go_editor asked Sep 29, 2014
18,935 views
The cyclomatic complexity of each of the modules $\text{A}$ and $\text{B}$ shown below is $10.$ What is the cyclomatic complexity of the sequential integration shown on t...
7 7 votes
4 answers 4 answers
8.4k
8.4k views
go_editor asked Feb 16, 2015
8,353 views
Consider the following software items: Program-$X$, Control Flow Diagram of Program-$Y$ and Control Flow Diagram of Program-$Z$ as shown belowThe values of McCabe's Cyclo...
6 6 votes
3 answers 3 answers
6.5k
6.5k views
Misbah Ghaya asked Feb 11, 2015
6,509 views
Match the following:(P) Condition Coverage (i) Black-box testing(Q) Equivalence class partitioning (ii) System testing(R) Volume testing ...