Pass by Reference
Completed
Write void doubleIt(int &x) that doubles its argument in place. Read n numbers, double each with your function, and print them.
Input
Line 1: n. Line 2: n integers.
Output
One line.
Example
Input
4 1 2 3 4
Output
2 4 6 8
Editor
C++
Hints
int &x is a reference: changes to x change the caller's variable.
Solution explanation
References are C++'s cleaner alternative to pointers for "modify my argument".
Show solution
#include <iostream>
using namespace std;
void doubleIt(int &x) {
x *= 2;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
doubleIt(x);
cout << x << (i < n - 1 ? " " : "\n");
}
return 0;
}