Swap Function: Exchanging data in two memory locations.

Hey everybody, I need an expert's opinion on what to do here. I've been spinning my wheels for awhile on this problem and need some advice on where to go next.

//here is main (could be the issue, i don't know)
int main()
{
int nums [ 25 ];
int size = 25;

fillArray ( nums, size );
swap ( &nums, &size );
findMax ( nums, size );
selectionSort ( nums, size );
printArray ( nums, size );
}

//here is the function
void swap ( int &nums, int &size )
{
int *x, *y;
int temp;

temp = *x;
*x = *y;
*y = temp;
}

//Thank you!
The problem might be that you never actually told the x and y to effect nums and size so your swap function isn't actually doing anything.
Thanks shrine spirit. Should I tell the x and y to effect nums and size in main? or in the function?
You only need one temporary variable in swap. It doesn't need to be a pointer. You should also put your code in [code][/code] tags :)

1
2
3
4
5
6
void swap(int &a, int &b)
{
    int tmp = a; // hold onto the value of a for later
    a = b;          // replace the contents of a with b
    b = tmp;      // put the original contents of a into b
}


It works :D
http://ideone.com/cHXOlf

Hope that helps
Last edited on
The first problem is that you have not declared a swap function with the following signature:

void swap(int **, int *);

The second being that in the swap function you currently have, you have not affected the arguements num and size in any way, you seem to be more concerned with swapping the pointers in your code that do nothing rather than swapping the varaibles you got.
Last edited on
Topic archived. No new replies allowed.