C++ Interview code?

alkemyst

No Lifer
Feb 13, 2001
83,769
19
81
Pseudo code:

#define int swap(a,b) a=a-b;b=b-a;a=a-b;

int main()
{


int x = 5 ; int y =10;

swap(x,y);

printf("%d %d\n",x,y);

swap2(x,y);

printf("%d %d\n",x,y);

}

int swap2(int a, int b)
{
temp=a;
b=a;
a=temp;
return 0;

}

now it returns:

10 5
10 5

it appears swap is built into C++ as the define and swap2 can be changed to whatever I'd like (#define swap(a,b) with nothing else and make swap2 as 'return 0;' and nothing else) and get:
10 5
10 5

anyone want to explain this if it's not just a built in function?

Thanks

Å
 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
I'm not sure what you're asking for.

Also, have you tried out your pseudocode? The swap macro doesn't seem to work out with swap(5, 10).
a = a - b = 5 - 10 = -5
b = b - a = 10 - (-5) = 15
a = a - b = -5 - 15 = -20

so, a = -20, b = 15? I don't think that's what you wanted.
 

alkemyst

No Lifer
Feb 13, 2001
83,769
19
81
Originally posted by: AgentEL
I'm not sure what you're asking for.

Also, have you tried out your pseudocode? The swap macro doesn't seem to work out with swap(5, 10).
a = a - b = 5 - 10 = -5
b = b - a = 10 - (-5) = 15
a = a - b = -5 - 15 = -20

so, a = -20, b = 15? I don't think that's what you wanted.

my answer was based on a solution like that....

however this code compiled returns the same result of 10 5 twice....it's odd, I don't know why, I was never taught there was a builtin function to 'swap'....however it appears to work.

#include <iostream>
#include <stdlib.h>

using namespace std;


int main()
{


int x ; int y ;
x=5;
y=10;

swap(x,y);

printf("%d %d\n",x,y);


printf("%d %d\n",x,y);

return 0;
}


Seems it was a trick question....

&Aring;
 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
Yes, I'm confused.

I think I'm confused because it seems that swap (the swap macro and swap2) do not seem to swap anything. Your swap2 seems to set b to the value of a.
 

alkemyst

No Lifer
Feb 13, 2001
83,769
19
81
it is confusing....the way it looks to work is (I made a slight mistake in my memory/braindump):

the define: #define swap(a,b) a=a-b;b=b-a;a=b-a;

a=5-10
a=-5

b=10--5
b=15

a=15--5
a=20

so you should have x=20 and y = 15 for the 'swap'

then you have: swap2(int a, int b) { temp=a;b=a;a=temp;return 0;}

temp = 5
b=5
a=5

so you should have 5 5 as output....

however....output is a true 'swap': 10 5

I took out all code but just int x=5;int y=10; swap(x,y); printf and it works :(

I don't know if it is documented, however it appears swap(x,y) switches x to y and y to x....I took C and C++ and we were never taught this was a built-in function....our solution was to:

temp=b;
b=a;
a=temp;

I am confused myself.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
a=a-b;b=b-a;a=a-b;

should be

a=a-b;b=b+a;a=b-a;

eg a = 7, b = 15

a=7-15=-8
b=15+(-8) = 7
a=7-(-8) = 15
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
That swap macro is a little dangerous since it could cause overflow / underflow. If a,b are very large or are something like bit fields then garbagealarity may ensue.
 

alkemyst

No Lifer
Feb 13, 2001
83,769
19
81
Originally posted by: dighn
a=a-b;b=b-a;a=a-b;

should be

a=a-b;b=b+a;a=b-a;

The wierd part of it all is I can't remember the initial sequence....however, I changed them each time and the output was always a simple x and y swap....

I wish I knew exactly what the sheet I was tested on said now.

 

dighn

Lifer
Aug 12, 2001
22,820
4
81
wait a second, macros dont have return types. mnaybe that macro has no effect b.c it's trying to define int?

btw there is a swap function in STL
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
#define int swap(a,b) a=a-b;b=b-a;a=a-b; doesn't compile here on vc++ .net 2003
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: dighn
#define int swap(a,b) a=a-b;b=b-a;a=a-b; doesn't compile here on vc++ .net 2003


No way that should work.
#define should take the first parameter and try to assign the rest as a substitution
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
There's not any c++ in that code; you're even using C-style i/o.

And preprocessor macros are not functions. The preprocessor is a text processor, and simply replaces macros with their defined replacement text.

Interview? Is this for a job interview?
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
even if you fix the macro by getting rid of "int", it still shouldn't give yo the correct result because the preproc is run before compilation replacing swap so the stl call is never seen by the compiler. i dont understand how you are getting the corect result.
 

alkemyst

No Lifer
Feb 13, 2001
83,769
19
81
This is what I have:

#include <iostream>
#include <stdlib.h>

using namespace std;

#define swap(a,b) a=a+b;b=b-a;a=a-b;

int swap2(int a, int b)
{
int temp = a;
b=a;
a=temp;
return 0;
}

int main()
{

int x ; int y ;
x=5;
y=10;

swap(x,y);

printf("%d %d\n",x,y);

swap2(x,y);

printf("%d %d\n",x,y);


return 0;
}


now this is what it took to get it to compile...in the 'test' the swap2 function was after the main program....

I thought the answer was (going off memory).
20 -5
-5 -5

However the correct answer is supposedly:

10 5
10 5....

This works for:

#include <iostream>
#include <stdlib.h>

using namespace std;


int main()
{


int x ; int y ;
x=5;
y=10;

swap(x,y);

printf("%d %d\n",x,y);


printf("%d %d\n",x,y);

return 0;
}



 

alkemyst

No Lifer
Feb 13, 2001
83,769
19
81
Originally posted by: Ulukai
It's a function in the STL

I discovered that, however we were never ever taught it and many of my books here don't even mention it. I hear it's a good thing to use only some of the time.

Sort of sucks that this is what they used to 'determine' a programmer's ability. I am hoping I still get the job, she really liked my answers during the interview and told me she didn't think my missing the question was so bad especially once I was more interested in figuring out 'why' it worked that way. The coder that wrote this test was called in and told me it was due to 'global variables'
rolleye.gif
....

Thanks