SDP2006
05-20-2004, 04:00 PM
Is there any way I could loop through a * to produce something like this?*
**
***
****
*****
******
*******
********
*********
********
*******
******
*****
***
**
*Thanks. I'm pretty sure it is possible, but *I'm* not sure how to achieve it.
Thanks
Spookster
05-20-2004, 04:50 PM
I had to do that in both C++ and Java in college for a homework question. The coding for it is simple. It's the use of loops which is what is meant to be taught here. This is for school isn't it?
Can be done with just two lines of code :
for ($i=1;$i<=10;$i++){ $output .= str_repeat('*', $i) . '<br />'; }
for ($i=9;$i>=1;$i--){ $output .= str_repeat('*', $i) . '<br />'; }
Another education wasted?
SDP2006
05-20-2004, 05:13 PM
This is for school isn't it?lol, no. I just got outta school for the summer yesterday and I am bored.
Raf, i get this Notice: Undefined variable: output in c:\server\htdocs\asterisk.php on line 4 I returns the asterisks, but thats at the top! Dunno, why. im using this<?php
for ($i=1;$i<=10;$i++)
{
$output .= str_repeat('*', $i) . '<br />';
}
for ($i=9;$i>=1;$i--){
$output .= str_repeat('*', $i) . '<br />';
}
echo $output;
?>Thanks, bud!
Spookster
05-20-2004, 07:45 PM
Although that may work that's not the way that problem was meant to be answered. It was meant to teach nested loops.
gsnedders
05-20-2004, 08:44 PM
lol, no. I just got outta school for the summer yesterday and I am bored.
I cannot belive that, I've got until the beginning of July...
As for it not working, I sometimes have problems copying code from here, try typing it out...
mordred
05-20-2004, 10:35 PM
Example with nested loops:
<xmp>
<?php
function test($length) {
for ($i = 1; $i <= $length; $i++) {
for ($k = 1; $k <= $i; $k++) {
print '*';
}
print "\n";
}
for ($i = $i - 2; $i >= 1; $i--) {
for ($k = 1; $k <= $i; $k++) {
print '*';
}
print "\n";
}
}
test(10);
?>
</xmp>
SDP2006
05-21-2004, 02:22 AM
I cannot belive that, I've got until the beginning of July...
What day did you start? ;)
Thanks guys, btw. I've got it working. Thanks :p
you get the notice because $output isn't defined.
(Because i was trying to keep the number of lines as low as 2). The notice will have no effect on the executed code, since it only points to the sloppy code-practice where you assume that all variables have value '' on there first call (it's the example of sloppyness inside the manuals section on error-reporting).
firepages
05-21-2004, 11:18 AM
errr what about 1 big line .. do I win something ?
with say $n=50;
<?
while($x<$n){++$x;echo($x<($n/2))?str_repeat('*',$x).'<br />':str_repeat('*',($n-$x)).'<br />';}
?>
firepages
05-21-2004, 11:24 AM
<?
$n=50;while($x<$n){++$x;echo str_repeat('*',($x<($n/2))?$x:($n-$x)).'<br />';}
?>
:D
<edit>
lol , now you got me distracted ..
its an envelope :D not quite what I intended but an envelope non the less !
<?
$n=50;
while($x<$n){
++$x;
echo str_repeat('*',($x<($n/2))?$x:($n-$x)).
' '.
str_repeat('*',($x<($n/2))?($n-$x):$x).
'<br />';
}
?>
</edit>
good thinking, but wouldn't you say it's kinda cheating to just wrap it all onto one line.
<edit>Posts crossed. I'd still go for my two simpl lines (because i can at least understand those after 10 whiskey which is my ultimate criterium :D )</edit>