1) Consider the function fun to be designed as: int fun(int *k){ *k += 4; return
ID: 3688517 • Letter: 1
Question
1) Consider the function fun to be designed as:
int fun(int *k){
*k += 4;
return 3 * (*k) – 1;
}
int main(){
int i = 20, j = 20, sum1, sum2;
sum1 = (i / 2) + fun(&i);
sum2 = fun(&j) + (j / 2);
}
Show full details of your results or computations. No credit will be given for any unexplained answer.
What are the values of sum1 and sum2 in the following two cases if
(a)The operands in the expressions are evaluated left to right?
(b)The operands in the expressions are evaluated right to left?
Explanation / Answer
a)If the Operands in the expressions are evaluated left to right
sum1=71
sum2=83
Explanation:
Given i=20,j=20
sum1=(i/2)+fun(&i)
here i/2=10
fun(&i) evaluates k=24,returns 3*24-1=71,
sum1=(i/2)+fun(&i)
sum1=10+71=81
sum2=fun(&j)+(j/2)
fun(&j) evaluates k=24.returns 3*24-1=71, the value of j becomes 24
sum2= fun(&j)+(j/2)
=71+(24/2)=83.
(b)The operands in the expressions are evaluated right to left?
sum1=81
sum2=79
Explanation:
Given i=20,j=20
sum1=(i/2)+fun(&i)
fun(&i)modifies i value to 24, and returns 3*24-1 right to left evaluation gives 3*23=69
sum1=(24/2)+69=81
sum2=fun(&j)+(j/2) right to left evaluation gives
=fun(&j)+(20/2)
=fun(&j)+10
=69+10=79
fun(&j) updates j=24 and returns 3*24-1,right to left produces 3*23=69
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.