Jump to content

Number of paths


mpsn

Recommended Posts

Hi, I need some help with homework ques using the following function:

 

<?php
function getValue($x, $y, $z) {

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

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

return $z;
} ?>

 

Here is the question:

Provide a function, getValue(x y z), for the number of paths through

the program in terms of x, y, and z.

 

I'd appreciate any help, I think the ques is asking for a general formula, but I think the number of paths is just 4, b/c each

if-statement can be: T to F, T to T, F to F, F to T. I don't know...

Link to comment
Share on other sites

In this case it would be 4, but you have to be careful because that's not necessarily the case just because there are two if/else statements. For example, consider the following code:

 

// $x, $y, $z each given a true or false value

if($x) {
    $y = true;
} else {
    $z = true;
}

if($x || $z) {
    
} else {
    
}

 

In that case it would be impossible for the else statement on the second if statement to ever be called. If $x had an initial value of true, then the if statement is true, but if $x was false to begin with then $z gets set to true, and as a result the statement, $x || $z is still true.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.