Number of paths ques

closed account (4ET0pfjN)
Hi, for my assignment, for the given function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public int getValue(int x, int y, int z) {

 if (x != 0)
  y = 5;
 else
  z = z-x;

 if (z > 1)
  z = z/x;
 else
  z = y;

 return z;
}


the question is:
Provide a general function/formula for the number of paths through
the program in terms of x, y, and z.


I want to say 4 paths, but that is not what the ques is asking. I'd appreciate any help for this.


In general, the algorithm is:

a. Identify the basic blocks in the code being analysed http://en.wikipedia.org/wiki/Basic_block

b. Generate the control flow graph http://en.wikipedia.org/wiki/Control_flow_graph
closed account (4ET0pfjN)
Creating a control flow graph was actually the first part of this question and I did that already,
any other ideas?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int getValue( int x, int y, int z ) 
{
    
 if( x != 0 )
    y = 5;

 /* dead code from here */   
 else // x == 0 
    z = z-x ; // by constant folding, z = z-0 ; => dead code
 /* till here */
 
 /* getValue results in undefined behaviour if z > 1, and x ==0 */
 if( z > 1 )
  z = z/x ; // will lead to undefined behaviour if x==0 

 else // z < 2
  z = y;

 return z;

}


The invariant on entry is therefore:
1
2
3
4
if( x==0 ) z must be less than or equal to 1

if( x!= 0 ) a. z may be less than or equal to 1
            b. z may be greater than 1


Take it from there.
Topic archived. No new replies allowed.