how do you solve this problem

closed account (9G3v5Di1)
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"

using namespace 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.
Last edited on
x = 27946;

That looks like directly assigning a value.


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" 
closed account (9G3v5Di1)
what that line 2 mean? I added 27946 to that but didn't get the first value
I added 27946 to that but didn't get the first value

No. That code already sets x to 27946.


1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
  int x = 654321; // starting value
  x = x ^ 628443;
  std::cout << x << '\n';
}

http://cpp.sh/95275


closed account (9G3v5Di1)
what how!! can you explain what ^ 628443 mean?
^ is the bitwise XOR operator.
closed account (9G3v5Di1)
how do you even use that
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.
Last edited on
1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
  int x = 654321;
  x -= 626375;
  std::cout << x << '\n';
}
Topic archived. No new replies allowed.