Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

-Wherever you find the word @TODO in the file, modify the file as instructed. When you are completely finished, execute the code one last time,

-Wherever you find the word @TODO in the file, modify the file as instructed.

  1. When you are completely finished, execute the code one last time, and take a screenshot (or two or three...you may need multiple screenshots to get everything) of the output. #!/usr/bin/php -d display_errors

error_reporting(E_ALL); //above causes php to send errors to the console, instead of hiding them.

/** * The code file for problem set 7 * * We want to create a function that implements a few algorithms, and * we will use while loops to do it. */

/** * Breaking out of a loop. * * Supose we have the code: */ //$i = 0; //while(true) { // print($i++ . " "); //} /** * How many times will this code run? (By the way, hit ctrl-c to stop that in the command window.) * @TODO Uncomment the code above, and write your answer below. * * WRITE YOUR ANSWER HERE * * @TODO: comment the code again, so it stops doing that. * * Notice since the condition in the while loop is always true, the loop never stops. * * There are two ways to stop a loop from executing: * If the loop is inside a function, a return statement will exit the function, breaking the loop. * Or, you can use the break keyword. * For example. */ function loopInFunction() { $i = 0; while (true) { print($i++ . " "); if ($i > 5) { return 1; } } }

loopInFunction(); //calls the loop in a function code //@TODO How many times will that loop run? Try to guess before running the code. // Write your answer here: //

//example of break $i = 0; while(true){ print($i++ . " "); if ($i > 10) { break; } }

/** * So, why would you want to? * * Sometimes, especially if you have something complicated, it is easier to break out * of loops then write the perfect "while" condition...especially if it only happens sometimes. * * For more information, see * https://www.php.net/manual/en/control-structures.break.php * https://www.w3schools.com/php/php_looping_break.asp */

/** * The Division Algorithm */

/** * This method implements the division algorithm. * * It takes as input $a as an integer * and d as a positive integers, computes a/d, * and outputs an array contatining the quotient q and the remainder r. */ function divisionAlg(int $a, int $d) { if ($d < 1) { throw new InvalidArgumentException('divisionAlg requires divisor be positive. Input was: ' . $d); } //@TODO write what this thing does // Your Answer: $r = $a; //this will become the remainder $q = 0; //this will become the quotient /* * This handles negatives. We did not do this part of the algorithm in class. */ while ($r < 0) { $r = $r + $d; $q--; print( "r: " . $r . "\tq: " . $q . " " ); //debug code } /** * @TODO Comment the above loop, so that someone who did not quite understand * could read the code and understand what it does. */ while ($d <= $r) { $r = $r - $d; $q++; print( "r: " . $r . "\tq: " . $q . " " ); //debug code } /** * @TODO Comment the above loop, so that someone who did not quite understand * could read the code and understand what it does. */ return array( "q" => $q, "r" => $r ); }

print_r(divisionAlg(28, 5)); print_r(divisionAlg(-26, 5));

/** * Naive GCD Calculator * * The following will compute the gcd of two numbers, using the "definition" of gcd we did in class. */ function getGCD(int $a, int $b) { if ($a < 1 || $b < 1) { throw new InvalidArgumentException('getGCD requires non-negative integers. Input was: ' . $a . 'and' . $b); } if($a > $b) { $maxtocheck = $b; } else { $maxtocheck = $a; } $k = 1; $currentGCD = 1; while($k <= $maxtocheck) { if(($a % $k == 0) && ($b % $k == 0)) { //@TODO Write this condition in english: $currentGCD = $k; } print_r(array( "k" => $k, "currentGCD" => $currentGCD)); //@TODO what does this line do? $k++; } return $currentGCD; }

print (" The GCD of 9 and 495 is ... " . getGCD(9,495));

//print (" The GCD of 105280 and 495 is ... " . getGCD(105280,495)); //@TODO How many times would the program loop to compute the above line? // Write your answer here:

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions