Cyclomatic Complexity-Practice Questions
Cyclomatic Complexity-Practice Questions
2 x x =2 = 2-1 = 1
3 x x =2 = 2-1 = 1
4 x x =2 = 2-1 = 1
5 x =1 = 1-1 = 0
6 x =1 = 1-1 = 0
7 --- ---
Cyclomatic Complexity = 4 + 1 = 5
Independent Paths:
1. 1-7 (value[i] = -999.0)
2. 1-2-7 (value[i] = 0, totinputs = 100)
3. 1-2-3-6-1-7
4. 1-2-3-4-6-1-7
5. 1-2-3-4-5-6-1-7
Practice Quiz
void fib (int x)
{
int a=0,b=1,sum=0;
for(int i=1;i<=x;i++)
{
sum=a+b;
cout<<sum<<"\t";
a=b;
b=sum;
}
}
Solution
Cyclomatic Complexity:
Number of regions = 2
Number of predicates = 1+1 = 2
Edges – Nodes + 2 = 5 – 5 + 2 = 2
Sample Quiz
#include <stdio.h>
int main()
{
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
while (n != 0)
{
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);
return 0;
}
4
1 2 3 6
Cyclomatic Complexity:
Number of regions = 3
Number of predicates = 2+1 = 3
Edges – Nodes + 2 = 7 – 6 + 2 = 3
Practice Question
Public boolean find(int key)
{ int bootom = 0;int top = elements.length-1; int last index = (bottom + top)/2; int mid;
boolean found = key == elements [lastIndex];
while ((bottom <= top) && !found )
{
mid = (bottom + top) / 2;
found = key == elements[mid];
if (found)
{
lastIndex = mid;
}
else
{
if (elements[mid] < key)
{
bottom = mid +1;
}
else
{
top = mid – 1;
}
}
}
Solution:
Paths:
1) 1-9
2) 1-2-9
3) 1-2-3-4-1-9
4) 1-2-3-5-6-1-9
5) 1-2-3-5-7-8-1-9