Daan
Daan

Reputation: 598

PHP Nested for loop - pyramid shape

Using a nested for loop in PHP, i have to create the following pattern:

- - - - - - - - - - - - - - -
- - - - - - - + - - - - - - -
- - - - - - + + + - - - - - -
- - - - - + + + + + - - - - -
- - - - + + + + + + + - - - -
- - - + + + + + + + + + - - -
- - + + + + + + + + + + + - -
- + + + + + + + + + + + + + -
+ + + + + + + + + + + + + + +

I have tried to do this and wrote the following code:

$pluscount = -1;
$mincount  = 8;
for ($rows = 0; $rows <= 8; $rows++) {
    for ($min = 0; $min < $mincount; $min++) {
        echo " - ";
    }
    for ($plus = 0; $plus < $pluscount; $plus++) {
        echo " + ";
    }
    for ($min = 0; $min < $mincount; $min++) {
        echo " - ";
    }
    $pluscount += 2;
    $mincount  = (15 - $pluscount) / 2;
    echo "<br />";
}

However, this results in:

- - - - - - - - - - - - - - - - 
- - - - - - - + - - - - - - - 
- - - - - - + + + - - - - - - 
- - - - - + + + + + - - - - - 
- - - - + + + + + + + - - - - 
- - - + + + + + + + + + - - - 
- - + + + + + + + + + + + - - 
- + + + + + + + + + + + + + - 
+ + + + + + + + + + + + + + + 

As you can see, the first line is incorrect. How do I solve this?

Upvotes: 3

Views: 1807

Answers (5)

Monty Khanna
Monty Khanna

Reputation: 1120

<?php
    create_pyramid("+", 10);

    function create_pyramid($string, $level) {
        echo "<pre>";
        $level = $level * 2;
        print str_repeat("-",$level - 1)."<br/>";
        for($i = 1; $i <= $level; $i ++) {
            if (!($i % 2) && $i != 1)
                continue;
            print str_pad(str_repeat($string, $i),($level - 1) * strlen($string), "-" , STR_PAD_BOTH);
            print PHP_EOL;
        }
    }
?>

Upvotes: 0

J A
J A

Reputation: 1766

I know the best answer has already been selected but it takes me back to time when everything was much simpler and I just started to learn programming.. Couldn't resist walking through the memory lane ;)

$xLength = 15;
$yLength = 9;
$fillerChar = '-';
$outputChar = '+';

$drawPoint = floor($xLength/2);
$endPoint  = $drawPoint;

for ($yPosition=0; $yPosition<$yLength; $yPosition++) {

    for ($xPosition=0; $xPosition<$xLength; $xPosition++) {

        if (($drawPoint < $xPosition) && ($xPosition < $endPoint)) {

            print $outputChar;

        } else {

            print $fillerChar;

        }
    }

    $drawPoint--;
    $endPoint++;

    print("\n");

}

Upvotes: 0

Halayem Anis
Halayem Anis

Reputation: 7805

$pluscount = -1;
$mincount  = 8;
for ($rows = 0; $rows <= 8; $rows++) {
    for ($min = 0; $min < $mincount; $min++) {
        echo " - ";
    }
    for ($plus = 0; $plus < $pluscount; $plus++) {
        echo " + ";
    }
    for ($min = 0; $min < min($mincount, 7); $min++) {
        echo " - ";
    }
    $pluscount += 2;
    $mincount  = (15 - $pluscount) / 2;
    echo "<br />";
}

Upvotes: 1

Danijel
Danijel

Reputation: 12709

You can replace the sub loops with str_repeat(), and use the substr() to trim the last part.

for ( $row = 8; $row >= 0; $row-- ) {
    echo substr( 
       str_repeat( " - ", $row ) . str_repeat( " + ", max( 15 - ($row*2), 0 ) ) . str_repeat( " - ", $row ), 
    0, 45 );
}

Upvotes: 2

drneel
drneel

Reputation: 2907

This is a hacky way to take care of the problem. I'm not a fan of this solution overall.

$pluscount = -1;
$mincount  = 7;
for ($rows = 0; $rows <= 8; $rows++) {
    for ($min = 0; $min < $mincount; $min++) {
        echo " - ";
    }

    for ($plus = 0; $plus < $pluscount; $plus++) {
        echo " + ";
    }

    if ($rows == 0) {
            $mincount += 1;
    }

    for ($min = 0; $min < $mincount; $min++) {
        echo " - ";
    }
    $pluscount += 2;
    $mincount  = (15 - $pluscount) / 2;
    echo "<br />";
}

I think you would be better off starting with a string of 15 '-' and replacing the number that you need based on what row you were on.

//Pseudo-code
plusCount = -1;
baseString = "---------------";
startIndex = 7;
for (row = 0; row < 10; row++) {
     if(plusCount > 0) {
         //string replace startingLocation = startIndex - plusCount; number = pluscount
     } else {
         //baseString
     }
}

Upvotes: 1

Related Questions