Page 1 of 1

[Challenge] - Pascal's Triangle

Posted: Sun Jul 31, 2016 8:48 am
by dagumanz
[[ split from viewtopic.php?f=1&t=112782 --requinix ]]

Hi Apollo,

PLease help.

I tried your code below:

Code: Select all

<?php
$n = $_POST['n'];
if (!$n) die("<form method='post'>How many? <input type='text' name='n'> <input type='submit'></form>");
print("<table>");
$e = "<td>&nbsp;</td>";
for ($i=0; $i<=$n; $i++)
{
    $row = array();
    $row[] = 1;
    $s = str_repeat($e,$n-$i);
    print("<tr>$s");
    for ($j=0; $j<$i; $j++)
    {
        if ($j)
        {
            $row[] = $prev[$j-1]+$prev[$j];
            print($e);
        }
        print("<td>$row[$j]</td>");
    }
    $row[] = 0;
    $prev = $row;
    print("$s</tr>");
}
print("</table>");
?>
When I run it in the browser, the pascal triangle runs correctly but there's an error which says:
Undefined index: n in D:\wamp\www\exer\pascaltriangle.php on line 2 Call Stack

Can anyone solve this error? THanks in advance

Re: [Challenge] - Pascal's Triangle

Posted: Sun Jul 31, 2016 12:07 pm
by Christopher
The error says "Undefined index: n ... on line 2". Line 2 is:

Code: Select all

$n = $_POST['n'];