Let the function fun be defined as
int fun(int *k) {
*k - = 2;
return 4 * (*k) +2 ;
}
Suppose fun is used in a program as follows:
void main() {
int i = 16, j = 16, sum1, sum2;
sum1 = (i / 2) + fun(&i);
sum2 = fun(&j) + (j / 2);
}
What is the values of sum1 and sum2
a. If the operands in the expressions are evaluated from left to
right
sum1 = sum2 =
b. If the operands in the expressions are evaluated from right to
left
sum1 = sum2 =
Thanks for the question.
Here is the completed code for this problem. Let me know if you
have any doubts or if you need anything to change.
Thank You !!
================================================================================================
a. If the operands in the expressions are evaluated from left to
right
sum 1 will be : 66
first i/2 will be evaluated 16/2 = 8
fun(&i) = 4*14+2 = 58
so sum1 will be 8+58 = 66
sum 2 will be : 65
first fun(&j) will be evaluated which is equal to 4*14+2 = 58
and in this step j is reduced to 14
then j/2 will be evaluated which is equal to 14/2 = 7
so sum2 will be 58+7=65
===========================================================================================
b. If the operands in the expressions are evaluated from right to left
sum 1 will be : 65
first fun(&i) will be evaluated which is equal to 4*14+2 =
58 and in this step i is reduced to 14
then i/2 will be evaluated which is equal to 14/2 = 7
so sum1 will be 58+7=65
sum 2 will be : 66
first j/2 will be evaluated 16/2 = 8
fun(&j) = 4*14+2 = 58
so sum2 will be 8+58 = 66
=====================================================================================
Let the function fun be defined as int fun(int *k) { *k - = 2; return...
4. Consider the following C program: (2 points) int fun( int *j){ + 11; return 9; } void main(){ int k 2; k + fun( &k); k What is the value of k after the assignment statement in main, assuming operands are evaluated left to right. operands are evaluated right to left. Please, also explain how you got the values in detail.
Write the proposed program in Java, JavaScript, and Python. Run them and compare the results. Submit source code for 3 programs. Let the function fun be defined as int fun(int* k) { *k += 4; return 3 * (*k) - 1; } Suppose fun is used in a program as follows: void main() { int i = 10, j = 10, sum1, sum2; sum1 = (i / 2) + fun(&i); sum2 = fun(&j) + (j / 2); }
Consider the following C program: int sub(int *sum) { *sum =*sum +*sum; return 10; } void main() { int num= 3; num = sub(&num)+ num; } What is the value of num after the assignment statement in main, assuming operands are evaluated left to right. operands are evaluated right to left.
Consider the following program: # include <iostream> int x = 3, y = 5; void foo(void) { x = x + 2; y = y + 4; } void bar(void) { int x = 10; y = y + 3; foo( ); cout << x << endl; cout << y << endl; } void baz(void) { int y = 7; bar( ); cout << y << endl; } void main( ) { baz( ); } What output does this program...
16 Points) Question 3 Write down the outputs of the following program into the provided table include <iostream> using namespace std; void fun I(int a); int fun2(int a, int b); int x-3: int main) int x-1,y 0,z-2; x-fun2(y,z); cout sx fun 1 (z); cout (#xtytz(endl; y-fun2(x,x); cout <exty+zscendl; system("pause"); void fun 1 (int a) int fun2(int a, int b) int static c2; return atx;
16 Points) Question 3 Write down the outputs of the following program into the provided table...
int *fun(int *p) { while(*p >= 0) p++; return p; } void main() { I int *a; int v[8]={1,2,3,-4,5,6,7,8); q = fun(); printf("%d", _Missing_1_); printf("%d", -_Missing_2_); } However, part of the code is missing (indicated by). The code is supposed to give the output -46 What can the missing parts be? DS (4 Points) Missing 1: * Missing 2: [2] Missing_1: V[4] Missing 2: [2] Missing 1: *q Missing 2: q[1] Missing 1: *(q+1) Missing 2: *(q+2)
Given the following program in a C-like syntax, what does F1(m) return assuming static scoping and dynamic scoping? You can assume that expressions are evaluated from left to right. int m = 3; int F1(int i) { return (i + F2(m) + m); } int F2(int j) { j++; return (j * m); } int main() { int m = 5; F1(m); } Explain in details how you got the answers to earn full credit. Don't just write the answer....
(a) Consider the following C++ function: 1 int g(int n) { 2 if (n == 0) return 0; 3 return (n-1 + g(n-1)); 4} (b) Consider the following C++ function: 1 bool Function (const vector <int >& a) { 2 for (int i = 0; i < a. size ()-1; i ++) { 3 for (int j = i +1; j < a. size (); j ++) { 4 if (a[i] == a[j]) return false; 5 6 } 7 return...
Write a C++ function int calculate(char, int , int ) which takes a char of either ‘+’, ‘ ‘--’, ‘*’, or ‘/’ as operator and two integers as operands. The function should return the result of applying the corresponding operator on the operands if all of them are of proper types and values. It should throw a char exception if the operator is unknown and a char* exception if the operator is ‘/’ and the second operand is zero. Write...
Help with a question in Java: What is the output from the following program? public class Methods2 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { for (int j = 0; j <= i; j++){ System.out.print(fun(i, j) + "\t"); } System.out.println(); } } static long fun(int n, int k) { long p = 1; while (k > 0){ p *= n; } return p; } }