edited by
2,567 views
2 votes
2 votes

Consider the following pseudo-code

I=0; J=0; K=8;
while(I<K-1) //while-1
{
     J=J+1;
     while(J<K) //while-2
     { 
         if(x[I]<x[J])
         {
             temp = x[I];
             x[I]=x[J];
             x[J]=temp;
         }
     }// end of while-2
     I=I+1;
}// end of while-1

The cyclomatic complexity of the above is

  1. $3$
  2. $2$
  3. $4$
  4. $1$
edited by

1 Answer

1 votes
1 votes

$\underline{\textbf{Answer:}\Rightarrow}\;\mathbf {c.}(4)$

$\underline{\textbf{Explanation:}\Rightarrow}$

Cyclomatic complexity is defined as the quantitative estimate of the number of linearly independent paths in it.

It is calculated using the control flow graph of the program.

$\underline{\text{Example:}}$

If a code does not have any control flow statement then the cyclomatic complexity will be $\mathbf 1$.

If the source code contains $\textbf {one if condition}$ then the cyclomatic complexity will be $\mathbf 2$ since there will be $\textbf{two paths}$ one for true and another one for false.

For while loop cyclomatic complexity will be $\mathbf 1$

In this question, Number of while loops are $2$. So, cyclomatic complexity of the while loop $=2\times 1$

Number of if statement $=1$. So, cyclomatic complexity of the if statement $=2$

$\therefore$ Total cyclomatic complexity $=2+2 = 4$


It can also be calculated by drawing the control flow diagram and using the below formula:

$\mathbf{M = E-N+2P}$

where,

$\mathbf E = $Number of edges in the control flow graph.

$\mathbf N =$ the number of nodes in the control flow graph.

$\mathbf P = $the number of connected components.

$\therefore \;\mathbf c$ is the correct option.


https://www.geeksforgeeks.org/cyclomatic-complexity/

edited by
Answer:

Related questions

3 votes
3 votes
1 answer
1
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...
3 votes
3 votes
1 answer
2
Satbir asked Jan 13, 2020
2,398 views
In a class definition with $10$ methods, to make the class maximally cohesive, number of direct and indirect connections required among the methods are$90,0$$45,0$$10,10$...
2 votes
2 votes
1 answer
3
Satbir asked Jan 13, 2020
1,344 views
What is the defect rate for Six sigma?$1.0$ defect per million lines of code$1.4$ defects per million lines of code$3.0$ defects per million lines of code$3.4$ defects pe...
2 votes
2 votes
2 answers
4
Satbir asked Jan 13, 2020
3,711 views
What is the availability of the software with the following reliability figuresMean Time Between Failures (MTBF) is $20$ daysMean Time To Repair (MTTR) is $20$ hours$90\%...