Jump to content

prohybe

Members
  • Posts

    10
  • Joined

  • Last visited

prohybe's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. 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!
  2. @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
  3. 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); ?> */
  4. 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); ?>
  5. 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++".
  6. 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!
  7. 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.
  8. 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.
  9. 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); ?>
×
×
  • 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.