C++ problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • srikun10
    New Member
    • Jul 2008
    • 1

    C++ problem

    please solve this for me

    write a program in c++

    P=w+x/y-z

    give me the commands please......... ...
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by srikun10
    P=w+x/y-z
    Suppose P, w, x, y and z are all variables of type double then what you're looking
    for is this:

    [code=c]
    P=w+x/y-z;
    [/code]

    kind regards,

    Jos

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      I bet there must be parentheses.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by newb16
        I bet there must be parentheses.
        Why? If Occam's razor doesn't see any parentheses why should there be any
        parentheses? e.g. there is no sine() or cosine() function in that expression either,
        so why anticipate for them?

        kind regards,

        Jos

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #5
          Originally posted by JosAH
          Why? If Occam's razor doesn't see any parentheses why should there be any
          parentheses? e.g. there is no sine() or cosine() function in that expression either,
          so why anticipate for them?
          Because "plz gimme teh c0des for a plus b divided by c minus d" is not likely to be a-d+(b/c) . Just like 1 divided by 1 plus x != (1/1) + x . And it is not the absolute truth, but supposition.

          Comment

          • phiefer3
            New Member
            • Jun 2007
            • 67

            #6
            Originally posted by newb16
            Because "plz gimme teh c0des for a plus b divided by c minus d" is not likely to be a-d+(b/c) . Just like 1 divided by 1 plus x != (1/1) + x . And it is not the absolute truth, but supposition.
            Parentheses are only needed if you need to override the normal order of operations. Multiplication and division have higher priority than addition and subtraction, so

            P = w + x/y - z

            is the same as

            P = w + (x/y) - z

            which is the same as

            P = (w + (x/y)) - z

            Basically, without any parenthesis, the program will first divide x by y, then it will add that to w and lastly it will subtract z.

            If you need it to do things differently then you'll need parenthesis. But you didn't really provide enough information to know what order you need the things to be carried out in.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by phiefer3
              But you didn't really provide enough information to know what order you need the things to be carried out in.
              Actually newb16 didn't provide any information because they are not the original poster they are commenting on the solution proposed by Jos saying they think it is likely that some parentheses may be required. I suspect newb16 is fully aware of the significance of parentheses or lack their of.

              Personally I am with Jos on this, why try to second guess what someone who can't be bothered to do there own work really means. I will be quite surprised if we get any follow up from the OP.

              Comment

              • Niheel
                Recognized Expert Moderator Top Contributor
                • Jul 2005
                • 2432

                #8
                This question has been closed and will be deleted in 24 hours.
                niheel @ bytes

                Comment

                Working...