Function Exchange and Prime Finder

Hai i think all of u will be fine enough to answer my question

1. a function named Exchanged(int,int);
this function will exchange the values of variablesbut donot use any local as well as globel variable as temp

ok


2....
a function that calculates
the Next prime number every time it is called

receies prays to answer these
God bless u all

Comments

  • That's your job to write the programs. Post your code when you have problems

  • : 1. a function named Exchanged(int,int);
    : this function will exchange the values of variablesbut donot use any local as well as globel variable as temp
    :

    The function must use either pointers (int*) or references (int&) as arguments for this to work. Otherwise the function will only change the values of the local copies of the arguments passed into it and it will not behave the way you want it to. Other than that, a big hint would be to use the exclusive-or operator. There are only 3 lines of code necessary to do this.
    [code]
    void Exchange(int& x, int& y)
    {
    // Line 1: use exclusive-or somehow
    // Line 2: use exclusive-or somehow
    // Line 3: use exclusive-or somehow
    }
    [/code]
    Try experimenting with different things to fill out those 3 lines of code.
  • [code]
    void Exchange(int& x, int& y)
    {
    _asm
    {
    mov ecx,dword ptr [x]
    mov edx,dword ptr [y]
    mov eax,dword ptr [ecx] // x
    mov ebx,dword ptr [edx] // y
    xchg eax,ebx
    mov dword ptr [ecx],eax // x
    mov dword ptr [edx],ebx // y
    }
    }
    [/code]

  • : [code]
    : void Exchange(int& x, int& y)
    : {
    : _asm
    : {
    : mov ecx,dword ptr [x]
    : mov edx,dword ptr [y]
    : mov eax,dword ptr [ecx] // x
    : mov ebx,dword ptr [edx] // y
    : xchg eax,ebx
    : mov dword ptr [ecx],eax // x
    : mov dword ptr [edx],ebx // y
    : }
    : }
    : [/code]
    :
    :

    =)



  • [b][red]This message was edited by stober at 2004-1-2 5:15:46[/red][/b][hr]
    : : [code]
    : : void Exchange(int& x, int& y)
    : : {
    : : _asm
    : : {
    : : mov ecx,dword ptr [x]
    : : mov edx,dword ptr [y]
    : : mov eax,dword ptr [ecx] // x
    : : mov ebx,dword ptr [edx] // y
    : : xchg eax,ebx
    : : mov dword ptr [ecx],eax // x
    : : mov dword ptr [edx],ebx // y
    : : }
    : : }
    : : [/code]
    : :
    : :
    :
    : =)
    :
    [blue]Its the only method that works for every value of x and y. Any other method will destroy the value of one or both varables.[/blue]
    :
    :
    :



  • On a 32bit machine this works with integers less than 32768. With only a little extra code you could increase that to less than 65536. But for some reason I can't seem to move the values outside the function unless I used Global variables. Comments?

    Take Care,
    Ed Hall

    [code]
    i<<=16;
    i+=j;
    j=i;
    i<<=16;
    i>>=16;
    j>>=16;
    [/code]
  • [b][red]This message was edited by stober at 2004-1-2 10:34:12[/red][/b][hr]
    : On a 32bit machine this works with integers less than 32768. With only a little extra code you could increase that to less than 65536. But for some reason I can't seem to move the values outside the function unless I used Global variables. Comments?
    :
    : Take Care,
    : Ed Hall
    :
    : [code]
    : i<<=16;
    : i+=j;
    : j=i;
    : i<<=16;
    : i>>=16;
    : j>>=16;
    : [/code]
    :
    use the function prototype that HK posted earlier then just copy your code into it.



  • Here is what I was considering at the time when I made my post. This is probably something most experienced programmers should have come across in their travels through the web at some point in time.

    [code]
    void Exchange(int& x, int& y)
    {
    x ^= y;
    y ^= x;
    x ^= y;
    }
    [/code]
  • Ok, this is just too much for my current understanding. The following works in a scaled down (16bit) version with my Borland 4.52, but it won't compile with Dev-C++ which gives the following error on the declaration of the exchange function: "parse error before '&' token"

    [code]
    #include
    #include

    void exchange(int& i, int& j)
    {
    printf("i=%d j=%d
    ",i,j);
    i<<=16;
    i+=j;
    j=i;
    i<<=16;
    i>>=16;
    j>>=16;
    printf("i=%d j=%d
    ",i,j);
    }


    int main(int argc, char *argv[])
    {
    int i=31425;
    int j=731;
    printf("i=%d j=%d
    ",i,j);
    exchange(i, j);
    printf("i=%d j=%d
    ",i,j);

    system("PAUSE");
    return 0;
    }
    [/code]

    I've tried using a separate two integers for the routine (x, y) but it still won't compile. What am I missing? Something simple, I presume. . .

    Take Care,
    Ed Hall



  • : Ok, this is just too much for my current understanding. The following works in a scaled down (16bit) version with my Borland 4.52, but it won't compile with Dev-C++ which gives the following error on the declaration of the exchange function: "parse error before '&' token"
    :
    .3
    [blue]I just compiled it with Dev-C++ Version 4.9.8.3 and it compiled with 0 errors and 0 warnings, and ran just right.[/blue]
  • : Here is what I was considering at the time when I made my post. This is probably something most experienced programmers should have come across in their travels through the web at some point in time.
    :
    : [code]
    : void Exchange(int& x, int& y)
    : {
    : x ^= y;
    : y ^= x;
    : x ^= y;
    : }
    : [/code]
    :
    Thank you for the direct code. I was going to try to write this based on your other message but was too wrapped up in why I couldn't pass my other values back. This won't compile in Dev-C++ for me either (see my other message). I finally went back and tried my Borland 4.52 and it had no trouble. I suppose I should have moved back to Borland a bit sooner. I hope I didn't force you to reveal the above ahead of plans when you were allowing the original poster to work on it. As for me, I can see the how, but I'm going to study the why for bit longer. Thanks again.

    Take Care,
    Ed Hall

  • : : Ok, this is just too much for my current understanding. The following works in a scaled down (16bit) version with my Borland 4.52, but it won't compile with Dev-C++ which gives the following error on the declaration of the exchange function: "parse error before '&' token"
    : :
    : .3
    : [blue]I just compiled it with Dev-C++ Version 4.9.8.3 and it compiled with 0 errors and 0 warnings, and ran just right.[/blue]
    :

    Thank you for checking that for me. Apparently, it won't compile under C, but works OK under C++. I guess when I first started it I set it up as a C Console Application. I just tried it as a C++ Console application and it ran fine. Thank you again.

    Take Care,
    Ed Hall

  • : Thank you for checking that for me. Apparently, it won't compile under C, but works OK under C++. I guess when I first started it I set it up as a C Console Application. I just tried it as a C++ Console application and it ran fine. Thank you again.
    :
    : Take Care,
    : Ed Hall
    :
    :

    Your right -- it will not (and can not) be compiled as a *.c program because it contains c++ -specific syntax, such as the references in the Exchange function. But you could change those to pointers and it will probably compile as either c or c++.
  • : [code]
    : void Exchange(int& x, int& y)
    : {
    : _asm
    : {
    : mov ecx,dword ptr [x]
    : mov edx,dword ptr [y]
    : mov eax,dword ptr [ecx] // x
    : mov ebx,dword ptr [edx] // y
    : xchg eax,ebx
    : mov dword ptr [ecx],eax // x
    : mov dword ptr [edx],ebx // y
    : }
    : }
    : [/code]
    :

    no need for xchg, just write them back to the other address ;-)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories