views:

279

answers:

3

I am creating an array named "fifty" with 50 elements.

Instruction #1:Initialize the array so that the first 25 elements are equal to the square of the index/key variable.

When squaring we are raising a base or number to some power so in PHP
we need some function like this:

<?php
    print pow(10,2); // Squared raised to the 2nd power = 100 
    print pow(10,3); // Cubed raised to the 3rd power = 1000
    print pow(10,4); // 10000
    print pow(-10, 4); // 10000
?>

Solved....

+3  A: 
John Kugelman
Thank you, that really clarified in what context square is used in php and what square means in php.
Newb
Now I know how to use square in a php array, I guess next time I will clarify my questions so as to avoid the above, and the flamers that have nothing useful to add other than just to type random crap.
Newb
+3  A: 

Square is a common mathematical term meaning "to the power of two"

For the second last 25 values they want the value in the array to be equal to the key * 3. So if the key was 145, the value would be 435.

Also, I weep for the education system that's teaching someone to program before teaching them reading comprehension.

Alan Storm
Okay I have no idea what reading comprehension has to do with this?I just wanted to understand the instructions intimately, so as to not mess up, call me picky about my results.
Newb
Define reading comprehension?
Newb
+1  A: 

This is very basic stuff. It's early in the morning where I'm at right now so nothing else to do. Here the answer to:

Instruction #1:Initialize the array so that the first 25 elements are equal to the square of the index/key variable.

<?php
$myarray = array();
for($i=0; $i<25; $i++) {
  $myarray[] = $i * $i;  // squaring
}

var_dump($myarray);  // output to screen
?>
Yada
Disclaimer, I am new to PHP: Is there a reason why I can't use the pow() function?You response is pretty elegant and I take it you have been programming for a while, I am just learning and this stuff is all new to me, thank you for the response.
Newb
It's okay. I understand that you're new to programming. Everybody starts as a newbie. Sometime I do think stackoverflow community doesn't welcome n00bs.You can use the pow function if you want.replace$myarray[] = $i * $i;with with:$myarray[] = pow($i, 2); // squaring
Yada
Thank you, just got me some really good books on PHP:Got Andy Harris's PHP 6 in addition, to Larry Ullman's PHP for the Web pdf-really good books. All this stuff about loops and arrays is cool stuff, especially when working with data structures.Next time I will read first and try really hard to solve the problem myself before posting questions here, since all the answers seem to be in the text book, and man I really enjoy PHP with this guy Andy Harris and Larry Ullman, the examples and explanations are phenomenal.
Newb
for ($i=0; $i<count[$myarray]; $i++) is supposed to be for ($i = 0; $i < count($myarray); $i++), cause [] is reserved for index keys, my bad...
Newb