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 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jun 2011
    Posts
    119
    Thanks
    43
    Thanked 0 Times in 0 Posts

    Question Design Pattern not coming

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


    <t> *
    <t> * *
    <t> * *
    <t> * *

    The pattern may be not coming so please see the attached bmp image to get an idea.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?
    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.
    Attached Images Attached Images
    Last edited by ippo; 05-05-2012 at 05:44 AM.

  • #2
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    do you need the shape to change depending on different input data or you simply want only a triangle exactly like the image and nothing else?


    if you only ever want that triangle...

    PHP Code:
    $astStarData = array('&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;','&nbsp;&nbsp;*&nbsp;&nbsp;*&nbsp;&nbsp;','*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*');
    $j count($astStarData);
    for(
    $i 0$i $j$i++){
        echo 
    $astStarData[$i].'<br>';

    Last edited by jmj001; 05-05-2012 at 08:11 AM.

  • Users who have thanked jmj001 for this post:

    ippo (05-05-2012)

  • #3
    Regular Coder
    Join Date
    Jun 2011
    Posts
    119
    Thanks
    43
    Thanked 0 Times in 0 Posts
    Hi jmj001,
    Great!! I liked your answer and I thanked you too for the great help.
    But ,yes .I am looking for variable input , that is ,I want user to input the number of rows and then the output in the form of a triangle of stars with the specified no. of rows will be displayed.
    Also,I will request you to give me in the same format as the code I posted.I mean can you pls tell me how to display with loops only without using PHP array?
    Or if you can give me in "C" Language that will also be fine.
    Whichever I would request you not to the format I have posted ,i.e. ,pls use the loops (whatever u can use -while,for,if-else without using any advanced features of php or any other language except C programming as I said before)!!

    Thanks again.
    Hope you will help me.

  • #4
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    This will allow users to input any number of rows, then display the asterisks accordingly.

    It only needs two for loops. One to iterate through the rows, and one for the stars of each. The style is controlled with CSS, rather than using a billion &nbsp;.

    Edit: Just updated as used span instead of div by mistake

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
        
    <head>
        <title>Stars</title>
        
        <style type="text/css">
            
            div {
                text-align: center;
            }
            
            .ast {
                padding: 5px;
                display: inline;
            }
        
        </style>
    </head>

    <body>
        
        <form method="post" action="">
            Select number of rows <input type="text" size="2" name="rows" />
            <input type="submit" value="Go" />
            
            <div>
                <?php
                
    if (isset($_POST['rows'])) {
                    
    $rows $_POST['rows'];
                    
                    if (
    $rows == "")
                        echo 
    'You didn\'t enter anything!';
                    else {
                        
                        echo 
    $rows " rows of stars";
                        
                        for (
    $r 0$r $rows$r++) {
                            echo 
    '<div>';
                            
                            for (
    $a 0$a $r 1$a++) {
                                echo 
    '<span class="ast">*</span>';    
                            }
                            echo 
    '</div>';
                        }
                    }
                }
                
    ?>
            </div>

    </form>

    </body>
    </html>
    Last edited by dan-dan; 05-05-2012 at 04:36 PM.

  • Users who have thanked dan-dan for this post:

    ippo (05-05-2012)

  • #5
    Regular Coder
    Join Date
    Jun 2011
    Posts
    119
    Thanks
    43
    Thanked 0 Times in 0 Posts
    Hi Dan-dan,
    Problem is you perhaps didn't see the picture I attached!!
    It is a hollow triangle and in your case it is a filled one!!
    Please see the attached bmp image .
    Good Luck!!
    Also,if it is possible you can also write in C language.

  • #6
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    I did see the attachment, so I'm not sure why I had it filled. I was half asleep at the time.

    It's easy enough to fix. I don't know the C language.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
        
    <head>
        <title>Stars</title>
        
        <style type="text/css">
            
            div {
                text-align: center;
            }
            
            .ast {
                padding: 5px;
                display: inline;
            }
            
            .spacer {
                display: inline-block;
                width: 15px;
            }
        
        </style>
    </head>

    <body>
        
        <form method="post" action="">
            Select number of rows <input type="text" size="2" name="rows" />
            <input type="submit" value="Go" />
            
            <div>
                <?php
                
    if (isset($_POST['rows'])) {
                    
    $rows $_POST['rows'];
                    
                    if (
    $rows == "")
                        echo 
    'You didn\'t enter anything!';
                    else {
                        
                        echo 
    $rows " rows of stars";
                        
                        for (
    $r 0$r $rows$r++) {
                            echo 
    '<div>';
                            
                            for (
    $a 0$a $r 1$a++) {
                                if ((
    $a == 0) || ($a == $r)) 
                                    echo 
    '<span class="ast">*</span>';  
                                else
                                    echo 
    '<span class="spacer"></span>';
                            }
                            echo 
    '</div>';
                        }
                    }
                }
                
    ?>
            </div>

    </form>

    </body>
    </html>

  • Users who have thanked dan-dan for this post:

    ippo (05-05-2012)

  • #7
    Regular Coder
    Join Date
    Jun 2011
    Posts
    119
    Thanks
    43
    Thanked 0 Times in 0 Posts
    Dear dan-dan,
    Thanks Very much for the Help! Ya this is what I wanted!!Great!!!
    2 Thanks for ur 2 answers!!

  • #8
    Regular Coder
    Join Date
    Jun 2011
    Posts
    119
    Thanks
    43
    Thanked 0 Times in 0 Posts
    But still I have a few questions @dan-dan.
    Firstly,how u fix how many pixels are needed to give the spaces?
    2ndly,what is the spacer doing when there is padding?
    3rdly,why r u giving padding to the asterisks?
    4thly,can u tone down ur porgram to a more simpler one like the one I wrote?
    I mean without using CSS or Javascript or any other language ,can u write the program in the way I wrote originally in PHP only?(without using any advanced features of PHP of course like no arrays)

  • #9
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    1) The below loop works out how many points are needed for a full triangle, then echos an astrisk for the first and last points, and empty spans for the points in between. Bare in mind I originally thought it was a filled triangle. The class 'spacer' specifies the width of the spacer from the CSS. I just changed the number till it looked right.

    PHP Code:
    for ($a 0$a $r 1$a++) {
                                if ((
    $a == 0) || ($a == $r)) 
                                    echo 
    '<span class="ast">*</span>';  
                                else
                                    echo 
    '<span class="spacer"></span>';
                            } 
    2) The padding was to create space in between the astrisks. Like said above the spacer was to replace what were the filling astrisks.

    3) So they're not squashed together.

    4) This isn't complex in the slightest! It isn't using any advanced features either. In fact it uses the same loops as yours. You can get rid of the CSS and just use HTML, but why take a step back?
    Also, you're not just using PHP, you're also using HTML within your PHP. HTML and CSS work hand-in-hand, so it might be worth learning.

    I'm not going to write the program again, I'm sure you can manage the tweaking.

  • #10
    Regular Coder
    Join Date
    Jun 2011
    Posts
    119
    Thanks
    43
    Thanked 0 Times in 0 Posts
    Dear dan-dan,
    I do know CSS nd HTML,but actually for some reason I want it in PHP(& of course HTML but no CSS) only.A program as u very well know can be written in HTML and PHP only without using any CSS at all.then why dont u write a program without CSS for my sake? Actually I originally want to write a C program so I need the loop logic clearly ,of course CSS is now a regular practice but as I aid for the above reason I want only in HTML and PHP only.
    Hope u now got me!!
    Now sorry ,I cannot manage the tweaking ,can you do me that one last favour pleaseee?

  • #11
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    If you know basic HTML and CSS, then you must know how to copy a few styles from one to the other. As you don't, have a look at this brief tutorial.

    http://www.w3schools.com/html/html_styles.asp

    You also need to change the <DOCTYPE> if you care about validity.
    Last edited by dan-dan; 05-06-2012 at 02:27 PM.


  •  

    Posting Permissions

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