Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#!/usr/bin/php -d display_errors

#!/usr/bin/php -d display_errors 5; //variable to play with. /** * Procedures in php are called... fucntions! * * Functions do things...they take inputs (called parameters), and return * an output. * * By default, a function does not modify the parameter, but rather makes a copy. * For example, this function will implement the successor fucntion we discussed in class. */ function successor($n) { $n++; return $n; } //@TODO add comments to the two lines inside the fucntion, saying what each does. //Now we call the fucntion. echo successor($n) . " is the successor of " . $n . "! "; //notice how the value of $n was not changed by the function? //also, note the $n outside the function is different than the $n inside the function. $k = 6; //@TODO write a line of code below to output the successor of $k, using $k as the variable name. // Do not change the successor function. // YOUR CODE HERE. /** * Aside (you may ignore): * you may notice that php is loosly typed, unlike java...I did not * declare a variable type. * In php7, support was added for strictly typing variables, * if you so desire. Sometimes, when debugging a complicated * script, this is useful, since loose typing can lead to strange bugs. * For a discussion, see https://www.w3schools.com/php/php_variables.asp * * Aside ends. */ /** * Implementation of the add fucntion from class. */ function add($n, $k) { // adds $k to $n by calling successor. echo " add called with n = " . $n . " and k = " . $k . ". "; $i = 0; echo "i = ". $i . ". "; while ($i < $k) { $n = successor($n); $i = successor($i); echo "i = " . $i . "\t k = " . $k . "\t n = " . $n . " "; } return $n; } //@TODO add comments to the above function, describing what each line of the above function does. echo "add(6,4) : " . add(6, 4) . " "; echo "add(4,6) : " . add(4, 6) . " "; echo "add(2,0) : " . add(2, 0) . " "; echo " "; /** * @TODO Write a function called "mult" that implements the multiplication algorithm we discussed * in class. * * use a while loop. * * call the add function above. * * output the values you begin with * * output each iteration of i, k, m, n in your loop * * return the new value * * Comment your code, so that someone reading it can understand * what it is doing. * * Tips: * * Like in class, you will need another variable, and start that * variable at zero. Refer to your class notes. * Fall 2020: Since we had a test review, we are going over this * on Tuesday, week 5. * * Keep $i = successor($i) in your loop. * */ // YOUR CODE HERE //@TODO once you have your mult, test it by uncommenting the following //echo "mult(2,3) : " . mult(2,3) . " "; //echo "mult(3,2) : " . mult(3, 2) . " "; //echo "mult(0,2) : " . mult(0, 2) . " "; //echo "mult(2,0) : " . mult(2, 0) . " "; //echo " ";

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 Fundamentals Study Guide

Authors: Dr. Sergio Pisano

1st Edition

B09K1WW84J, 979-8985115307

More Books

Students also viewed these Databases questions