3. What are the values of sum1 and sum2 if the operands in the expressions are e
ID: 3711740 • Letter: 3
Question
3. What are the values of sum1 and sum2 if the operands in the expressions are evaluated left to right ? Must include explanationint fun(int *k){ *k += 10; return 5 * (*k); } void main(void){ int a = 6, b =12, sum1, sum2; sum1= (a/2) + fun(&a) sum2 = fun(&b) + (b/2); } 3. What are the values of sum1 and sum2 if the operands in the expressions are evaluated left to right ? Must include explanation
int fun(int *k){ *k += 10; return 5 * (*k); } void main(void){ int a = 6, b =12, sum1, sum2; sum1= (a/2) + fun(&a) sum2 = fun(&b) + (b/2); }
int fun(int *k){ *k += 10; return 5 * (*k); } void main(void){ int a = 6, b =12, sum1, sum2; sum1= (a/2) + fun(&a) sum2 = fun(&b) + (b/2); }
Explanation / Answer
Solution:
Value of sum1 = 83
Value of sum2 = 121
Explanation:
a) sum1 = (a/2) + fun (&a)
sum1 = (6/2) + 80 = 3 + 80 = 83
because when we pass the address of 'a' in function fun, then the changed value of 'a' will be reflected in the calling function. Now, we see the implementation of the function fun:
* is called value at address operator.
fun(int *k) => fun(*(&6)) => fun(6)
fun(6) here k = 6 => k = 10 + 6 = 16 => 5 * 16 = 80, thus it will return 80.
b) sum2 = fun(&b) + (b/2);
similarly, sum2 = 110 + (22/2) = 110 + 11 = 121
because after calling fun(&b) the value of b will be 22. So fun(&b) would return 110 and b would 22.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.