Jump to content

Working with functions/array's/max/min/matching


Recommended Posts

I attempted to try to do what was in the comments, but I am not able to do it :( any help is appreciated

 

<?php
				$a = array(9, 3, 1, 0, 99, 2, 5, 6, 32, 1, 55);
				$b = array(9, 18, 1, 0, 23, 22, 4, 6, 5, 32, 55);

				function getAverage () {
					echo "The average is " . array_sum($a) . "\n"; // Show the sum of the entire array (should display 19.36)
				}
				getAverage();
				echo "<br><br>";

				function highestValue() {
					echo $a[max]; // Show the index value of the highest number (should display 4)
					echo $a(max); // Show the value of index 4 (should display 99)
					echo $a[min]; // Show the index value of lowest number (should display 3)
					echo $a(min); // Show the value of index 4 (should display 0)
				}
				highestValue();
				echo "<br><br>";

				function displayMatch() {
					//Write a function that tests the values of both arrays. If the values at the same index match display the following: "The values at <insert the matching index> match."
				}
				displayMatch();
?>

Start by reading the manual entries for array_sum, min and max. HINT: array_sum() doesn't return an average, and your syntax is wrong for min() and max().

 

EDIT While you're reading the manual, look at the list of array functions in the left sidebar, and see which one sounds like it would be used to find values that exist in more than one array for your last function.

We will not do your homework for you, and repeated attempts to get us to help you circumvent your own education (which you or your parents are paying an obscene amount of money for) will likely result in a ban.  We'd be happy to give you pointers, like Pikachu did, but show us that you've put forth at least a token effort first.

We will not do your homework for you, and repeated attempts to get us to help you circumvent your own education (which you or your parents are paying an obscene amount of money for) will likely result in a ban.  We'd be happy to give you pointers, like Pikachu did, but show us that you've put forth at least a token effort first.

 

Of course, btw I am not asking for answer's. I would like answer's like how Pikachu answerd me. That is perfect, I am going to look into that and re-post back here my results.

Ok I am going to tackle this piece by piece...

 

<?php
				$a = array(9, 3, 1, 0, 99, 2, 5, 6, 32, 1, 55);

				function getAverage () {
					echo "The average is " . array_sum($a) . "\n";
				}
				getAverage();
?>

 

I know that array_sum doesn't return the average, it returns the sum of the array.  But even that is not displaying the total why? if I display the following line of code outside the function

 

<?php
echo "The average is " . array_sum($a) . "\n";
?>

 

It works, but if I write it inside the function, it does not work why? And to get the average, what is the term for diving by the number of value's in the array?

Wow that's really interesting!

<?php
                                        $a = array(9, 3, 1, 0, 99, 2, 5, 6, 32, 1, 55);

				function getAverage () {
					global $a;
					echo "The average is " . array_sum($a) . "\n";
				}
				getAverage();
?>

 

I tried do divide that number by the amount of value's in the array like the following

 

<?php
echo "The average is " . array_sum($a)/11 . "\n"; // try 1

echo "The average is " . array_sum($a) . "\n"; // try 2
echo "array_sum($a)/11"
?>

 

or is there some sort of code that knows how many elements are in the array? And take the array_sum and divide it by that.

This is because of variable scope. With the exception of the globals/superglobals, values that are not assigned within a function, need to be passed to the function as parameters to make them available within it.

 

It's also better not to echo from within a function, but to return the value from the function for use by the script.

Variable scope is a simple concept but can be hard to understand at first if you have preconceived ideas that run counter to how it actually operates. Here are a couple of examples that should help

 

function echoValue1()
{
    echo "The value is: " . $a;
}

function echoValue2($var)
{
    echo "The value is: " . $var;
}

$a = 'foo';

echoValue1($a); //Output: 'The value is: '
echoValue2($a); //Output: 'The value is: foo'

I just did several google searches for queries related to php functions to find the number of elements in an array. And each one found the exact manual page to the function you need. Heck, I even used your exact text from above "how many elements are in the array" along with PHP and the first result is the answer you need.

 

By the way, do not use global. Instead pass the variable to your function.

Thank you, I was able to do it :)

<?php
            echo "The average is " . array_sum($a)/count($a) . "\n";
?>

 

As for the max and min's I know how to find the actual highest and lowest, but how would u indicate the index of the highest and lowest?

<?php
				$a = array(9, 3, 1, 0, 99, 2, 5, 6, 32, 1, 55);

				echo max($a);
				echo "<br>";
				echo min($a);
?>

Another problem

 

I am having same trouble with scope for displaying max within a function, I tried global and return, and my output is ----> max(Array), it should show 99

 

<?php
                                        $a = array(9, 3, 1, 0, 99, 2, 5, 6, 32, 1, 55);

				function highestValue () {
					global $a;
					echo "The highest value is at index: (insert index), The value at index (insert index) is: max($a)";
				}
				highestValue();
?>

OUTPUT: The highest value is at index: (insert index), The value at index (insert index) is: max(Array) 

 

 

I still don't know the code to display the index also

You're telling it to literally echo "max($a)". You need to concatenate the function to the string, or assign the result of the function to a variable before the string, and use that variable in the string.

You're telling it to literally echo "max($a)". You need to concatenate the function to the string, or assign the result of the function to a variable before the string, and use that variable in the string.

 

Sorry, but I dont understand what u mean by concentrating the function to the string.

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.