Ahmedoooov Posted January 29, 2016 Share Posted January 29, 2016 Hello! I'm starting to learn php and for practice I tried to resolve a simple exercise. the idea is to write a php function that inputs a tabe and returns +1 if positive numbers in the table are more than the negative ones. and return -1 if negative numbers in the table are more than the positive ones. 0 if they are equal. this is my code for the function starting from line 8 function plusmin ($tab) { $n = count($tab); $plus = 0; $min = 0; for ($i = 0; $i <= $n; $i++) { if ($tab[$i] > 0){ $plus++; } elseif ($tab[$i] < 0){ $min++; } } if ($plus > $min) { $result = "+1"; } elseif ($plus < $min) { $result = "-1"; } elseif ($plus == $min){ $result = "0"; } return $result; } and just to test it I created a table manually $tab[0] = 1; $tab[1] = -5; $tab[2] = -4; $tab[3] = 7; $tab[4] = -8; $tab[5] = -3; $tab[6] = 2; $tab[7] = 0; $tab[8] = -6; $tab[9] = -9; $k = plusmin($tab); echo $k; when I execute it It works, it shows for the case -1 but the browser shows this error Notice: Undefined offset: 10 in C:\Users\ahmed\PhpstormProjects\Exam 2013\tab.php on line 13 line 13 is this one if ($tab[$i] > 0){ Notice: Undefined offset: 10 in C:\Users\ahmed\PhpstormProjects\Exam 2013\tab.php on line 16 line 16 is this one elseif ($tab[$i] < 0){ I dunno what's the problem. and I know it will turn out to be a stupid problem but hey that's how we all learn. could anyone help? Thanks in advance Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted January 29, 2016 Share Posted January 29, 2016 (edited) function plusmin ($tab) { $n = (count($tab) -1);//this would be 0 to actual count in your loop, so deduct one, was adding extra $plus = 0; $min = 0; for ($i = 0; $i <= $n; $i++) { if ($tab[$i] > 0){ $plus++; } elseif ($tab[$i] < 0){ $min++; } } if ($plus > $min) { $result = "+1"; } elseif ($plus < $min) { $result = "-1"; } elseif ($plus == $min){ $result = "0"; } return $result; } Index key 10 does not exist Edited January 29, 2016 by QuickOldCar Quote Link to comment Share on other sites More sharing options...
Solution QuickOldCar Posted January 29, 2016 Solution Share Posted January 29, 2016 You could also loop for just less than $n value rather than less or equals $n function plusmin ($tab) { $n = count($tab); $plus = 0; $min = 0; for ($i = 0; $i < $n; $i++) { if ($tab[$i] > 0){ $plus++; } elseif ($tab[$i] < 0){ $min++; } } if ($plus > $min) { $result = "+1"; } elseif ($plus < $min) { $result = "-1"; } elseif ($plus == $min){ $result = "0"; } return $result; } Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted January 29, 2016 Share Posted January 29, 2016 I suppose will mention can use a foreach instead of counting the array and building the for loop function plusmin ($tab) { $plus = 0; $min = 0; foreach ($tab as $key=>$value) { if ($tab[$key] > 0){ $plus++; } elseif ($tab[$key] < 0){ $min++; } } if ($plus > $min) { $result = "+1"; } elseif ($plus < $min) { $result = "-1"; } elseif ($plus == $min){ $result = "0"; } return $result; } And for something as simple as this can just loop the array and use it's value. function plusmin ($tab) { $plus = 0; $min = 0; foreach ($tab as $value) { if ($value > 0){ $plus++; } elseif ($value < 0){ $min++; } } if ($plus > $min) { $result = "+1"; } elseif ($plus < $min) { $result = "-1"; } elseif ($plus == $min){ $result = "0"; } return $result; } Quote Link to comment Share on other sites More sharing options...
Ahmedoooov Posted January 29, 2016 Author Share Posted January 29, 2016 Thank you so much I knew it would turn out to be a stupid mistake yeah 'foreach' seems a better option than just 'for' 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.