Write a C program to include two functions to swap two integ

Write a C++ program to include two functions to swap two integer values which you get from the user. a- Write two version of the swap function, one version using call by value and the other version use call by reference. Using call by reference you should not print anything inside the function. Test your program and output the swapped values for both versions. b- Write two overloaded functions to swap two double and two character values (use call by reference).

Solution

Answer:

Call by value -

void main()

{

int a,b;

printf(\"Enter values for a & b\");

scanf(\"%d %d\",&a,&,b);

printf(\"Before swap %d %d\",a,b,);

swap(a,b); getch();

}

int swap(int x, int y)

{

x = x+y;

y = x - y;

x = x - y;

printf(\"After swap %d %d\",x,y);

return 1;

}

Call by reference -

void main()

{

int a,b;

printf(\"Enter values for a & b\");

scanf(\"%d %d\",&a,&,b);

printf(\"Before swap %d %d\",a,b,);

swap(&a,&b);

printf(\"After swap %d %d\",a,b);

getch();

}

void swap(int *x, int *y)

{

*x = *x+*y;

*y = *x - *y;

*x = *x - *y;

}

Write a C++ program to include two functions to swap two integer values which you get from the user. a- Write two version of the swap function, one version usin

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site