0% found this document useful (0 votes)
869 views

Cyclomatic Complexity-Practice Questions

The document contains code to calculate the cyclomatic complexity of a program. It shows a while loop code snippet and calculates its cyclomatic complexity as 5 using three different methods: number of enclosed areas plus 1, number of simple predicates plus 1, and edges minus nodes plus 2. It then shows another code example and calculates its cyclomatic complexity. The document explains independent paths to determine cyclomatic complexity. It shows 5 independent paths through a flow graph to calculate the complexity as 5. It also contains practice questions and solutions for calculating cyclomatic complexity.

Uploaded by

marrisha26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
869 views

Cyclomatic Complexity-Practice Questions

The document contains code to calculate the cyclomatic complexity of a program. It shows a while loop code snippet and calculates its cyclomatic complexity as 5 using three different methods: number of enclosed areas plus 1, number of simple predicates plus 1, and edges minus nodes plus 2. It then shows another code example and calculates its cyclomatic complexity. The document explains independent paths to determine cyclomatic complexity. It shows 5 independent paths through a flow graph to calculate the complexity as 5. It also contains practice questions and solutions for calculating cyclomatic complexity.

Uploaded by

marrisha26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Example

while(value[i] != -999.0 && totinputs < 100)


{ totinputs++;
if(value[i] >= min && value[i] <= max)
{ totvalid++;
sum = sum + value[i];
}
i++;
}
●Cyclomatic Complexity:
V(G) = number of enclosed areas + 1 = 5
V(G) = number of simple predicates + 1 = 5
V(G) = edges - nodes + 2 = 10 - 7 + 2 = 5
Cyclomatic Complexity by Graph Matrix
1 2 3 4 5 6 7 Calculations
1 x x =2 = 2-1 = 1

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy