Jump to content

Ahmedoooov

New Members
  • Posts

    2
  • Joined

  • Last visited

Ahmedoooov's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Thank you so much I knew it would turn out to be a stupid mistake yeah 'foreach' seems a better option than just 'for'
  2. 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
×
×
  • 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.