Question

In: Computer Science

Consider the following program written in C syntax: void swap(int a, int b) { int temp;...

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

  1. Passed by value
  2. Passed by reference
  3. Passed by value-result

Solutions

Expert Solution

Passed by value

In passed by value method only the copys of actual variables are passed as parametrs.Which means whatever changes are happened to parameters in swap function it will not reflect on the actual variables

so in our program,

There will be no change for 'value' and 'list' after exexution of the program.Becouse whenever we call swap using pass by value method we are jsut passing a copy of it

so Lets check the changes after each line of execution

at beginning : value= 2 ,list = {1, 3, 5, 7, 9}

swap(value, list[0]);

value= 2 ,list = {1, 3, 5, 7, 9}

swap(list[0], list[1]);

value= 2 ,list = {1, 3, 5, 7, 9}

swap(value, list[value]);

value= 2 ,list = {1, 3, 5, 7, 9} //no change

Passed by reference

In passed by reference method ,reference to the actual variable is being passed .So for every change in the formal parameters in swap function will reflect on the actual variables in the main functions. Actually we are passing a reference to the actual variable into the swap function.so that the swap function can change the variable in main() using that reference

Lets check the changes after each line of execution

At beginning:

value = 2, list = {1, 3, 5, 7, 9}

swap(value, list[0]);

value = 1, list = {2, 3, 5, 7, 9}

swap(list[0], list[1]);

value = 2, list = {3, 2, 5, 7, 9}

swap(value, list[value]);

value = 5, list = {1, 3, 2, 7, 9}

Passed by value-result

This method of parameter passing is similar to pass by referenece ,but instead of changing the actual variables at every execution this method will change the actual variable only once when the function ends.Which means the sawp function will change the value of actual variable at end of the swap function only

But this method have no effect in our program, it will be same as pass by reference method shown above

(This methods have effects in functions in which values of formal variables changed several times in called function)

So lets check the changes at every line of execution

At beginning:

value = 2, list = {1, 3, 5, 7, 9}

swap(value, list[0]);

value = 1, list = {2, 3, 5, 7, 9}

swap(list[0], list[1]);

value = 2, list = {3, 2, 5, 7, 9}

swap(value, list[value]);

value = 5, list = {1, 3, 2, 7, 9}


Related Solutions

Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
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; }
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
Make a function definition in C for the following: void insert (double *b, int c, double...
Make a function definition in C for the following: void insert (double *b, int c, double s, int pos); //Insert value s at position pos in array. //needs: // c > 0, pos >= 0, and pos <= c-1. Elements b[0]...b[c-1] exist. //this will do: //Elements from indexes pos up to c-2 have been moved up to indexes pos+1 up to c-1. The value s has been copied into b[pos]. //Note: the data that was in b[c-1] when the function...
R5.18The following function swaps two integers, without requiring a temporary variable(C++): void tricky_swap(int& a, int& b)...
R5.18The following function swaps two integers, without requiring a temporary variable(C++): void tricky_swap(int& a, int& b) { a = a - b; b = a + b; a = b - a; } However, it fails in one important case, namely when calling tricky_swap(x, x). Explain what should happen and what actually happens.
Consider the following program: 1 #define Size 64 int A[Size; Size], B[Size; Size], C[Size; Size]; int...
Consider the following program: 1 #define Size 64 int A[Size; Size], B[Size; Size], C[Size; Size]; int register i, j; for (j = 0; j< Size; j ++) { { for (i = 0; i< Size; i++) C[i; j] = A[i; j] + B[i; j]; } } Assume that the program is running on a system using demand paging and the page size is 1 Kilobyte. Each integer is 4 bytes long. It is clear that each array requires a 16-page...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT