Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

At each of the five commented lines in the following procedural program: - Write

ID: 3729853 • Letter: A

Question

At each of the five commented lines in the following procedural program:
- Write out the contents of the type environment during typechecking
- Write out the contents of the evaluation environment (the stack) during
evaluation for each time the line is hit during execution, assuming execution starts with main().

num distance(num x, num y) {
bool b = x < y;
num d = 0;
// 1

if b {
num z = y - x;
d = z;
// 2
} else {
num w = x - y;
d = w;
// 3
}

// 4
return d;
}

unit main() {
num x = distance(1, 2);
num y = distance(4, 3);
// 5
}

Please, TYPE the answer CLEARLY with some explanation if possible.

Explanation / Answer

If we will compile this code, the code will compile from top to bottom,but execution will start from main() function only.So main() function is calling another function two times namelly distance().

// Case-1 (1st call)

In 1st call of distance() function the two actual argument is passing i.e (1,2).Whenever distance is incountered it receive value formally in variables i.e (x, y).so here x is 1 and y is 2.

There is a veriable called b of bool type which will store true and false value i.e (1 or 0) respectively.So comparison of x and y will retuer true (1) value in b variable because x i.e(1) is less than y i.e (2),so become true means 1.

The next is 'if' statement, the part of code inside 'if' statement will execute when value of variable b is true.here in 1st case 1<2 is true to 'if' will execute.

Inside ''if' part substraction will give result as 1 (x-y).so z become 1 and same result is initialised in d variable i.e (d = z), so finally d value is return back to main() function and stored in x variable, so x become 1.

Case-2 ( after finishing the 1st call 2nd call will happen from main() to same function distance() ).

Now x will receive 4 and y will receive 3.So in comparison between x and y ,the variable b will get false (0) value because 4 is not less than 3.

If b is zero so 'if' statement will not execute,here ''else" part will execute.And substraction will give result as 1 i.e(4-3).So we will initialised with one and same value is going to d variable.and finally d value is return back to main() function, so y variable in main() function will receive result as a 1.

Thankyou.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote