Complete the code to declare a reference to an integer variable.
int x = 10; int& [1] = x;
The code declares r as a reference to the integer variable x.
Complete the code to modify the original variable through its reference.
int a = 5; int& ref = a; ref [1] 10; // Now a should be 10
Assigning 10 to ref changes the original variable a to 10.
Fix the error in the function parameter to accept a reference.
void increment([1] num) { num++; } int main() { int x = 3; increment(x); // x should become 4 }
Using int& allows the function to modify the original variable x.
Fill both blanks to create a function that swaps two integers using references.
void swap([1]& x, [2]& y) { int temp = x; x = y; y = temp; }
void& or const int& which prevents modification.The function parameters must be references to int to swap the original values.
Fill all three blanks to create a function that returns a reference to the larger of two integers.
int& max([1]& a, [2]& b) { return (a [3] b) ? a : b; }
const int& which prevents returning a modifiable reference.The function returns a reference to the larger integer by comparing a and b.
