retagged by
13,212 views
11 votes
11 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_______________.

retagged by

2 Answers

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

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

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:

Related questions

4 votes
4 votes
2 answers
3
Ishrat Jahan asked Nov 3, 2014
2,782 views
Tb carry out white box testing of a program, its flow chart representation is obtained as shown in the figure below:For basis path based testing of this program, its cycl...
3 votes
3 votes
1 answer
4
Ishrat Jahan asked Nov 2, 2014
3,836 views
Consider the following program module:int module1 (int x, int y) { while (x! = y) { if (x y) x = x - y, else y = y - x; } return x; }What is Cyclomatic complexity of the...