SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Spacing for a Pattern - Nesting Loops

    Hi all,

    I have to create a program for the following pattern.
    ^
    ^^
    ^^^
    ^^^^
    ^^^^^
    ^^^^^^
    ^^^^^^^
    ^^^^^^^^
    ^^^^^^^^^
    ^^^^^^^^^^
    ^^^^^^^^^^^



    I'm able to get
    ^
    ^^
    ^^^
    ^^^^
    ^^^^^
    ^^^^^^
    ^^^^^^^
    ^^^^^^^^
    ^^^^^^^^^
    ^^^^^^^^^^
    ^^^^^^^^^^^
    but I'm unsure how to go about the spacing. I know its using a loop and the &nbsp. Anyone able to help?

    My code so far:
    PHP Code:
    <?php
    for ($count 1$count <=20$count++)
    {
        for (
    $num 1$num <=$count$num++)
        {
            print(
    "^");
        }
        
        print(
    "<br/>");
    }
    ?>
    Thanks,
    Jess

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your image is password protected, so we can't see it.

    Is this your homework?

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Your image is password protected, so we can't see it.

    Is this your homework?
    Changed image.

    Its 'practice' stuff - not graded.

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Are you sure that's the pattern in the image? Each row has one more carat than the last, not two? Because you can't print half spaces, which makes that impossible with a fixed width font...

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Are you sure that's the pattern in the image? Each row has one more carat than the last, not two? Because you can't print half spaces, which makes that impossible with a fixed width font...
    Ok uploaded image. Sorry didn't see the upload option the first time.
    Attached Images Attached Images

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    See, that's not what you typed, the problem in the image is much more straightforward!

    PHP Code:
    for ($count 1$count <= 20$count += 2) {
            for (
    $num 0$num floor((20 $count) / 2); $num++) {
                    echo 
    "&nbsp;";
            }
            for (
    $num 1$num <= $count$num++) {
                    echo 
    "^";
            }
            echo 
    "<br />";


  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see how its more similar to the image - but its not 'centred'

    Whats 'floor'?

    That does look familiar though (saw a friends version that kind of worked)

  8. #8
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It's centered if you use a fixed width font, as the problem image does, else this is not a (simple) solvable problem. Specify a fixed-width font for your webpage.



    floor() rounds down -- php.net is the PHP manual where you can look up any function.

  9. #9
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Tee-he-he
    PHP Code:
    echo '<pre>';
    for (
    $i=10$c=1$i-- && printf("%{$i}s%'^{$c}s\n"''''); $c+=2);
    echo 
    '</pre>'
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

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