Jump to content

Simple php function working with Undefined offset ?


Ahmedoooov
Go to solution Solved by QuickOldCar,

Recommended Posts

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

 

Link to comment
Share on other sites

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 by QuickOldCar
Link to comment
Share on other sites

  • Solution

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;
    }
Link to comment
Share on other sites

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;
    }
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.