prohybe Posted March 17, 2015 Share Posted March 17, 2015 Hello!I just started learning php at school and I tried to complete our first assignment but couldn't make it. The professor wasn't very helpful too. So this consists of two questions. I can't get any of them to work. Tried commenting both but none of them seem to work. I could really use some help! <h1>Class number 3 </h1> <h2>Arrays</h2> <h3>1. Determine whether or not your number is contained within an array.</h3> <?php function findNumber($arr, $num) { $size = count($arr); $i = 0; while ($i < $size) { if ($arr[$i] == $num) { return TRUE; } else { i++; } return FALSE; } } $arr = array(3, 8, 5, 1, 6, 7); $num = 6; if (findNumber($arr, $num)) { echo "Value $num belongs to the array.<br/>"; } else { echo "Value $num does not belong to the array.<br/>"; } ?> <h3>2. Determine the average of an array.</h3> <?php function avgArr($arr) { $size = count($arr); $sum = 0; for ($i = 0; $i < $size; $i++) { $sum = $sum + $arr[$i]; } return $avg = $sum / $size; } $myArray = array(1, 2, 3, 4, 5); echo "The average of your array is: " . avgArr($myArr); ?> Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/ Share on other sites More sharing options...
ginerjm Posted March 17, 2015 Share Posted March 17, 2015 Not that I am going to help you do your homework, regardless of how you feel about the instructor, but what doesn't work here? Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508356 Share on other sites More sharing options...
prohybe Posted March 17, 2015 Author Share Posted March 17, 2015 Not that I am going to help you do your homework, regardless of how you feel about the instructor, but what doesn't work here? We're supposed to learn with searches and stuff. I did my code alone, just can't find the mistake. I don't know what's wrong. I tried previewing it on a browser but nothing appears. Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508357 Share on other sites More sharing options...
ginerjm Posted March 17, 2015 Share Posted March 17, 2015 Searches and stuff? As in 'do your own research'? Asking for help on a forum is not what I would call research. Hints - a flaw in your logic in your while loop for one problem. A mis-type of something is your second problem which, if you had turned on error checking, would have been pointed out. Did your instructor not tell you about enabling error checking when developing? See my signature. Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508363 Share on other sites More sharing options...
cypher86 Posted March 17, 2015 Share Posted March 17, 2015 php has already a built-in function that does the trick. it's called in_array() http://php.net/manual/en/function.in-array.php Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508365 Share on other sites More sharing options...
prohybe Posted March 17, 2015 Author Share Posted March 17, 2015 Searches and stuff? As in 'do your own research'? Asking for help on a forum is not what I would call research. Hints - a flaw in your logic in your while loop for one problem. A mis-type of something is your second problem which, if you had turned on error checking, would have been pointed out. Did your instructor not tell you about enabling error checking when developing? See my signature. We get 30 minute lectures with some code on a PowerPoint just presenting the variables and loops. No examples, nothing. I had try again and again for this. Just coded this myself and i did in fact look for the correct usage of the while loop. And no, i did not hear about the error checking until just now. Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508366 Share on other sites More sharing options...
prohybe Posted March 17, 2015 Author Share Posted March 17, 2015 php has already a built-in function that does the trick. it's called in_array() http://php.net/manual/en/function.in-array.php Thank you! It works with that function, but in this stage i wanted to know what I have got wrong just to learn how to type the code! Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508367 Share on other sites More sharing options...
ginerjm Posted March 17, 2015 Share Posted March 17, 2015 And? Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508368 Share on other sites More sharing options...
prohybe Posted March 17, 2015 Author Share Posted March 17, 2015 And? Not going to argue. Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508369 Share on other sites More sharing options...
ginerjm Posted March 17, 2015 Share Posted March 17, 2015 My point was what happens when you turned on error checking? And did you find the glitch in your while loop that I pointed out? Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508370 Share on other sites More sharing options...
prohybe Posted March 17, 2015 Author Share Posted March 17, 2015 My point was what happens when you turned on error checking? And did you find the glitch in your while loop that I pointed out? So sorry, understood it the wrong way! I pasted your error checking code under the "<?php" line of code and refreshed the browser but nothing happened. About the while loop and don't really know if it has anything to do with the position of the "i++". Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508371 Share on other sites More sharing options...
ginerjm Posted March 17, 2015 Share Posted March 17, 2015 Can we see your new code in its entirety? Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508372 Share on other sites More sharing options...
cypher86 Posted March 17, 2015 Share Posted March 17, 2015 i think you got the parenthesis wrong while ($i < $size) { if ($arr[$i] == $num) { return TRUE; } else { i++; } return FALSE; } like this the while does 1 loop only. either find the number on the first hit or return false. should be more like $found=false while ($i < $size) { if ($arr[$i] == $num) { //return TRUE; $found=true; } else { i++; } //return FALSE; } return $found; this solution is not optimized because evaluate all the array even when not needed but does the trick Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508373 Share on other sites More sharing options...
ginerjm Posted March 17, 2015 Share Posted March 17, 2015 So much for letting the student learn how to debug his own errors! And the EASY correction was to simply move the return outside of the while loop! No need for code bloat. Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508375 Share on other sites More sharing options...
prohybe Posted March 17, 2015 Author Share Posted March 17, 2015 i think you got the parenthesis wrong while ($i < $size) { if ($arr[$i] == $num) { return TRUE; } else { i++; } return FALSE; } like this the while does 1 loop only. either find the number on the first hit or return false. should be more like $found=false while ($i < $size) { if ($arr[$i] == $num) { //return TRUE; $found=true; } else { i++; } //return FALSE; } return $found; this solution is not optimized because evaluate all the array even when not needed but does the trick Can we see your new code in its entirety? Thank you both for your help! It currently is like this (not working still): <h1>Class number 3 </h1> <h2>Arrays</h2> <h3>1. Determine whether or not your number is contained within an array.</h3> <?php error_reporting(E_ALL); ini_set('display_errors', '1'); function findNumber($arr, $num) { $size = count($arr); $i = 0; $found = false; while ($i < $size) { if ($arr[$i] == $num) { $found = true; } else { i++; } } return $found; } $arr = array(3, 8, 5, 1, 6, 7); $num = 6; findNumber($arr); if ($found == true) { echo "Value $num belongs to the array.<br/>"; } else { echo "Value $num does not belong to the array.<br/>"; } ?> <h3>2. Determine the average of an array.</h3> <?php error_reporting(E_ALL); ini_set('display_errors', '1'); function avgArr($arr) { $size = count($arr); $sum = 0; for ($i = 0; $i < $size; $i++) { $sum = $sum + $arr[$i]; } return $avg = $sum / $size; } $myArray = array(1, 2, 3, 4, 5); echo "The average of your array is: " . avgArr($myArr); ?> Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508376 Share on other sites More sharing options...
ginerjm Posted March 17, 2015 Share Posted March 17, 2015 Discard cycpher86's code and just study the original code. Re-read your second set of code until you see the typo you made. I already told you what had to change in the first problem. Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508378 Share on other sites More sharing options...
prohybe Posted March 17, 2015 Author Share Posted March 17, 2015 (edited) Discard cycpher86's code and just study the original code. Re-read your second set of code until you see the typo you made. I already told you what had to change in the first problem. I will do that, thank you. Just one last thing, I'm back to the original code and moved return False; outside the loop. So should this give me some output on the browser? Because I am not getting any. (I commented the whole 2nd question) <h1>Class number 3 </h1> <h2>Arrays</h2> <h3>1. Determine whether or not your number is contained within an array.</h3> <?php function findNumber($arr, $num) { $size = count($arr); $i = 0; while ($i < $size) { if ($arr[$i] == $num) { return TRUE; } else { i++; } } return FALSE; } $arr = array(3, 8, 5, 1, 6, 7); $num = 6; if (findNumber($arr, $num)) { echo "Value $num belongs to the array.<br/>"; } else { echo "Value $num does not belong to the array.<br/>"; } ?> /* <h3>2. Determine the average of an array.</h3> <?php function avgArr($arr) { $size = count($arr); $sum = 0; for ($i = 0; $i < $size; $i++) { $sum = $sum + $arr[$i]; } return $avg = $sum / $size; } $myArray = array(1, 2, 3, 4, 5); echo "The average of your array is: " . avgArr($myArr); ?> */ Edited March 17, 2015 by prohybe Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508379 Share on other sites More sharing options...
CroNiX Posted March 17, 2015 Share Posted March 17, 2015 (edited) The problem is when you run the function. findNumber($arr); Your function returns a value, but you aren't capturing it. You then try to use it here if ($found == true) { $found does not exist except inside of your function, so anything outside of the function doesn't know anything about it. This is called variable scope and I suggest you read up on it. To fix it, change findNumber($arr); to $found = findNumber($arr); Edited March 17, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508380 Share on other sites More sharing options...
cypher86 Posted March 17, 2015 Share Posted March 17, 2015 Discard cycpher86's code and just study the original code. Re-read your second set of code until you see the typo you made. I already told you what had to change in the first problem. right! follow the ginerjm way Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508381 Share on other sites More sharing options...
ginerjm Posted March 17, 2015 Share Posted March 17, 2015 Also your error reporting put the 1 in quotes. Remove them. Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508383 Share on other sites More sharing options...
Psycho Posted March 17, 2015 Share Posted March 17, 2015 On the first function, the problem is in the logic when the number is not found. Look at where the "return FALSE;" is located with respect to the loop Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508384 Share on other sites More sharing options...
prohybe Posted March 17, 2015 Author Share Posted March 17, 2015 Also your error reporting put the 1 in quotes. Remove them. The problem is when you run the function. findNumber($arr); Your function returns a value, but you aren't capturing it. You then try to use it here if ($found == true) { $found does not exist except inside of your function, so anything outside of the function doesn't know anything about it. This is called variable scope and I suggest you read up on it. To fix it, change findNumber($arr); to $found = findNumber($arr); @cronix i changed the $found thing! @ginerjm i'm sorry but i'm not totally getting the error checking. I pasted ur signature just like ini_set('display_erros'... are we supposed to take the " ' " off? anyway i did already check without it Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508385 Share on other sites More sharing options...
ginerjm Posted March 17, 2015 Share Posted March 17, 2015 So you got one problem working? The other you have an undefined var in your function call (the typo). Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508393 Share on other sites More sharing options...
prohybe Posted March 17, 2015 Author Share Posted March 17, 2015 So you got one problem working? The other you have an undefined var in your function call (the typo). Are you still refering to the 1st question? In the findNumber? Because the program for it is still not working. I'm going to try and fix the function then! Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508403 Share on other sites More sharing options...
ginerjm Posted March 17, 2015 Share Posted March 17, 2015 Fixing the function on #1 is key. Fixing the call on #2 is the other problem. Quote Link to comment https://forums.phpfreaks.com/topic/295325-help-with-easy-school-assignment/#findComment-1508404 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.