This is a discussion on Please help me to make this c++ code work. Only needs help with one part.? within the C++ Programming forums, part of the General Programming Boards category; The only problem is this part: Code: " num[i] ^= num[i+1]; num[i+1] ^= num[i]; num[i] ^= num[i+1]; " #include <iostream> ...

1. ## Please help me to make this c++ code work. Only needs help with one part.?

The only problem is this part:
Code:
```" num[i] ^= num[i+1];
num[i+1] ^= num[i];
num[i] ^= num[i+1]; "

#include <iostream>
#include <cmath>

using namespace std;

double integrate(double lower, double upper);
double f(double x);

int main()
{
int i, j;
int ranges[][2] = {{0,1},{1,2},{2,3},{3,4},{4,5},{5,6},
{6,7},{7,8},{8,9},{9,10}};
double num[10];
for(i=0; i<10; ++i)
num[0] = integrate(ranges[i][0],ranges[i][1]);

i = 0;
while ((i+1) < 10) {
if (num[i] > num[i+1]) {
num[i] ^= num[i+1]; // from here to
num[i+1] ^= num[i];
num[i] ^= num[i+1]; here, it shows an error //
if (i > 0) i = i -1;
} else {
i += 1;
}
}

for(i=0; i<10; ++i)
cout << num[i] << endl;
return 0;
}
double integrate(double lower, double upper)
{
double n = 0.0;
double dx = (upper-lower)/2000;
for (double i = lower; i < upper-dx; i += dx)
{
n += sin(i)*dx;

}
return n;
}

double f(double x)
{
return sin(x);
}```
I tried to sort this code from smallest to largest.

2. So one day you should learn how to swap two items. Hint: ^ is not involved.

3. huh? why did you do the XOR? and if you are thinking to do a swap between 2 variables, that isn't the way to do it. complementing tabstop's comment: hint: temporary variable.

4. Code:
```int a;
int b[4];
b[1] = 1; b[2] = 5; b[3] = 3

for(int i = 1; i < 4; i++)
{
for(int ii = 1; ii < 4; ii++)
{
if(b[ii] < b[i])
{
a = b[ii];
b[ii] = b[i];
b[i] = a;
}
}
}```
Swaps, B[1] contains the largest number after the swap is done, B[3] the smallest.

5. ManyTimes, your code is wrong.

6. But surely you can identify the "swap" portion of the code?

7. swap portion? I have never seen "ii"

8. But surely you can identify which portion of his code does the swapping? Bonus super hint: variable names do not actually matter.

9. Originally Posted by gulfx01
The only problem is this part:
Code:
```" num[i] ^= num[i+1];
num[i+1] ^= num[i];
num[i] ^= num[i+1]; "

#include <iostream>
#include <cmath>

using namespace std;

double integrate(double lower, double upper);
double f(double x);

int main()
{
int i, j;
int ranges[][2] = {{0,1},{1,2},{2,3},{3,4},{4,5},{5,6},
{6,7},{7,8},{8,9},{9,10}};
double num[10];
for(i=0; i<10; ++i)
num[0] = integrate(ranges[i][0],ranges[i][1]);

i = 0;
while ((i+1) < 10) {
if (num[i] > num[i+1]) {
num[i] ^= num[i+1]; // from here to
num[i+1] ^= num[i];
num[i] ^= num[i+1]; here, it shows an error //
if (i > 0) i = i -1;
} else {
i += 1;
}
}

for(i=0; i<10; ++i)
cout << num[i] << endl;
return 0;
}
double integrate(double lower, double upper)
{
double n = 0.0;
double dx = (upper-lower)/2000;
for (double i = lower; i < upper-dx; i += dx)
{
n += sin(i)*dx;

}
return n;
}

double f(double x)
{
return sin(x);
}```
I tried to sort this code from smallest to largest.
http://en.wikipedia.org/wiki/Swap_(computer_science)

10. Gulfx01. The idea of swap that everyone is throwing around is quite simple.

Say I have two variables. A and B. I want A to hold B's value and B hold A's value.

So if you want to put them in order...

In this case I put them in an array.

Code:
```int sort[2];

sort[0] = 5;
sort[1] = 3;

If (sort[1] < sort[0])
{
int temp; // Holds one of the values temporarily while the two are swapped

temp = sort[0]; // So now the temp variable stores the value of 5
sort[0] = sort[1]; // Now 3 is stored in the first position
sort[0] = temp; // Now 5 is stored in the second position
}```
obviously you can make this into a function

Code:
```void swap(int& a, int& b)
{
// The same idea as above.

int temp;
temp = a;
a = b;
b = temp;
}```

11. The x^=y^=x^=y trick is common among beginning programmers.
They dont realize it's non portable, can only be used on integers and is slower than the temporary variable swap. They just think its cool that you can use xor to swap two things