Consider the following C++ program. Assume that all the function calls are indep
ID: 3697611 • Letter: C
Question
Consider the following C++ program. Assume that all the function calls are independent that is no function call affects other functions’ calls for each of the parameter passing method. void swap(int a, int b){ int temp = a; a = b; b = temp; } int main(){ int value = 2, list[5] = {1, 3, 5, 7, 9}; swap(value, list[0]); swap(list[0], list[1]); swap(value, list[value]); } What are the values of the variables value and list after each of the three calls to swap for each of the following parameter-passing methods? a) Pass-by-value: swap(value, list[0]) swap(list[0], list[1]) swap(value, list[value]) b) Pass-by-reference: swap(value, list[0]) swap(list[0], list[1]) swap(value, list[value]) c) Pass-by-value-reference: swap(value, list[0]) swap(list[0],list[1]) swap(value, list[value])Explanation / Answer
void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}
int main(){
int value = 2,
list[5] = {1, 3, 5, 7, 9};
swap(value, list[0]);
swap(list[0], list[1]);
swap(value, list[value]);
}
What are the values of the variables value and list after each of the three calls to swap for each of the following parameter-passing methods?
a) Pass-by-value: // No change in original list and variable value
swap(value, list[0]) :
value = 2
list : {1, 3, 5, 7, 9}
swap(list[0], list[1])
value = 2
list : {1, 3, 5, 7, 9}
swap(value, list[value])
value = 2
list : {1, 3, 5, 7, 9}
b) Pass-by-reference:
swap(value, list[0])
value = 1
list : {2, 3, 5, 7, 9}
swap(list[0], list[1])
value = 2
list : {3, 1, 5, 7, 9}
swap(value, list[value])
value = 5
list : {1, 3, 2, 7, 9}
c) Pass-by-value-reference:
swap(value, list[0])
value = 1
list : {2, 3, 5, 7, 9}
swap(list[0],list[1])
value = 2
list : {3 1, 5, 7, 9}
swap(value, list[value])
value = 5
list : {1, 3, 2, 7, 9}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.