John Davidson

make cartesian coordinate with for-loop in php

0 comments
Message:


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



My Answer:
Sure, here is an example of how you can create a Cartesian coordinate system using a for loop in PHP:

php

// Define the size of the coordinate system
$size = 10;

// Loop through the y-axis
for ($y = $size; $y >= -$size; $y--) {
// Loop through the x-axis
for ($x = -$size; $x <= $size; $x++) {
// Check if the current point is the origin
if ($x == 0 && $y == 0) {
echo "O ";
} else {
echo "($x, $y) ";
}
}
echo "\n";
}

?>


This code will create a Cartesian coordinate system with a size of 10 in both the x and y directions. The origin will be represented by the letter "O" and all other points will be displayed as (x, y) coordinates.

Rate this post

5 of 5 based on 2835 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us