Serigne Posted December 7, 2022 Share Posted December 7, 2022 Hello I have a little problem with this exercise I managed to access the elements of the arrays but I can't display the results for: echo "Expression 1 evaluates to: " . evaluate($expression1) . " <br>"; echo "Expression 2 evaluates to: " . evaluate($expression2) . " <br>"; echo "Expression 3 evaluates to: " . evaluate($expression3) . " <br>"; Need help please <?php function evaluate($expression){ // TODO : add rendering code here $test0 = $expression['children'][0]['value']; $test1 = $expression['children'][1]['value']; $test2 = $expression['children'][2]['value']; return $test0+$test1+$test2; $exp = $expression['children'][0]['value']; $exp1 = $expression['children'][1]['children'][0]['value']; $exp2 = $expression['children'][1]['children'][1]['children'][0]['value']; $exp3 = $expression['children'][1]['children'][1]['children'][1]['value']; return ($exp+$exp1 * ($exp2 + $exp3)); $resp = $expression['children'][0]['value']; $resp1 = $expression['children'][1]['top']['value']; $resp2 = $expression['children'][1]['bottom']['value']; return $resp + $resp1 / $resp2; } // 100 + 200 + 300 $expression1 = [ "type" => "add", 'children'=> [ [ "type" => "number", "value"=>100 ], [ "type" => "number", "value"=> 200 ], [ "type" => "number", "value"=> 300 ] ] ]; $expression2 = [ "type" => "add", 'children'=> [ [ "type" => "number", "value"=>100 ], [ "type" => "multiply", "children" =>[ [ "type" => "number", "value"=>2 ], [ "type" => "add", "children" =>[ [ "type" => "number", "value"=>5 ], [ "type" => "number", "value"=>45 ] ] ] ] ] ] ]; $expression3 = [ "type" => "add", 'children'=> [ [ "type" => "number", "value"=>1 ], [ "type" => "fraction", "top"=> [ "type" => "number", "value"=>100 ], "bottom"=> [ "type" => "number", "value"=>1000 ] ] ] ]; echo "Expression 1 evaluates to: " . evaluate($expression1) . " <br>"; echo "Expression 2 evaluates to: " . evaluate($expression2) . " <br>"; echo "Expression 3 evaluates to: " . evaluate($expression3) . " <br>"; Quote Link to comment Share on other sites More sharing options...

Barand Posted December 7, 2022 Share Posted December 7, 2022 Only the first 4 lines of your function will ever execute - a function exits on meeting a return statement and returns that value. Quote Link to comment Share on other sites More sharing options...

Serigne Posted December 7, 2022 Author Share Posted December 7, 2022 11 minutes ago, Barand said: Only the first 4 lines of your function will ever execute - a function exits on meeting a return statement and returns that value. Thank you for your answer, what do you suggest as a solution? Quote Link to comment Share on other sites More sharing options...

Barand Posted December 7, 2022 Share Posted December 7, 2022 I don't know what the original problem is that your code was supposed to solve, so I have no solution. Quote Link to comment Share on other sites More sharing options...

Serigne Posted December 7, 2022 Author Share Posted December 7, 2022 3 minutes ago, Barand said: I don't know what the original problem is that your code was supposed to solve, so I have no solution. I think the problem is the return I don't know if i change it by ECHO or not Quote Link to comment Share on other sites More sharing options...

ginerjm Posted December 7, 2022 Share Posted December 7, 2022 An echo statement sends data to the client screen usually. A function, given a set of arguments to work with, does something for you and literally returns a result in a certain format to the calling code. That mainline code then continues on with that value(s) where it may be used further or actually output, using an echo perhaps. Quote Link to comment Share on other sites More sharing options...

Serigne Posted December 7, 2022 Author Share Posted December 7, 2022 The exrcise is here: Implement the 'evaluate()' function in the following program to calculate the result of arbitrary expressions, such as the ones provided in ﬁle exercice-1.php. Output will look like this : <?php function evaluate($expression){ // TODO : add rendering code here } // 100 + 200 + 300 $expression1 = [ "type" => "add", 'children'=> [ [ "type" => "number", "value"=>100 ], [ "type" => "number", "value"=> 200 ], [ "type" => "number", "value"=> 300 ] ] ]; $expression2 = [ "type" => "add", 'children'=> [ [ "type" => "number", "value"=>100 ], [ "type" => "multiply", "children" =>[ [ "type" => "number", "value"=>2 ], [ "type" => "add", "children" =>[ [ "type" => "number", "value"=>5 ], [ "type" => "number", "value"=>45 ] ] ] ] ] ] ]; $expression3 = [ "type" => "add", 'children'=> [ [ "type" => "number", "value"=>1 ], [ "type" => "fraction", "top"=> [ "type" => "number", "value"=>100 ], "bottom"=> [ "type" => "number", "value"=>1000 ] ] ] ]; echo "Expression 1 evaluates to: " . evaluate($expression1) . " <br>"; echo "Expression 2 evaluates to: " . evaluate($expression2) . " <br>"; echo "Expression 3 evaluates to: " . evaluate($expression3) . " <br>"; 27 minutes ago, ginerjm said: An echo statement sends data to the client screen usually. A function, given a set of arguments to work with, does something for you and literally returns a result in a certain format to the calling code. That mainline code then continues on with that value(s) where it may be used further or actually output, using an echo perhaps. Quote Link to comment Share on other sites More sharing options...

ginerjm Posted December 7, 2022 Share Posted December 7, 2022 Ok - ran your code and you have a data structure problem. Plus your function is FU. You can only execute ONE return statement in a function. As you have been told, once you return from a function that's it. Your first expression data works just fine with your function, using the first return of course. Your 2nd expression data is different than the first one. You have a 'children' array inside of the 2nd children element which your first one does not have. Your code fails when trying to navigate thru that data. Here's what the script gives to me: ( I have error checking turned on, hence the error message): Expression 1: Array ( [type] => add [children] => Array ( [0] => Array ( [type] => number [value] => 100 ) [1] => Array ( [type] => number [value] => 200 ) [2] => Array ( [type] => number [value] => 300 ) ) ) Expression 1 evaluates to: 600 Expression 2: Array ( [type] => add [children] => Array ( [0] => Array ( [type] => number [value] => 100 ) [1] => Array ( [type] => multiply [children] => Array ( [0] => Array ( [type] => number [value] => 2 ) [1] => Array ( [type] => add [children] => Array ( [0] => Array ( [type] => number [value] => 5 ) [1] => Array ( [type] => number [value] => 45 ) ) ) ) ) ) ) Notice: Undefined index: value in /home/albany/public_html/homejg/test.php on line 95 Notice: Undefined offset: 2 in /home/albany/public_html/homejg/test.php on line 96 Notice: Trying to access array offset on value of type null in /home/albany/public_html/homejg/test.php on line 96 Expression 2 evaluates to: 100 Expression 3: Array ( [type] => add [children] => Array ( [0] => Array ( [type] => number [value] => 1 ) [1] => Array ( [type] => fraction [top] => Array ( [type] => number [value] => 100 ) [bottom] => Array ( [type] => number [value] => 1000 ) ) ) ) Notice: Undefined index: value in /home/albany/public_html/homejg/test.php on line 95 Notice: Undefined offset: 2 in /home/albany/public_html/homejg/test.php on line 96 Notice: Trying to access array offset on value of type null in /home/albany/public_html/homejg/test.php on line 96 Expression 3 evaluates to: 1 This is line 95: $test1 = $expression['children'][1]['value']; Quote Link to comment Share on other sites More sharing options...

mac_gyver Posted December 7, 2022 Share Posted December 7, 2022 the nested data structures and the statement of 'arbitrary expressions' indicates that you are expected to write a recursive function to do this and since you wouldn't have been given this assignment without first having covered recursive functions, what information has been covered prior to this assignment? Quote Link to comment Share on other sites More sharing options...

Barand Posted December 7, 2022 Share Posted December 7, 2022 In addition to being recursive, your evaluate() function needs to be able to deal with each expression type and calculate thier results. Something like this to get you started (I've done the type='number' case for you) function evaluate($exp) { switch ($exp['type']) { case 'number': $result = $exp['value']; break; case 'add': $result = ???; break; case 'multiply': $result = ???; break; case 'fraction': $result = ??? break; } return $result; } Quote Link to comment Share on other sites More sharing options...

Serigne Posted December 8, 2022 Author Share Posted December 8, 2022 18 hours ago, Barand said: In addition to being recursive, your evaluate() function needs to be able to deal with each expression type and calculate thier results. Something like this to get you started (I've done the type='number' case for you) Thank's for your answer so for doing the same for add i use: $result = $exp['value']+$exp['value'] or i use $exp['children']['value']? I'm trying to resolve this function i just wont to improve my PHP. Just give me a incation for add please Quote Link to comment Share on other sites More sharing options...

Solution Barand Posted December 8, 2022 Solution Share Posted December 8, 2022 OK - here's the "add" case... case 'add': $result = 0; foreach ($exp['children'] as $child) { $result += evaluate($child); } break; Quote Link to comment Share on other sites More sharing options...

Serigne Posted December 8, 2022 Author Share Posted December 8, 2022 49 minutes ago, Barand said: OK - here's the "add" case... 😔I'm ashamed to say that I can't Quote Link to comment Share on other sites More sharing options...

Serigne Posted December 8, 2022 Author Share Posted December 8, 2022 <?php function evaluate($expression){ // TODO : add rendering code here switch ($expression['type']) { case 'number': $result = $expression['value']; break; case 'add': $result = 0; foreach ($expression['children'] as $child) { $result += evaluate($child); } break; case 'multiply': $result = 1; foreach ($expression['children'] as $child) { $result *= evaluate($child); } break; case 'fraction': $result = evaluate($expression['top']) / evaluate($expression['bottom']); break; } return $result; } // 100 + 200 + 300 $expression1 = [ "type" => "add", 'children'=> [ [ "type" => "number", "value"=>100 ], [ "type" => "number", "value"=> 200 ], [ "type" => "number", "value"=> 300 ] ] ]; $expression2 = [ "type" => "add", 'children'=> [ [ "type" => "number", "value"=>100 ], [ "type" => "multiply", "children" =>[ [ "type" => "number", "value"=>2 ], [ "type" => "add", "children" =>[ [ "type" => "number", "value"=>5 ], [ "type" => "number", "value"=>45 ] ] ] ] ] ] ]; $expression3 = [ "type" => "add", 'children'=> [ [ "type" => "number", "value"=>1 ], [ "type" => "fraction", "top"=> [ "type" => "number", "value"=>100 ], "bottom"=> [ "type" => "number", "value"=>1000 ] ] ] ]; echo "Expression 1 evaluates to: " . evaluate($expression1) . " </br>"; echo "Expression 2 evaluates to: " . evaluate($expression2) . " <br>"; echo "Expression 3 evaluates to: " . evaluate($expression3) . " <br>"; Thank to all for your intervention and special thank to @Barand who help me for solution: here the code for someone interesting Quote Link to comment Share on other sites More sharing options...

Barand Posted December 8, 2022 Share Posted December 8, 2022 My full explanation... Let's go right back to basics. If you have an array of numbers $arr = [ 100, 200, 300 ]; the to get the sum of those numbers you would start with a 0 total then loop through the array adding each of the numbers to the total. $total = 0; foreach ($arr as $num) { $total += $num; } echo $total; // 600 $expression1 is a similar array but this time each item of the children array is an expression which has to be evaluated. $expression1 = [ "type" => "add", 'children'=> [ [ "type" => "number", "value"=>100 ], [ "type" => "number", "value"=> 200 ], [ "type" => "number", "value"=> 300 ] ] ]; So we start with a basic evaluate() function which knows how to evaluate a number experession and how to handle $expression1 (an add expression)... function evaluate($exp) { switch ($exp['type']) { case 'number': $result = $exp['value']; break; case 'add': $result = 0; foreach ($exp['children'] as $child) { $result += evaluate($child); } break; } return $result; } We can now call echo evaluate($expression1); This initial call to the function looks at the expression type and sees it is "add", so it sets the result to 0 and loops through the children to add each one to the result. To do this it has to call evaluate($child) for each one to get the number value to be added. The multiply is very similar to the add except we must start with a result of 1 (otherwise we just multiply everything by 0, resulting in 0). This is the full evalute() function... function evaluate($exp) { switch ($exp['type']) { case 'number': $result = $exp['value']; break; case 'add': $result = 0; foreach ($exp['children'] as $child) { $result += evaluate($child); } break; case 'multiply': $result = 1; foreach ($exp['children'] as $child) { $result *= evaluate($child); } break; case 'fraction': $result = evaluate($exp['top']) / evaluate($exp['bottom']); break; } return $result; } giving Expression 1 evaluates to 600 Expression 2 evaluates to 200 Expression 3 evaluates to 1.1 Quote Link to comment Share on other sites More sharing options...

ginerjm Posted December 8, 2022 Share Posted December 8, 2022 That Barand guy is pretty good, isn't he? Quote Link to comment Share on other sites More sharing options...

## Recommended Posts

## Join the conversation

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