Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a programmer defined function with void return type that swaps two charact

ID: 3801912 • Letter: W

Question

Write a programmer defined function with void return type that swaps two characters. Your main function must declare two integers and ask the user for their values. In your main function, display the characters as well as the memory addresses of the respective variables. Now, call your function to swap the value in the character variables. Inside your function, display the value of the characters before and after swapping, and the memory addresses occupied by the variables in your function. After returning to your main function, display the value of character variables. Use pass by reference method when calling your function. Problem 3. Repeat Problem 2 but pass by value this time. Don't worry if your swap is not successful. The objective of this program is to demonstrate difference between pass by value v/s pass by reference.

Explanation / Answer

#include void swap(int *num1, int *num2); void swapV(int num1, int num2); int main() { int x, y; printf(" Enter First number : "); scanf("%d", &x); printf(" Enter Second number : "); scanf("%d", &y); printf(" Before Swaping x = %d and y = %d", x, y); printf(" Before Swaping address x = %p and y = %p", &x, &y); swap(&x, &y); // Function Call - Pass By Reference printf(" Before Swaping address x = %p and address y = %p", &x, &y); printf(" After Swaping x = %d and y = %d", x, y); printf(" Passby value example"); printf(" Before Swaping x = %d and y = %d", x, y); printf(" Before Swaping address x = %p and y = %p", &x, &y); swapV(x, y); //function calll passs by value printf(" After Swaping x = %d and y = %d", x, y); printf(" Before Swaping address x = %p and y = %p", &x, &y); return 0; } void swapV( int a, int b ) { int t; t = a; a = b; b = t; } void swap(int *num1, int *num2) { int temp; temp = *num1; *num1 = *num2; *num2 = temp; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote