Advanced Search

Results 1 to 8 of 8

Thread: Traingle of asterisks

  1. #1
    Join Date
    Jul 2011
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Arrow Traingle of asterisks

    Hi
    I am trying to display this design pattern(a triangle of asterisks) in my screen-


    *
    * *
    * *

    The pattern may not be displaying here properly ,so request you to please see the attached .bmp image to get an idea of what i mean.The stars are simple asterisks in keyboard.

    Problem is I tried very hard with this code I wrote but it is not giving the desired result!!
    Please tell me what is wrong with the code and can you give me a full working code using only loops and conditions in php and not use any array or any other language?
    One another important thing is the no. of rows can vary according to user input.

    Code:
    <html>
    <body>
    <?php
    $c=1;
    for($i=1;$i<=7;$i++)
    {
    	$s=1;
    for($j=1;$j<=7-$i;$j++)
    echo "&nbsp;";
    for($k=1;$k<=$s;$k++)
     echo "*";
    // echo "<BR>";
    
    	 for($z=$i;$z>1;$z--)
    		 echo "&nbsp;"; 
    		
    	 
    echo "&nbsp;";
    if($c=1)
    	{
       		
    		$c++;break;
    	}
    	else
    	{
    	    echo "*";
    		$c++;
    	}
     echo "<BR>";
     }
     ?>
     </body>
     </html>
    Since I am a noob I may be using many unnecessary variables while experimenting, like $c above .Please change the code if necessary but please give me a full program code.
    Will be greatly obliged.

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,627
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    You might look into using string functions like str_pad().
    Instead of trying to format this using html, I would recommend putting the entire output into a <pre> element and use a monospace font, so it will display as intended in plain text.
    Quote Originally Posted by dcr33 View Post
    ... please give me a full program code.
    It's fine to expect someone to help you with specific problems (and I will be happy to help you), but keep in mind that we're all volunteering our time here.
    If you want a complete solution, you should consider hiring someone.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,681
    Thanks
    80
    Thanked 89 Times in 87 Posts

    Default

    What he is asking for is relatively simple. I don't think I would suggest hiring in this case.

    I want to add that lots of us regulars are mostly website enthusiasts or people who just like web design puzzles to figure out. It also helps to keep their website mids sharp. I consider myself a web design enthusiast.
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,681
    Thanks
    80
    Thanked 89 Times in 87 Posts

    Default

    Why do you want to do this? I only ask because having us work on school projects is rather frowned upon here.
    Last edited by james438; 05-06-2012 at 06:47 AM.
    To choose the lesser of two evils is still to choose evil. My personal site

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,627
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    How's it coming?

    I think I'd approach it something like this:

    1. initialization:
    -- set number of rows
    -- write initial row to an array (first row is always a single star)
    2. if more than one row:
    -- second row will have 1 space between stars
    -- each subsequent row will add two spaces between stars
    3. when you're done, add padding:
    -- count width of last (largest) row
    -- pad all other rows on left+right to match
    4. print each row in the array on a new line, inside <pre> tags
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  6. #6
    Join Date
    Jul 2011
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Traq i am upset that wont work ever.Leave aside hirng u , Have u ever tried that in ur own machine?pathetic co...
    Last edited by dcr33; 05-06-2012 at 05:38 PM.

  7. #7
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,681
    Thanks
    80
    Thanked 89 Times in 87 Posts

    Default

    Traq does have a working version of the script you are working on, but I suspect he wants to see you try working on it some more. If you think about your project there are actually several ways to create the pyramid script that you are working on.
    To choose the lesser of two evils is still to choose evil. My personal site

  8. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,627
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by dcr33 View Post
    Traq i am upset ... Leave aside hirng u ...
    I wasn't suggesting you hire me. Your initial post seemed to imply you just wanted someone to give you working code -- hiring someone is a better solution to that end.

    I'm sorry to have upset you, and I apologize. I was trying to discover if you wanted to learn something or just have someone do it for you.
    Quote Originally Posted by dcr33 View Post
    ... that wont work ever. Have u ever tried that in ur own machine?pathetic co...
    yes, I have.
    PHP Code:
    <?php
    // $n determines the number of rows in your triangle
    $n 8;
    // first row (always a single star)
    $R[] = '*';
    // more than one row needed?
    if( $n ){
      
    // $w is the amount of whitespace needed for each row
      //   (the second row will need only 1 space)
      
    $w 1;
      
    // loop to create each row
      
    for( $c=2$c<=$n$c++ ){
        
    // each row adds an element to the array
        //   containing star + whitespace + star
        
    $R[] = '*'.str_repeat' ',$w ).'*';
        
    // increment the whitespace count by two for next row
        
    $w $w+2;
      }
      
    // $l is the length of the last (longest) row
      
    $l strlen$R[$n-1] );
      
    // loop through each row in the array
      
    for( $c=0$c<count($R); $c++ ){
        
    // pad each row so they're all the same length
        
    $R[$c] = str_pad$R[$c],$l,' ',STR_PAD_BOTH );
      }
    }
    // using <pre> to preserve the whitespace / newlines
    print '<pre>'.implode"\n",$R ).'</pre>';
    output:
    Code:
           *       
          * *      
         *   *     
        *     *    
       *       *   
      *         *  
     *           * 
    *             *
    I hope this helps you with your questions.
    Last edited by traq; 05-06-2012 at 10:41 PM.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

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
  •