Complexity Examples


发布时间:2017-03-23 来源:本站原创 作者:本站编辑   


Complexity Examples

1.

  Derive the flow graph for the pseudo code below and calculate McCabe's Cyclomatic metric.    

1   PROCEDURE search

2   BEGIN

3       initialise pointer

4       initialise flags

5       DO WHILE  last element = false AND   found = false

6       BEGIN

7               read element and check against key

 8              IF element found THEN

9               BEGIN

10                  FOUND = true

11             END

12             ELSE

13             BEGIN

14                IF last element THEN

15                BEGIN  

16                   last element = true

17                 END

18                 ELSE

19                 BEGIN

20                    INCREMENT POINTER

21                  END

22             END

23       END(while loop)

27     END(procedure)

 

 

 

2.

 

Compare the complexities of the following two pieces of code using the LOC and cyclomatic-complexity metrics. What conclusions can you draw about the relative complexity of the code?

 

Code A                                                                              Code B

 inti=1;                                                                                intj=0;

 while(i<=5){                                                                      inti= 2;       

    playACard(i);                                                                  j = i;    

   (playerHasWon(i))                                                         j = j + i;

          break;                                                                        j++;

    i++                                                                                   System.out.println (j);                                                                    }                                                                                           System.out.println (i);