0

My code is below. I need to:

  1. Create a function for avg.
  2. Create a function for bigger.

This will help me repeat the process below for arrays $n3 and $n4. I have tried to create a function for avg but it is just not right. Please, look below:

INPUT:

$n1 = array(4, 14, 8, 3, 24);
$n2 = array(3, 1, 4, 7, 5);
$n3 = array(0, 30, 7, 25, 17);
$n4 = array(6, 2, 5, 4, 3);

for ($i = 0; $i < 5; $i++); {
    $average1[] = avg($n1[$i], $n2[$i]);
    $average2[] = avg($n3[$i], $n4[$i]);
    $bigger[] = bigger($average1[$i], $average2[$i]);

MY Output for function avg:

function avg($n1[$i], $n2[$i]) {
    $av = ($n1[i] + $n2[$i]) / 2;
    return $av;
}
echo $av;
1
  • I converted your options to numbered list so that it looks more attractive. I also indented your code so that it renders properly – please see the editing help for more information on formatting. Commented Mar 11, 2015 at 4:50

2 Answers 2

1

To find max value

max($your_array);

To find avg

function average($your_array) {
 return array_sum($your_array) / count($your_array);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I like @user1844933 solution, but if for some weird or student purpose reason you need to create thes two functions byiterating the array, this can be the solution

<?php
$n1 = array(4, 14, 8, 3, 24);
$n2 = array(3, 1, 4, 7, 5);
$n3 = array(0, 30, 7, 25, 17);
$n4 = array(6, 2, 5, 4, 3);

function bigger($array) {
    $result = 0;
    foreach($array as $num) {
        if($num > $result) {
            $result = $num;
        }
    }
    return $result;
}

function average($array) {
    $result = 0;
    foreach($array as $num) {
        $result += $num;
    }
    return $result / count($array);
}

echo "<h2>n1</h2>";
echo "Average: ".average($n1)."<br>";
echo "Bigger: ".bigger($n1)."<br>";

echo "<h2>n2</h2>";
echo "Average: ".average($n2)."<br>";
echo "Bigger: ".bigger($n2)."<br>";

echo "<h2>n3</h2>";
echo "Average: ".average($n3)."<br>";
echo "Bigger: ".bigger($n3)."<br>";

echo "<h2>n4</h2>";
echo "Average: ".average($n4)."<br>";
echo "Bigger: ".bigger($n4)."<br>";

EDIT I am not checking if arrays has different amount of elements. I assumed both arrays always have the same amount of elements

<?php
$n1 = array(4, 14, 8, 3, 24);
$n2 = array(3, 1, 4, 7, 5);
$n3 = array(0, 30, 7, 25, 17);
$n4 = array(6, 2, 5, 4, 3);

function average($array1, $array2) {
    $result = 0;
    $result ="";
    for($i=0; $i < count($array1); $i++) {
        $result .= "Average Between: ". $array1[$i] . " and " . $array2[$i] . " is " . ($array1[$i] + $array2[$i]) /
            2 . "<br>";
    }
    return $result;
}

echo "<h2>n1 & n2</h2>";
echo average($n1, $n2)."<br>";

echo "<h2>n3 & n4</h2>";
echo average($n3, $n4)."<br>";

EDIT 2

The SO user ask for this function in the comment.

function table($array) {
    $table = '<table style="border:1px solid #000;">';
    foreach($array as $row) {
        $table .= "<tr><td style='border:1px solid #000;'>$row</td></tr>";
    }
    $table .= '<table>';

    return $table;
}

7 Comments

Thanks, you are right in assuming we need to create our own functions.I was trying to write my function avg() based on what you suggested, and came up with this .Like I mentioned, the expectation for the avg function is that you add the first item from the first array with the first item from the second array and divide it by two to get the average. The process needs to be repeated for each item in both the arrays.Please tell me how to rectify my code below?
function avg($n1,$n2){ $ result=0; foreach($n1 as $num1){ foreach($n2 as $num2){ $result=$num1+$num2 ; } return $result/count($n1,$n2) ; } } echo $result;
@newbie I am not sure exactly what you are asking for. However, I edited my answer. I hope I guessing correctly what you meant.
your code kind of works for me and i am trying to rectify a small error.thanks so much. I am also trying me to create a function to print a table(like a html table for the output of my array). Any thoughts on this ?
sure, what do you want to display in that table?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.