Using C++ . Write a programmer defined function with void return type that swaps
ID: 3863836 • Letter: U
Question
Using C++
. 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.
HINT: Use the provided demo programs to see how you can display memory addresses.
Explanation / Answer
#include <iostream>
using namespace std;
void swap(char* ch1,char* ch2 );
int main()
{
char character1;
char character2;
cout << "Enter character 1:";
cin >> character1;
cout << "Enter Character 2:";
cin >> character2;
cout << "Character 1 Memory Address : "<<&character1<<" Value : "<<character1<<endl;
cout << "Character 2 Memory Address : "<<&character2<<" Value : "<<character2<<endl;
swap(character1,character2);
cout << "Character 1 Memory Address : "<<&character1<<" Value : "<<character1<<endl;
cout << "Character 2 Memory Address : "<<&character2<<" Value : "<<character2<<endl;
return 0;
}
void swap(char* ch1,char* ch2 ){
char temp = *ch1;
*ch1 = *ch2;
*ch2 = temp;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.