Jump to content

Help with easy school assignment!


prohybe

Recommended Posts

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

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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++".

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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

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

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

Link to comment
Share on other sites

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!

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.