Consider the following simple C program: Sum of first n natural numbers
#include <stdio.h>
int
main(int argc, char **argv)
{
int i;
int sum;
int n = 10;
sum = 0;
for (i = 1; i <= n; i++)
sum += i;
printf(Sum of first %d natural numbers is:
%d\n", n, sum);
return 0;
}
Any sequence of instructions in a program could be represented in terms of basic blocks, and a CFG could be drawn using those basic blocks. For the given C program:
Limitations: The current workspace can generate CFGs only for the main function for the above code. In other words, this would not work with user-defined functions. However, in real life a program would contain several modules. All such modules have to be taken into account while determining the complexity.
Consider the following simple C program: Initialize elements of a 2D array to 0
#include <stdio.h>
int
main(int argc, char **argv)
{
int a[5][10];
int i;
int j;
int nr;
int nc;
nr = 5;
nr = 5;
nc = 10;
for (i = 0; i <= nr; i++) {
for (j = 0; j <= nc; j++) {
a[i][j] = 0;
}
}
return 0;
}
Limitations: The current workspace can generate CFGs only for the main function for the above code. In other words, this would not work with user-defined functions. However, in real life a program would contain several modules. All such modules have to be taken into account while determining the complexity.
The following C program implements the binary search technique performed over an array of integers. This is an example of a non-trivial program that we often encounter in our lives.
#include <stdio.h>
int
main(int argc, char **argv)
{
int a[] = {2, 4, 6, 8, 10, 12, 14, 16};
int key = atoi(argv[1]);
int n=8;
int low = 0;
int high = n - 1;
int middle;
int flag = 0
while (low <= high) {
middle = (low + high) / 2;
if (key == a[middle]) {
printf("Found key %d at position %d\n", key, middle);
flag = 1;
}
else if (key > a[middle]) {
high = middle - 1;
}
else {
low = middle + 1;
}
if (flag)
break;
}
if (! flag)
printf("Key
%d was not found!\n", key);
return 0;
}
Learning Objectives:
Limitations: The current workspace can generate CFGs only for the main function for the above code. In other words, this would not work with user-defined functions. However, in real life a program would contain several modules. All such modules have to be taken into account while determining the complexity.
To determine McCabe's cyclomatic complexity, we observe that
E = # of edges = 7
N = # of nodes = 7 (including the ENTRY and EXIT nodes)
So, the cyclomatic complexity becomes:
V(G) = E - N + 2
= 7 - 7 + 2
= 2
From the above CFG we find that:
N = # of nodes = 15
E = # of edges = 19
So, the cyclomatic complexity becomes
V(G) = E - N + 2
= 19 - 15 + 2
= 6
Since V(G) is less than 10, the concerned program could be categorized as simple in terms of its complexity.