Write a program with two functions: main and swap. The main function will read t
ID: 3642094 • Letter: W
Question
Write a program with two functions: main and swap. The main function will read two integer numbers (the first read into a variable called x while the second read into a variable called y) and then calls the function swap that accepts the pointers to the two variables x and y respectively. The swap function should swap the value of the two variables. The main function should then print the values of the variables x and y.
Sample Run:
Enter the first number
80
Enter the second number
70
The value for x is: 80.
The value for y is: 70.
After swapping:
The value for x is: 70.
The value for y is: 80.
Explanation / Answer
#include <iostream>
using namespace std;
void swap(int &x, int &y);
int main() {
int one, two;
cout << "Enter the first number: ";
cin >> one;
cout << "Enter the second number: ";
cin >> two;
cout << "The value for x is " << one << "." << endl;
cout << "The value for y is " << two << "." << endl;
swap(one, two);
cout << "The value for x is " << one << "." << endl;
cout << "The value for y is " << two << "." << endl;
system("pause");
}
void swap(int &x, int &y) {
int temp = x;
x = y;
y = temp;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.