Home>

### php - how to find the average using an array

I'm new to programming.
Define a user-defined function getAryAve that receives an array and the number of elements as arguments and returns the average of the elements of the received array. In the main program, getAryAve is used to calculate the average of multiple numerical values ​​input from the standard input. Display. The numerical value input from the standard input is stored in the array. The maximum number of numerical values ​​that can be entered in the array is 10, and it is repeated until the number of records reaches 10 or the EOF is read.

``The last value comes out (?)``
Corresponding source code
``````<? php
function getAryAve (\$x, \$y) {
\$h = new SplFixedArray (\$y);
\$total = 0;
for (\$i = 0;\$i<\$y;\$i ++) {
\$h [\$i] = \$x;
\$total + = \$x;
}
\$ave = \$total/\$y;
return \$ave;
}
// main
print ('Enter a number');
\$a = trim (fgets (STDIN));
\$i = 0;
while (\$a! = NULL and \$i<10) {
print ('Enter a number');
\$a = trim (fgets (STDIN));
\$i ++;
}
print ('The average'. getAryAve (\$a, 10). "\ N");``````
• Answer # 1

``````<? php
function getAryAve (\$x, \$y) {
\$total = 0;
for (\$i = 0;\$i<\$y;\$i ++) {
\$total + = \$x [\$i];
}
\$ave = \$total/\$y;
return \$ave;
}
// main
\$values ​​= [];
print ('Enter a number');
\$a = trim (fgets (STDIN));
\$values ​​[] = \$a;
while (\$a! = NULL and count (\$values)<10) {
print ('Enter a number');
\$a = trim (fgets (STDIN));
\$values ​​[] = \$a;
}
print ('The average is'. GetAryAve (\$values, count (\$values)). "\ n");``````

• Answer # 2

It's a program that you don't know what you're doing.

First of all, "Sequentially store the input numerical values ​​in the array,`var_dump (\$array name);`Please make a program called "Display the data with."
The array is`SplFixedArray`Instead of using an ordinary array.

• Answer # 3

It's \$a, but it just holds the last entered value and discards any previous entries.
In getAryAve (), an array is created, but \$x is set for all elements.

In the first place

Receives an array and the number of elements as arguments

However, when \$x is not an array, the request is not satisfied.

Was it designed before the code was written?
Did you think about what each variable stores?
If you are a beginner, please make it clear by writing out the above items before writing the code.

• Answer # 4

Since there seem to be some problems, it seems better to postpone the calculation of the average and aim at "store up to 10 input numbers in the array" first.

``````print ('Enter a number');
\$a = trim (fgets (STDIN));
\$i = 0;
while (\$a! = NULL and \$i<10) {
print ('Enter a number');
\$a = trim (fgets (STDIN));
\$i ++;
}
var_export (\$a);``````

Last`var_export ()`So, it's OK if all the entered numbers are displayed, but this code doesn't work.
Why

`\$a`Is not an array

`while`Every time the loop goes around, the previous input value is overwritten.

Because.

Prepare an array separately and go there`\$a`It is necessary to post (add) the contents of.

``````\$values ​​= [];// Accumulate in this array.
print ('Enter a number');
\$a = trim (fgets (STDIN));
\$i = 0;
while (\$a! = NULL and \$i<10) {
\$values ​​[] = \$a;// Accumulation.
print ('Enter a number');
\$a = trim (fgets (STDIN));
\$i ++;
}
var_export (\$values);``````

This still has problems, but first try moving it yourself.