Read the question, write C++ on the right, then Run or Check.
QuestionHint and solution stay closed until you open them
Complete swap so it exchanges two ints using references (not pointers).
main reads a and b, calls swap(a, b), and prints them. After the call, print the original b then the original a.
Input. Two integers a and b.
Output. b then a, separated by a space.
Constraints
- You must change the values through the references. Do not only print in reverse order inside main.
Examples
Input
7 2
Output
2 7
Input
0 9
Output
9 0
Hint
- void swap(int& a, int& b) — the & means an alias, not an address you must dereference.
- int tmp = a; a = b; b = tmp;
Show correct code
Peek only after you have tried. You can still Check your own version.
#include <iostream>
void swap(int& a, int& b) {
int tmp = a;
a = b;
b = tmp;
}
int main() {
int a, b;
std::cin >> a >> b;
swap(a, b);
std::cout << a << " " << b << "\n";
return 0;
}
main.cppC++17 · g++ · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.