Ex3 Software Engineering Virtual Lab - IIT Kharagpur
Ex3 Software Engineering Virtual Lab - IIT Kharagpur
Select 3
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.
01 #include <stdio.h>
02
03 int
04 main(int argc, char **argv)
05 {
06 int a[] = {2, 4, 6, 8, 10, 12, 14, 16};
07 int key = atoi(argv[1]);
08 int n = 8;
09 int low = 0;
10 int high = n - 1;
11 int middle;
12 int flag = 0;
13
14 while (low <= high) {
15 middle = (low + high) / 2;
16 if (key == a[middle]) {
17 printf("Found key %d at position %d\n", key, middle);
18 flag = 1;
19 }
20 else if (key < a[middle]) {
21 high = middle - 1;
22 }
23 else {
24 low = middle + 1;
25 }
26 if (flag)
27 break;
28 }
29
30 if (! flag)
31 printf("Key %d was not found!\n", key);
32
33 return 0;
34 }
Determine the cyclomatic complexity of this program. How would you classify this program in terms of its complexity (simple,
complex, unstable)?
Limitations: The current workspace can generate CFGs only for the main function. 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.
Submit
1 #include <stdio.h>
2
3 int
4 main(int argc, char **argv)
5
6 {
7
8 int a[] = {2, 4, 6, 8, 10, 12, 14, 16};
9
10 int key = atoi(argv[1]);
Position: Ln 63, Ch 1 Total: Ln 63, Ch 635 resize
Result
View Solution
Sponsored by MHRD (NME-ICT) | Licensing Terms | Disclaimer Except otherwise noted, content on
this site is licensed under the CC-BY-
Copyright © 2010-2016 IIT Kharagpur NC-SA-3.0 License. See details.