Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12

Thread: Looping *'s

  1. #1
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Looping *'s

    Is there any way I could loop through a * to produce something like this?
    Code:
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    ********
    *******
    ******
    *****
    ***
    **
    *
    Thanks. I'm pretty sure it is possible, but *I'm* not sure how to achieve it.

    Thanks
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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?
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can be done with just two lines of code :
    PHP 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?
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #4
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Spookster
    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 Code:
    <?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!
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Although that may work that's not the way that problem was meant to be answered. It was meant to teach nested loops.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #6
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Quote Originally Posted by SDP2006
    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...

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Example with nested loops:

    Code:
    <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>
    De gustibus non est disputandum.

  • #8
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Error 404
    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
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #9
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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).
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #10
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    errr what about 1 big line .. do I win something ?

    with say $n=50;
    PHP Code:
    <?
    while($x<$n){++$x;echo($x<($n/2))?str_repeat('*',$x).'<br />':str_repeat('*',($n-$x)).'<br />';}
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #11
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts

    and the winner is ...

    PHP Code:
    <?
    $n
    =50;while($x<$n){++$x;echo str_repeat('*',($x<($n/2))?$x:($n-$x)).'<br />';}
    ?>


    <edit>
    lol , now you got me distracted ..

    its an envelope not quite what I intended but an envelope non the less !
    PHP Code:
    <?
    $n
    =50
    while(
    $x<$n){ 
        ++
    $x
        echo 
    str_repeat('*',($x<($n/2))?$x:($n-$x)). 
        
    ' &nbsp; '
        
    str_repeat('*',($x<($n/2))?($n-$x):$x). 
        
    '<br />'

    ?>
    </edit>
    Last edited by firepages; 05-21-2004 at 12:32 PM.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #12
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 )</edit>
    Last edited by raf; 05-21-2004 at 12:34 PM.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •