Question

In: Computer Science

Assume that the following Ada like program compile successfully. int k = 0; A[3] = {0,...

Assume that the following Ada like program compile successfully.

int k = 0;
A[3] = {0, 1, 2};

void f(int p) {
     cout<< A[k];
     p*=3;
     cout<< A[k];
     p*=3;
     cout<< A[k];
}

int main () {
     f(A[k++]);        // postfix
     cout << A[1] << A[2] << endl;
{

   What is printed on the screen assuming?

  1. Pass by value
  2. Pass by reference
  3. Pass by value-result (assume lvalue is computed at call time)
  4. Pass by value-result (assume lvalue is computed at run time)

Solutions

Expert Solution

Solution:

1. 11112

2. 13992

Caller is the function which calls another function callee

In pass by value-result, just before the control is transferred back to the caller by the callee, the value of the formal parameter is transmitted back to the actual parameter. To put it in other words - Pass by value-result executes in the same way as pass by value, only difference is when the the function exits - the actual parameter value gets the value of the formal parmeter.

3. 11192

This is the case with when the lvalue is computed at call time, as the value of the formal parameter is transmitted back to the actual parameter when the function exits and returns back to the caller.

In main, function f is called as f(A(1)) (since its A(++k) and k is 0) = > f(1)

int k = 0; A[3] = {0, 1, 2}
void f(int p) { // p=1
cout<<A[k]; // prints 1, since k=1
p = p * 3; // p becomes 3 as initially its 1
cout<<A[k]; // prints 1, since k=1
p = p * 3; // p becomes 9 as initially its 3
cout<<A[k]; // prints 1, since k=1
} // at this point the value of p is transferred to A[1]
// in int main from where it was called. Thus A[1] = 9

int main(){
f(A[++k]); // the value 1 is passed to the function
cout<<A[1]<<A[2]; // prints 9 and 2
}
4. 11192

Same is the case with call by value result when lvalue is computed at runtime, as the value of the formal parameter is transmitted back to the actual parameter when the function exits and returns back to the caller.

int k = 0; A[3] = {0, 1, 2}
void f(int p) { // p=1
cout<<A[k]; // prints 1, since k=1
p = p * 3; // p becomes 3 as initially its 1
cout<<A[k]; // prints 1, since k=1
p = p * 3; // p becomes 9 as initially its 3
cout<<A[k]; // prints 1, since k=1
} // at this point the value of p is transferred to A[1]
// in int main from where it was called. Thus A[1] = 9

int main(){
f(A[++k]); // the value 1 is passed to the function
cout<<A[1]<<A[2]; // prints 9 and 2
}
Let me know of the issues if any, and please give me a like...thank u....


Related Solutions

int main() { system("ls"); return 0; } Please compile the above program, and change its owner...
int main() { system("ls"); return 0; } Please compile the above program, and change its owner to root, and make it a Set-UID program. Can you let this Set-UID program run your code instead of /bin/ls? If you can, is your code running with the root privilege? Describe and explain your observations.
public void printQueue(DoublyLinkedQueue<Integer> Q){ int len = Q.size(); int k = 0; for (int i=0; i...
public void printQueue(DoublyLinkedQueue<Integer> Q){ int len = Q.size(); int k = 0; for (int i=0; i < len; ++i){ k = Q.dequeue(); Q.enqueue(k);    System.out.println(Q.dequeue()); } } What is the complexity of this code? Select one: a. O(N), N = k b. Q(1) c. Q(N2), N = Q.size() d. O(M), M = Q.size() e. None of the alternatives is correct. which one?
My java program will not compile. I receive the following error; Test.java:9: error: incompatible types: int[]...
My java program will not compile. I receive the following error; Test.java:9: error: incompatible types: int[] cannot be converted to int int size = new int [start]; //Java Program import java.util.Scanner; public class Test{ public static void main(String []args) { int start, num; System.out.println("Enter the number of elements in a array : "); start = STDIN_SCANNER.nextInt(); int size = new int [start]; System.out.println("Enter the elements of array where last element must be '0' : "); for(int i = 0; i...
How many times is "invalid" displayed in the following loop? for (int k=o; k<3; K++) {...
How many times is "invalid" displayed in the following loop? for (int k=o; k<3; K++) { for(int j =0; j < 4; j++) { cout << "invalid"; } }
int a = 3; int b = -2; if((a>0)&&(b>0)){ if (a>b) { System.out.println("A"); } else {...
int a = 3; int b = -2; if((a>0)&&(b>0)){ if (a>b) { System.out.println("A"); } else { System.out.println("B"); } } else if ((b<0)||(a<0)) { System.out.println("C"); } else { System.out.println("D"); }
Consider the following function: int counter( int n) { int s = 0;    for ( int...
Consider the following function: int counter( int n) { int s = 0;    for ( int i = 0; i < n; i++)             for ( int j = i; j > 0; j--)                        s = s + 1;   return s; } Part (a): How many times "s=s+1;" will be executed? Determine a precise count.  Show all your work. Part (b): What is the time complexity of the "counter" function in terms of Big-O notation? Justify and show all your work.
(True or False) The following function will compile. void print(int num) { return num+1; } Group...
(True or False) The following function will compile. void print(int num) { return num+1; } Group of answer choices True False Flag this Question Question 2 10 pts (True or False) The following code will output "I was true". bool isGreater(string s1, string s2) { if(s1 > s2) { return true; } return false; } int main() { if(isGreater("before","back")) { cout << "I was true"; } else { cout << "I was false"; } return 0; } Group of answer...
Given the following int variables which represent a date, int day; // 0-6 for Sunday-Saturday int...
Given the following int variables which represent a date, int day; // 0-6 for Sunday-Saturday int month; // 1-12 for January-December int date; // 1-31 int year; // like 2020 Define the function void tomorrow(int& day, int& month, int& date, int& year); which updates the variables to represent the next day. Test in main().
Consider the following program written in C syntax: void swap(int a, int b) { int temp;...
Consider the following program written in C syntax: void swap(int a, int b) { int temp; temp = a; a = b; b = temp;} void main() { int value = 2, list[5] = {1, 3, 5, 7, 9}; swap(value, list[0]); swap(list[0], list[1]); swap(value, list[value]); } For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three calls to swap? Passed by value Passed by reference Passed...
Analyze following program and Find Syntax errors. #include<iostream> int show(int x) int main() {     int A,B;...
Analyze following program and Find Syntax errors. #include<iostream> int show(int x) int main() {     int A,B;       char B=10; cout<<”Enter Value of A”; cin<<A; show(A) show(50)          }       cin.get(); } int show(int x); { cout<<”Value is =” <<X cout<<endl; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT