Programing in C 4. Following function swap the values of two variables a and b.
ID: 655441 • Letter: P
Question
Programing in C
4. Following function swap the values of two variables a and b. For example, if value of a is 4 and value of b is 5, then the function will change the value of a to 5 and the value of b to 4. However, when you call this function, the values of the variables, x, y, in the calling function(function main()) are not going to be swapped. This is because parameter passing in C isby value (i.e., values are copied from the calling function to the callee function and the variables in callee function are different from those in the calling function).
void swap(int a, int b) {
int tmp = a; //Statement 1
a = b; //Statement 2
b = tmp; //Statement 3
}
int main() {
int x = 4, y = 5;
swap(x, y); //Statement 4
}
4.1 Give the value stored in each of the variables x, y, a, b, and tmp after each of the numbered statements. Statement 4 (note that statement 4 is executed first): The value of a is____ The value of x is____ The value of b is____ The value of y is____ The value of tmp is____
Statement 1:
The value of a is____ The value of x is____
The value of b is____ The value of y is____
The value of tmp is____
Statement 2:
The value of a is____ The value of x is____
The value of b is____ The value of y is____
The value of tmp is____
Statement 3:
The value of a is____ The value of x is____
The value of b is____ The value of y is____
The value of tmp is____
4.2 To solve the problem, the swap function needs to use pointer variables as its input parameters so that swap occurs in the variables in the calling function:
void swap(int *a, int *b) {
int tmp = *a; //Statement 1
*a = *b; //Statement 2
*b = tmp; //Statement 3
}
int main() {
int x = 4, y = 5;
swap(&x, &y); //Statement 4
}
Again give the value stored in each of the variables x, y, a, b, and tmp after each of the numbered statements (you can simply write the value of variable x
Explanation / Answer
4.1
Steps of execution
The program starts executing from the main function.
In the assignment statements, 4 is assigned to int variable 'x' and 5 is assigned to 'y'
Next, Statement 4 is executed which runs the swap(x,y) and calls the swap(int a,int b) passing the values 4 and 5 from x,y to
the local int variables a, b respectively.
Next the three assignment statements Statement 1, Statement 2, Statement 3 are executed in that order.
Statement 1 assigns the valve in 'a' which contains 4 into int variable'tmp'
Statement 2 assigns the valve in 'b' which contains 5 into 'a'
Statement 3 assigns the valve in 'tmp' which contains 4 into 'b'
Answer
Statement 1
The value of a is 4 The value of x is 4
The value of b is 5 The value of y is 5
The value of tmp is 4
Statement 2
The value of a is 5 The value of x is 4
The value of b is 5 The value of y is 5
The value of tmp is 4
Statement 3
The value of a is 5 The value of x is 4
The value of b is 4 The value of y is 5
The value of tmp is 4
4.2
Steps of execution
The program starts executing from the main function.
In the assignment statements, 4 is assigned to int variable 'x' and 5 is assigned to 'y'
Next, Statement 4 is executed which runs the swap(&x,&y) and calls the swap(int *a,int *b)
The addresses of variables 'x' and 'y' are passed to pointer variables 'a' and 'b'
Next the 3 assignment statements Statement 1, Statement 2, Statement 3 are executed in that order.
Statement 1 assigns the valve in the address which is in 'a' which contains 4 into the int variable 'tmp'
Statement 2 assigns the valve in the address which is in 'b' which contains 5 into the address which is in 'a'
Statement 3 assigns the valve in 'tmp' which contains 4 into the address which is in 'b'
Answer
Statement 4:
The value of a is &x The value of x is 4
The value of b is &y The value of y is 5
The value of tmp is. Not applicable as 'tmp' is not yet created
Statement 1:
The value of a is &x The value of x is 4
The value of b is &y The value of y is 5
The value of tmp is 4
Statement 2:
The value of a is &x The value of x is 5
The value of b is &y The value of y is 5
The value of tmp is 4
Statement 3:
The value of a is &x The value of x is 5
The value of b is &y The value of y is 4
The value of tmp is 4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.