Question

1. f (m, n) m- n* 10; main int z[] 10, 20, 30; print(z [w], z[0]); Given the code above, what is the output of the program if

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1) Using pass by value:

Output: 10 10

Explanation: Here the value of w is 0 so z[w] becomes equivalent to z[0] that is 10. Since we are using pass by value mechanism, therefore, the actual parameters that are z[w] and z[0] will be copied to the formal parameters m and n but since m and n are the copies so any changes made on m and n will not be reflected to z[w] and z[0]. Therefore output becomes 10 and 10.

2) value result and address of z[w]

If the value of z[w] is returned by the procedure and is computed at the time of call, in this fashion given below:

int f(int m,int n)

{

int w;

w = w + 1;

n = 3;

m = n*10;

return m;

}

int main()

{

int w = 0;

int rv;

int z[] = {10,20,30};

rv = f(z[w],z[0]);

printf(rv);

printf(z[w],z[0]);

}

then,

Output : 30 10 10 //address return value of z[w] and value of z[w] and z[0]

Explanation: Returning m will return m = 3*10

Rest of the part is same as explanied for the sub question 1 (return by value mechanism).

3) Passed by Reference

Output : 30 30

Explanation; Since we are passing the reference of the z[w] and z[0] to m and n, So any changes made on to m and n will be reflected on z[w] and z[0]. Since we are manipulating the values of m and n in the definition of our function f(), the value of m and n becomes 30 and 30 and consequently, z[w] and z[0] becomes 30 and 30.

4) Passed by name

Output ; 30 30

Explanation: Passing by name is somewhat equivalent to passing by reference. "But they are not the same". When we use passing by name mechanism what happens is that the actual parameters are substituted into the function definition and it is allowed to read as well as write the actual parameters within the function body itself.

In this case z[w] and z[0] if are "passed by name" then m and n are substituted by z[m] and z[0] and all the operations that happen inside the function body on m and n are equivalent to operations done on z[w] and z[0] . Therefore are directly reflected on z[w] and z[0].

Therefore the value becomes 30 30  

Add a comment
Know the answer?
Add Answer to:
1. f (m, n) m- n* 10; main int z[] 10, 20, 30; print(z [w], z[0]);...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. f(m, n) w = w+1; n = 3; m = n * 10; main W...

    1. f(m, n) w = w+1; n = 3; m = n * 10; main W = 0; int z[] = {10, 20, 30}; f(z[w], z[0]); print (z[w], z[0]); Given the code above, what is the output of the program if the values are passed by: a) Value. b) Value-result and address of z[w] is computed at the time of the call. c) Reference. d) Name. (Note: The order of evaluating the parameters of a subprogram are from left to...

  • Consider the following code C++ like program: int i, j, arr[5]; //arr is an array starting...

    Consider the following code C++ like program: int i, j, arr[5]; //arr is an array starting at index 0 void exchange(int x, int y) { int temp:= x; x:= y; y:= temp; } main(){ for (j = 0; j < 5; j++) arr[j]:= j; i:= 1; exchange(i, arr[i+1]); output(i, arr[2]); //print i and arr[2] } What is the output of the code if both parameters in function swapping are passed by: a- value? b- reference? c- value-result?

  • Given the following program in a C-like syntax, what does F1(m) return assuming static scoping and...

    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....

  • Demo Your instructor will now debug the below faulty QuickSort example int size 8,W makes code...

    Demo Your instructor will now debug the below faulty QuickSort example int size 8,W makes code simpler void swapint "xp, int yp) yp-semp int partitionlint intArry int left, int right, int pivot) t int left Pointer left in righPointer right; printfi-Calling quick sort, index %d to %d with pivot %d、m"lea, righe, pivot while) N korp increasing left pointer until run into something greater thon pivet W keep decrcasing right pointer until nun into something smaller than piv 1 else t...

  • 1-Is it possible to run a program without a main() function? Yes No 2- How many...

    1-Is it possible to run a program without a main() function? Yes No 2- How many main() functions can one have in a program? 2 This depends on what compiler you use. As many as you like 1 3- What does the following code fragment leave in x? char a = 'A'; int x = sizeof (a); 1 Depends on what compiler you use. 4 2 4- True or false: In a C program I can have two functions with...

  • XCORE Question 1 Consider the following program void Elint x, int y Y = y +...

    XCORE Question 1 Consider the following program void Elint x, int y Y = y + 1 cout<<x<<"*«y << endl; void nain) 1 int i, a13): all) = 15; a 2) - 203 a13) = 25; cout <i«"" <all) <<"" << a12) << ""« a[3] << endl; cout <i<** <all) << "" << a12) <<""« a[3] << endl; What values of the variable and array A are printed with the following rules. a parameters are passed by value bi parameters...

  • 7. What is the output of the following program? #include <iostream> int f(int n, int &...

    7. What is the output of the following program? #include <iostream> int f(int n, int & v, int * p) { V = *p; v = y + 1; return n+ (*p); int main() { int n = 10; int m = 20; std::cout << fin, n, & m); a. 30. b. 31. c. 41. d. 42. e. An error occurs. 8. What is the output of the following program? Note that both the signature and the body of the...

  • #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”,...

    #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”, “Hanenburg”, “Rhoderick”, “Pearce”, “Raymond”, “Kamphuis”}; for(int i=0; i<8;i++){ for(int j=0; j<9; j++){ if(strcmp(array[j],array[j+1])>0){ char temp[20]; strcpy(temp,array[j]); strcpy(array[j],array[j+1]); strcpy(array[j+1],temp); } } } printf(“---------File Names---------\n”); for(inti=0; i<9; i++){ printf(“\t%s\n”,array[i]); } printf(-------5 Largest Files according to sorting----\n”); for(int i=0;i>=5;i--) { printf(“\t%s\n”,array[i]); } return0; } Consider the "sort" program (using with void* parameters in the bubblesort function) from the week 10 "sort void" lecture. Modify it as follows...

  • Using C programming language Question 1 a) through m) Exercise #1: Write a C program that...

    Using C programming language Question 1 a) through m) Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. a. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. b....

  • (a) Consider the following C++ function: 1 int g(int n) { 2 if (n == 0)...

    (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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT