Log in

View Full Version : Traingle of asterisks



dcr33
05-05-2012, 11:29 AM
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.


<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.

traq
05-06-2012, 01:31 AM
You might look into using string functions like str_pad() (http://php.net/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.

... 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.

james438
05-06-2012, 03:35 AM
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.

james438
05-06-2012, 06:16 AM
Why do you want to do this? I only ask because having us work on school projects is rather frowned upon here.

traq
05-06-2012, 03:51 PM
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

dcr33
05-06-2012, 05:19 PM
Traq i am upset that wont work ever.Leave aside hirng u ,:D :p Have u ever tried that in ur own machine?pathetic co...
:mad:

james438
05-06-2012, 06:21 PM
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.

traq
05-06-2012, 10:18 PM
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.

... that wont work ever. Have u ever tried that in ur own machine?pathetic co...
:mad:
yes, I have.
<?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 > 1 ){
// $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:
*
* *
* *
* *
* *
* *
* *
* *I hope this helps you with your questions.