Write a program that will change the value of an integer variable with initial value of 654,321 to 27,946 without directly assigning a value to the variable. You cannot create any pointersor references in the main function.
I wrote mine like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include "_pause.h"
usingnamespace std;
void passByRef(int x){
x = 27946;
cout << "The value of x now is " << x << endl;
}
int main(){
int x = 654321;
cout << "The value of x is " << x << endl;
passByRef(x);
_pause();
return 0;
}
Output:
The value of x is 654321
The value of x now is 27946
please correct me if I am wrong, or share yours on how you solve it.
That said, what the question is talking about, I have no idea. Without assigning a value, there is no way to do this, and "directly" isn't exactly a well-defined term.
Maybe something like:
1 2
int x = 654321; // starting value
x = x ^ 628443; // now x is 27946 and we didn't set it "directly"
You use it as demonstrated in line 6 of the code above.
Questions such as you have rarely happen in a vacuum. If these are tasks being set as part of some kind of education, then whatever you have been learning about before the task is likely to be relevant to how you're meant to do it.