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 6 of 6
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    41
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Exclamation Algorithms & for loops




    Hello!


    Can somebody help me out?


    PHP Code:
    <?php 
    /*Create a function that does the following for a giving number (i.e $num = 10):


                               $stars -  $spaces -  $starSide
                                 ||         ||       ||
       $i                     ($i+1)*2 - $x-$stars-  $1+1
       ||                         ||        ||         ||
       0 *                  * (2 stars - 18 spaces - 1 star/side)
       1 **                ** (4 stars - 16 spaces - 2 stars/side)
       2 ***              *** (6 stars - 14 spaces - 3 stars/side)
       3 ****            **** (8 stars - 12 spaces - 4 stars/side)
       4 *****          ***** (10 stars - 10 spaces - 5 stars/side)
       5 ******        ****** (12 stars - 8 spaces - 6 stars/side)
       6 *******      ******* (14 stars - 6 spaces - 7 stars/side)
       7 ********    ******** (16 stars - 4 spaces - 8 stars/side)
       8 *********  ********* (18 stars - 2 spaces - 9 stars/side)
       9 ******************** (20 stars - 0 spaces - 10 stars/side)
         0123456789... = $k 
         $x = 20
    */   
    function starsTriangle($num){
        
    $x = ($num*2);
        for(
    $i=0;$i<$num;$i++){
            for(
    $k=0;$k<$x;$k++){
                
    //$i+1 = star/side
                //($i+1)*2 = stars
                //$x - stars = spaces
                
    $starSide $i+1;
                
    $stars = ($i+1)*2;
                
    $spaces $x-$stars;
                
    //What the f*ck is the algorithm 
            
    }
            echo 
    '<br>';    
        }
    }
    starsTriangle(10);
    ?>



    Last edited by matz0rz; 05-09-2013 at 02:21 PM.

  • #2
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    PHP Code:
    function starsTriangle($num)
    {
            
    $totalChars $num;
            for (
    $i 0$i $num$i++)
            {
                    for (
    $j 0$j <= $i$j++)
                            echo 
    "*";

                    
    $spaces $totalChars - (* ($i 1));
                    for (
    $sp 0$sp $spaces$sp++)
                            echo 
    " ";

                    for (
    $j 0$j <= $i$j++)
                            echo 
    "*";

                    echo 
    "\n";
            }
    }
    starsTriangle(10); 
    For execution in a browser, replace " " with &nbsp; and \n with <br />

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    41
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Resolved

    PHP Code:
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    </head>

    <body>
    <font style="font-family: monospace">
    <?php 
    /*Create a function that does the following for a giving number (i.e $num = 10):


                               $stars -  $spaces -  $starSide
                                 ||         ||       ||
       $i                     ($i+1)*2 - $x-$stars-  $1+1
       ||                         ||        ||         ||
       0 *                  * (2 stars - 18 spaces - 1 star/side)
       1 **                ** (4 stars - 16 spaces - 2 stars/side)
       2 ***              *** (6 stars - 14 spaces - 3 stars/side)
       3 ****            **** (8 stars - 12 spaces - 4 stars/side)
       4 *****          ***** (10 stars - 10 spaces - 5 stars/side)
       5 ******        ****** (12 stars - 8 spaces - 6 stars/side)
       6 *******      ******* (14 stars - 6 spaces - 7 stars/side)
       7 ********    ******** (16 stars - 4 spaces - 8 stars/side)
       8 *********  ********* (18 stars - 2 spaces - 9 stars/side)
       9 ******************** (20 stars - 0 spaces - 10 stars/side)
         0123456789... = $k 
         $x = 20
    */   
    function starsTriangle($num){
        
    $x = ($num*2);
        for(
    $i=0;$i<$num;$i++){
            for(
    $k=0;$k<$x;$k++){
                
    $str '&nbsp;';
                
    $starSide $i+1;
                if(
    $i>=$k){
                    echo 
    '*';
                }else if((
    $x-$i)<=$k+1){
                    echo 
    '*';
                }else{
                    echo 
    $str;
                }
                
            }
            echo 
    '<br>';    
        }
    }
    starsTriangle(10);
    ?>
    </font>
    </body>
    </html>

  • #4
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    PHP Code:
    function starsTriangle($num)
    {
            
    $totalChars $num;
            for (
    $i 0$i $num$i++)
            {
                    for (
    $j 0$j <= $i$j++)
                            echo 
    "*";

                    
    $spaces $totalChars - (* ($i 1));
                    for (
    $sp 0$sp $spaces$sp++)
                            echo 
    "&nbsp;";

                    for (
    $j 0$j <= $i$j++)
                            echo 
    "*";

                    echo 
    "<br />\n";
            }

    OR simpler

    PHP Code:
    function starsTriangle($num)
    {
            
    $totalChars $num;
            for (
    $i 1$i <= $num$i++)
            {
                    
    $spaces $totalChars - ($i);

                    echo 
    str_repeat('*'$i);
                    echo 
    str_repeat('&nbsp;'$spaces);
                    echo 
    str_repeat('*'$i);

                    echo 
    "<br />\n";
            }


  • #5
    New Coder
    Join Date
    Dec 2011
    Posts
    86
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Here's how to get it to look exactly on screen what you want (I know it just aesthetics):
    PHP Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Star Triangle</title>
    </head>
    <body>
    <?php
    function starsTriangle($num)
    {
            
    $totalChars $num;
           
            for (
    $i 1$i <= $num$i++)
            {
                    
    $spaces $totalChars - ($i);
                 echo 
    '<pre>';
                    
    printf("%s%s%s"str_repeat('*'$i) , str_repeat('&nbsp;'$spaces) , str_repeat('*'$i));                             
                 echo 
    '</pre>';
            }
    }  
    starsTriangle(10); 
    ?>
    </body>
    </html>
    Last edited by Strider64; 05-09-2013 at 04:03 PM.

  • #6
    rgb
    rgb is offline
    New Coder
    Join Date
    Jul 2011
    Posts
    17
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Slightly off topic, but the font tag used by the original poster is no longer supported in HTML5.


  •  

    Tags for this Thread

    Posting Permissions

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