Is it possible to explain why with your answers to help me better understand wha
ID: 3865094 • Letter: I
Question
Is it possible to explain why with your answers to help me better understand what is happening?
If x is an int variable with an initial value 5, find the value of x upon the return of the call f(x, x) to the following C++ function. void f(int& x, int& y) {x = x + y; y = x - y; x = x - y;} If x is an integer variable with an initial value 5, find the value of x upon the return of the call f (x, x) to the following Ada procedure, assuming pass by value-result. procedure f(x, y: in out Integer) is begin x: = x + y; y: = x - y; x: = x - y; end f; Draw a sequence of run-time stacks to show the activities of calling f(3) to the following C function. Show the parameter and return value in each stack frame. int f(int n) {if (nExplanation / Answer
1.
#include <iostream>
using namespace std;
void f(int& x,int& y)
{
x = x + y; //x = 10 y = 10 as y and x share the same copy of x reference variable
y = x - y; // y = 10-10 = 0
x = x - y; // x = 10 - 10 = 0
}
int main()
{
int x = 5;
f(x,x);
printf("x = %d",x); //x = 0
return 0;
}
Output:
x = 0
2.
in out parameter goes into the call and may be redefined in Ada
x := x+y; here x = 5+5 = 10
y := x-y ; y = 10-5 = 5
x := x-y; x = 10-5 = 5
after procedure executes x = 5; redefined by y.
3
f(3)
= f(2) + f(1) // recursive call
= f(1) +f(0) + f(1) // n<2 so return n
= 1+0+1 = 2
n=3 return = f(2)+f(1)Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.