Exercise 1 Software Engineering Virtual Lab - IIT Kharagpur
Exercise 1 Software Engineering Virtual Lab - IIT Kharagpur
Select 1
Identification of basic blocks from a program and determining it's cyclomatic complexity
01 // Sum of first n natural numbers (not in the best possible way though)
02
03 #include <stdio.h>
04
05 int
06 main(int argc, char **argv)
07 {
08 int i;
09 int sum;
10 int n = 10;
11
12 sum = 0;
13 for (i = 1; i <= n; i++)
14 sum += i;
15
16 printf("Sum of first %d natural numbers is: %d\n", n, sum);
17
18 return 0;
19 }
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:
1. Identify the basic blocks and verify whether your representation matches with the output produced after compiling your
program
2. Draw a Control Flow Graph (CFG) using these basic blocks. Again, verify how the CFG generated after compilation relates to
the basic blocks identified by the compiler
3. Calculate McCabe's complexity from the CFG so obtained
Note that gcc translates the high-level program into an intermediate representation using GIMPLE. So, the CFG generated from your
code would not show the actual instructions.
Learning Objectives:
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
5 main(int argc, char **argv) {
6
7 int i;
8
9 int sum;
10
Position: Ln 21, Ch 11 Total: Ln 21, Ch 183 resize
<bb 2>:
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.