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

Consider the code shown below. What is the value of evaluating the expression su

ID: 3665096 • Letter: C

Question

Consider the code shown below. What is the value of evaluating the expression sum-squares( add(5,2), mult(10,2) ) using applicative order? Normal order?

int add(int x, int y){
     return x + y;
}


int mult(int x, int y){
     return x * y;
}


int square(int x){
     return x * x;
}


int sum-squares(int x, int y){
     return square(x) + square(y);
}

Answer:

Consider the code shown below. What does the program print when q(p(1), 2, p(3)) is evaluated using applicative order? Normal order?

int p(int i) {
     printf("%d ", i);
     return i;
}


void q(int a, int b, int c) {
     int total = a;
     printf("%d ", b);
     total += c;
}

Answer:

Consider the program shown below. What is the result of executing this program if applicative order of evaluation is used? Normal order of evaluation is used?

int f(int x){
     return 3;
}


int fib(int n) {
     // return the nth Fibonacci number
}


void main() {
     int m = f(fib(10000));
     printf("%d", m);
}

Answer:

Explanation / Answer

sum-squares( add(5,2), mult(10,2) )

= sum-squares(7, 20)

= 49 + 400

= 449.

This will be the order of evaluation for both normal and applicative orders.

q(p(1), 2, p(3))

Normal order:

q(1, 2, 3) Where 1 3 will be printed, while evaluating p(1), and p(3). Then q() will print b value, i.e., 2.

Therefore, the output will be 1 3 2

Applicative order: This will print the values:3 1 2 on a windows machine, whereas, it will print 1 3 2 on a linux machine.

Normal order:

As f(n), whatever n is, will always return 3, we need not evaluate fib(10000) given in the statement f(fib(10000)), to conclude that the result is 3.

Applicative order:

This will take a real long time to calculate the fib(10000), and will come up with a number. Then that number will be passed as input to f(), which will lead to a result of 3 to be printed.