"Coordinate Planes"
In math, a Cartesian plane is most commonly known as an X,Y graph. This graph has 4 quadrants. Here is the listing for the Quadrants and the relation to the (x,y) pair:
Quad. II: (-x, y)
Quad. III: (-x, -y)
Quad. IV: (x, -y)
The pair (3, -5) lies in Quardant IV.
The pair (3, 3) lies in Quard. I
Given an input file of 1000 randomly generated x, y pairs (plane.txt), how many of those pairs lie in Quadrant III?
This is fairly simple once you understand it. First get a copy of the text file (for my script it requires cURL) then split it up and itterate over each value pair. If both numbers are negative (in quadrant 3) add a hit to the result counter.
Awnser:
(, ) (, ) (, ) ( , ) ( ) ( ) ( , ) (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, )
Q3 = 0
Q3 = 0
Source:
function fetch($url,$post=false,$cookie=false,$timeout=30){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); if ($cookie) { curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); } if ($post) { curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_POST, TRUE); } curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); $output=curl_exec($ch); curl_close($ch); return $output; } function solution(){ $result = 0; $list = fetch('http://www.cstutoringcenter.com/problems/files/plane.txt'); $lines = explode("\n", $list); foreach($lines as $coord) { $color = "red"; $quad = explode(",", $coord); if(($quad[0] < 0)&&($quad[1] < 0)){ $result++; $color = "lightgreen"; } echo "<small><font style=\"color:".$color.";\"> (".$quad[0].", ".$quad[1].") </font></small>"; } echo "<br /><strong>Q3 = ".$result."</strong>"; }