skip to Main Content

i’m trying to make cartesian coordinate for my coursework and can only get here, I’m grateful if anyone can help me to fix this "0" line

<?php
$n=10;
for($i=-10;$i<=$n;$i++){
    for($j=-10;$j<=$n;$j++){
        if($i==0 || $j==0){
            echo " $i ";
        }   else {
                echo "   ";
            }
        
    }
    echo "n";
}

Current output
What i expected

2

Answers


  1. You can try this in mode console.
    But it’s not really readable

    But the principle is there

    $n=10;
    
    for($y=$n;$y>-$n;$y--)
    {
        for($x=-$n;$x<=$n;$x++)
        {
            $infoDisplay = "  ";
    
            if($x==0)   $infoDisplay = $y;
            if($y==0)   $infoDisplay = $x;
    
            echo $infoDisplay;
        }
    
        echo "n";
    }
    
    Login or Signup to reply.
  2. You could reserve a grid. "Draw" the axis and print it out.

    $size = 10;
    $grid = array_fill(0, $size * 2 + 1, array_fill(0, $size * 2 + 1, ''));
    
    for ($x = -$size; $x <= $size; ++$x) {
        $grid[$size][$x + $size] = $x;
        $grid[$x + $size][$size] = $x;
    }
    
    foreach ($grid as $row) {
        foreach ($row as $cell) {
            $cell === '' ? print '    ' : printf('%+3d ', $cell);
        }
        echo PHP_EOL;
    }
    

    Output

                                            -10                                         
                                             -9                                         
                                             -8                                         
                                             -7                                         
                                             -6                                         
                                             -5                                         
                                             -4                                         
                                             -3                                         
                                             -2                                         
                                             -1                                         
    -10  -9  -8  -7  -6  -5  -4  -3  -2  -1  +0  +1  +2  +3  +4  +5  +6  +7  +8  +9 +10 
                                             +1                                         
                                             +2                                         
                                             +3                                         
                                             +4                                         
                                             +5                                         
                                             +6                                         
                                             +7                                         
                                             +8                                         
                                             +9                                         
                                            +10                                         
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search