Question

In: Computer Science

Requirements: In this assignment, you are going to create two switch functions. The first function is...

Requirements: In this assignment, you are going to create two switch functions. The first function is going to be called switchVal and the second is going to be called switchRef. The goal of this assignment is to demonstrate the understanding of what pass-by-reference (Links to an external site.) and pass-by-value (Links to an external site.) do when working with functions that you create. The two functions that you will modify need to accept two parameters and inside them they need to switch the values.  

Getting Started: Create a new folder ('assignment6'). Use the following template to create a PHP web page named switchExercise.php in your assignment5 folder. You will need to modify the two function definitions to do the work of switching the x and y values. You also need to add echo statements in the functions to announce which function was called and the values of x and y before the function exits.  

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // Retrieve the values from the web page for x and y
        $x = $_POST['x'];
        $y = $_POST['y'];

        function switchVal() {
            
        }

        function switchRef() {
            
        }
        ?>
        <form action="switchExercise.php" method="post">
            $x= <input name="x" style="width: 50px;" type="number" > (enter a number value to use for X)<br/>
            $y= <input name="y" style="width: 50px;" type="number" >  (enter a number value to use for Y)<br/>
            <br/>
            <input type="submit" name="switchVal" value="    Switch By Value    "/><br/>
            <input type="submit" name="switchRef" value="Switch By Reference"/><br/>            
        </form>
        <?php
        if (!empty($x) && !empty($y)) {
            echo "Values before function call <br/>";
            echo "X: $x  Y: $y";
            
            // Write a conditional statement to determine which button was selected        
            // and call the appropriate function
            
            echo "Values after function call <br/>";            
            echo "X: $x  Y: $y";            
        }
        ?>
    </body>
</html>

Solutions

Expert Solution

Source Code :

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>


<?php


// Retrieve the values from the web page for x and y
$x = $_POST['x'];
$y = $_POST['y'];
  
//pass by value function
//when this is used
//only the value inside the function is changed
function switchVal($x,$y) {


echo "<br/>switchVal function is called";
//temp variable is used to store
//a value when swapping or switching
//otherwise value in one variable will be lost
//value of x is stored in $temp
$temp = $x;
//value of $y is stored in $x
$x = $y;
//value of $temp (initial value in $x) is stored in $y.
$y = $temp;
//display the value before the function exit
echo "<br/>values before switchVal function exit <br/>";
echo "X: $x Y: $y";


}

//pass by reference function
//here & is used
//when it is used
//the change in value will also reflect in actual value
//because here the reference is passed
function switchRef(&$x,&$y) {


echo "<br/>switchRef function is called";
//temp variable is used to store
//a value when swapping or switching
//otherwise value in one variable will be lost
//value of x is stored in $temp
$temp = $x;
//value of $y is stored in $x
$x = $y;
//value of $temp (initial value in $x) is stored in $y.
$y = $temp;
//display the value before the function exit
echo "<br/>values before switchRef function exit<br/>";
echo "X: $x Y: $y";


}


?>
<form action="switchExercise.php" method="post">


$x= <input name="x" style="width: 50px;" type="number" > (enter a number value to use for X)<br/>
$y= <input name="y" style="width: 50px;" type="number" > (enter a number value to use for Y)<br/>
<br/>
<input type="submit" name="switchVal" value=" Switch By Value "/><br/>
<input type="submit" name="switchRef" value="Switch By Reference"/><br/>


</form>
<?php


if (!empty($x) && !empty($y)) {


//display value befor function call
echo "Values before function call <br/>";
echo "X: $x Y: $y";
  
// Write a conditional statement to determine which button was selected
// and call the appropriate function
//isset() checks whether a variable is set or not
//if set and not null, it will return true
//checks if switchVal button is clicked
if(isset($_POST['switchVal'])){
switchVal($x,$y);
}
//checks if switchRef button is clicked
if(isset($_POST['switchRef'])){
switchRef($x,$y);
}
//display value after function call
echo "<br/>Values after function call <br/>";
echo "X: $x Y: $y";


}


?>


</body>
</html>

Output and screenshots :


Related Solutions

create a C++ program where you have 2 functions with two parameters. Limit the first function...
create a C++ program where you have 2 functions with two parameters. Limit the first function allowed input to values of numbers from 1-10 and from 5 to 20 for the second function. have each function add their two-parameter together then add the functions final values together. Show error message if wrong input is entered   Ask the user if they wish to continue the program (use loop or decision for this question).
C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
Your assignment this week has two parts: First, you will create a timeline or infographic to...
Your assignment this week has two parts: First, you will create a timeline or infographic to address the following questions: Research and provide examples or comparisons that demonstrate these changes over time from each of the categories: Print, Radio/Music, Cinema, Television, and the Internet. Include images of each artifact. How have these various forms of communication have evolved since they were first introduced? (Give a “before” and “after”) Second, you will write a mini-statement answering the following question. In 300-500...
Assignment Requirements: 1. Find a workout or create a workout and perform it. If you use...
Assignment Requirements: 1. Find a workout or create a workout and perform it. If you use an online workout/video please reference it so I know what you've done. If you create one yourself, make sure to include your written workout in the assignment. 2. Write about each component/part of the workout and how it related to the health-related components of physical fitness. Answer in detail.
For this assignment, you are going to build upon several skills that you've learned: Create an...
For this assignment, you are going to build upon several skills that you've learned: Create an object that contains objects. Using querySelectorAll to read a nodeList from the DOM. Looping through the nodeList then updating the HTML page. Set up Create the assignment in a "week6" folder with the typical files: index.html css/styles.css js/scripts.js This is the standard structure that we'll use in all assignments. Here is the HTML to use for this assignment. Change the meta author tag to...
Create two functions that you can use for a dictionary manager: 1. remove_item(dictionary,key): a function that...
Create two functions that you can use for a dictionary manager: 1. remove_item(dictionary,key): a function that removes the item with the supplied key from the dictionary, if it exits. If the input key doesn’t exist in the dictionary, print out a message saying that the new item has not been removed because there is no matching key in the dictionary. Your function should not produce a Python error if the item does not exist; 2. add_new_item(dictionary,key,value): a function that adds...
In this assignment you are going to use the menu you created in Assignment 1 to...
In this assignment you are going to use the menu you created in Assignment 1 to test both your Double and Integer classes. You should add functionality to the menu to allow you test the add, sub, mul, div functions for instances of both classes You are going to have make a modification to your menu class. Currently it uses an array to hold a fixed amount of menu items. While this may be OK for most applications we want...
For this assignment you are required to create two loan amortization schedules. However, after you create...
For this assignment you are required to create two loan amortization schedules. However, after you create the first one, you can simply just copy and paste to add rows for the second schedule. Your directions are as follows: Create a loan amortization schedule using Excel for a $36,000 car loan that will be repaid over 60 months at an annual interest rate of 4.5%. What is your monthly payment? What is the total amount you have paid over the life...
Suppose that you are going to receive two payments of $500, the first at the end...
Suppose that you are going to receive two payments of $500, the first at the end of year 3, and the other at the end of year 6. First, draw the time line and mark the time (years) and payments on the timeline. How much are you willing to pay for those cash flows (CFs) today if the current discount rate is 5%? What if the discount rate is 6% instead? How much would you be willing to pay in...
use C++ to create a function to calculate it. My requirements are very simple: function should...
use C++ to create a function to calculate it. My requirements are very simple: function should be general to deal with any size matrices. That's all.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT