Sale!

Assignment #4 Given three pieces of C++ code,

$30.00

Category:
Rate this product

Assignment #4 (10 points)
1. C++
Given three pieces of C++ code, run and observe output, then answer the questions below. Note: in order to run the code you may need to edit the code such as including libraries, adding main functions etc. Also, if any syntax error spotted in the code please let instructor know or make correct based on your best judgement.

(1) void swap (int x, int y) {
int t = x;
x = y;
y = t;
return;
}
int a = 10, b = 20;
swap(a,b);
//now print a and b

(2) void swap (int *px, int *py) {
int t = *px;
*px = *py;
*py = t;
return;
}
int a = 10, b = 20;
swap (&a, &b);
//print a and b

(3) void swap (int& x, int& y) {
int t = x;
x = y;
y = t;
return;
}
int a = 10, b = 20;
swap(a,b);
//now print a and b

Question (a): what is the parameter passing method for each of the above code?
Question (b): Run the code and observe the results. Which code(s) swap two values? Which code(s) don’t? Why – briefly explain.

2. Python
def swap (x, y) :
t = x
x = y
y = t

a =10
b =20
swap(a,b)
print(a,b)

(a) What parameter passing method used in the above example?
(b) Run the above program. Based on the output please state whether the function swaps two values or not.
(c) If the above function doesn’t swap two values, write one line of Python code that would be able to swap two values.

3. Java: Write a Java function to swap two integers.

4. Examine the C++, Python, and Java codes that do swap two values, which one you think is the best, which one you think is the worse – Note: identify the language evaluation criteria you used, but no need to justify.

5. Looking at the following Python code that attempt to swap L[i]’s value with L[j]’s value.

def swap (Lst, indexA, indexB) :
t = Lst[indexA]
Lst[indexA] = Lst[indexB]
Lst[indexB] = t

L = [1,2,3,4,5,6,7,8,9,10]
i=3
j=7
print(L[i], L[j])
swap(L,i,j)
print(L[i],L[j])

Question (a): what parameter passing methods are used in each method? (note: multiple parameter passing methods may be used, in this case indicate the parameter name and its corresponding parameter passing method respectively.)

Question (b): will this code swap L[i]’s value with L[j]’s value? Run the code and observe the result.

6. Write a C++ or Java code that does similar swap action as that in Problem 5.

Open chat
Need help?
Hello
Can we help you?