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

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; }
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...
IN C Write a function in the form: void play( int key, int duration) // duration...
IN C Write a function in the form: void play( int key, int duration) // duration units are tenths of a second which generates and prints samples of sin(w*t) for t=0,1,2,...,n-1 which represent a tone corresponding to piano key number key, where: n = (duration/10.0)*8000 w = (2π440rkey-49)/8000 r = 21/12 In the main program call your function to play the first three notes of three blind mice.
*Answer in C program* Given a program as shown below: #include <stdio.h> void function1(void); void function2...
*Answer in C program* 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;...
#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[])...
#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] & b[i]; } } void func_or(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] || b[i]; } } void func_not(int a[], int result[]) { for (int i = 0; i < MAX; i = i + 1) { result[i]...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c,...
Translate the following function f to MIPS assembly code. int f(int a, int b, int c, int d) { return func(func(a,b), func(b+c,d)); } Assume the followings. • The prototype of function func is “int func(int a, int b);”. • You do not need to implement function func. The first instruction in function func is labeled “FUNC”. • In the implementation of function f, if you need to use registers $t0 through $t7, use the lower-numbered registers first. • In the...
4, Make the table project with C++. Write a function with the following interface: void multiplyTable(int...
4, Make the table project with C++. Write a function with the following interface: void multiplyTable(int num) This function should display the multiplication table for values from 1...num. For example, if the function is passed 10 when it is called, it should display the following: 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20...
/*Use recursion in the function: void getDigit( int num) /* Try this program and implement the...
/*Use recursion in the function: void getDigit( int num) /* Try this program and implement the function void getDigit( intnum) so that it displays the digits in a given number. For example, the number, 1234 consist of 1, 2, 3 and 4. This is an exercise to demonstate the use of a recursive function and the use of recusrion in C++ */ #include #include using namespace std; int main() {      int num;      cout <<"Enter an integer: ";     ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT