macro problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stdvu
    New Member
    • Oct 2008
    • 21

    macro problem

    how do i write a macro that performs swapping among 3 variables taken as parameters?
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Please describe what you want this macro to do. What gets swapped with what? Here's a starting point:
    #define MACRO(P1,P2,P3) ...
    You can describe the desired behavior in terms of P1, P2, and P3.

    Comment

    • stdvu
      New Member
      • Oct 2008
      • 21

      #3
      the problem is that the question states exactly the same way as i posted.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        You'll have to ask the teacher for clarification.
        The intent would be obvious if you were asked to swap two variables. "Swapping among three variables" could mean any number of things.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Yes unfortunately there are (at least) 6 different ways to do swapping among 3 variables A, B and C all producing different results so if you don't indicate which one (result) you are trying implement it is not going to be possible for us to help you.

          The experts and members of this community give their help and time free of charge. You have no right to that help and being rude and displaying a bad attitude is going to make it more likely that you do not receive help.

          The question would not have been asked if it was not necessary to know its answer in order to help you.

          Banfa
          Administrator

          Comment

          • stdvu
            New Member
            • Oct 2008
            • 21

            #6
            I was not trying to be rude :( I was stating the problem...... i will check with the teacher straight away ..... i myself am confused ..... :(

            Comment

            • stdvu
              New Member
              • Oct 2008
              • 21

              #7
              Ok my teacher replied that perform swap among 2 variables

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                What really? The macro takes 3 variables but must swap between 2 of them? Which 2 of the 3?

                Comment

                • stdvu
                  New Member
                  • Oct 2008
                  • 21

                  #9
                  I mailed my teacher to ask him what he meant and he answere me briefly saying :

                  dear student,

                  you can do swap operation on two values.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    No need to bother: you can't swap two variables by using a cpp macro; essentially
                    the macro processor uses a by name parameter passing mechanism which is
                    well known for the fact that you can't swap two variables using that mechanism.
                    Have a look:

                    Code:
                    #define swap(t, a, b) { t x= a; a= b; b= x; }
                    ...
                    int a[3] = { 2, 0, 1 };
                    int i= 1;
                    ...
                    swap(int, i, a[i]);
                    kind regards,

                    Jos

                    Comment

                    • Banfa
                      Recognized Expert Expert
                      • Feb 2006
                      • 9067

                      #11
                      Originally posted by JosAH
                      No need to bother: you can't swap two variables by using a cpp macro
                      Personally I think you are being just a little pedantic (if correct).

                      stdvu ignoring Jos counter example of how things can go horribly wrong I suggest you may be look at a writing a macro to swap 2 integer (int) variables which is probably what your teacher is asking for.

                      It isn't unusual for teachers assign simplistic assignments without considering the full implications of what they are asking.

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by Banfa
                        Personally I think you are being just a little pedantic (if correct).
                        Simply see to what that macro expands; it doesn't work; nothing pedantic about it.

                        kind regards,

                        Jos

                        Comment

                        • stdvu
                          New Member
                          • Oct 2008
                          • 21

                          #13
                          ok so you mean i simply need to swap 2 variables (int) without going into complexity ....... that is just write a simple program that is defined as macro right??? I will post the code, do temme whether I am correct or not..

                          Comment

                          • Banfa
                            Recognized Expert Expert
                            • Feb 2006
                            • 9067

                            #14
                            Originally posted by JosAH
                            Simply see to what that macro expands; it doesn't work; nothing pedantic about it.
                            No, no I understand that what you have written doesn't work and demonstrates that that type of macro doesn't work. But that is because of your use of an array and I am sure that is not what stdvu's teacher had in mind, hence "pedantic (if correct)".

                            It is possible to create a macro that, for instance, swaps the values of 2 int variables (as opposed to an int and an array variable indexed by that int). It is not possible to create a macro that works in all cases.

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by Banfa
                              No, no I understand that what you have written doesn't work and demonstrates that that type of macro doesn't work. But that is because of your use of an array and I am sure that is not what stdvu's teacher had in mind, hence "pedantic (if correct)".

                              It is possible to create a macro that, for instance, swaps the values of 2 int variables (as opposed to an int and an array variable indexed by that int). It is not possible to create a macro that works in all cases.
                              Ok, case closed; the mathematical lazy approach isn't appreciated: one
                              counter example and you can go back to sleep again: the example showed
                              that it can't be done so why bother ;-)

                              kind regards,

                              Jos

                              Comment

                              Working...