Explanation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew V
    New Member
    • May 2012
    • 16

    Explanation

    Can someone explain me what the
    Code:
    y=-y;
    stands for in
    Code:
    #include<iostream.h>
    using namespace std;
    int main(){
    int x, y;
    float product=1 ;
    cout << "Enter a base number: ";
    cin >> x;
    cout << "Enter an integer exponent: ";
    cin >> y;
    int i = 1;
    if (y<0)
    { y=-y;//HERE I AM
    while (y>=i)
    {
    product *= x;
    i++;
    }
    cout<<x<<"to the power of "<<y<<"equals="<<(1/product);
    }
  • Mariostg
    Contributor
    • Sep 2010
    • 332

    #2
    What would be your guess?
    Do you think y=-y is like y=(-1)*y ?
    See if you can cout<<y after the assignment. What do you conclude?

    Comment

    • Andrew V
      New Member
      • May 2012
      • 16

      #3
      When i compiled it it gives the same result,and i think it's the same.

      Comment

      • Mariostg
        Contributor
        • Sep 2010
        • 332

        #4
        So you answered your own question. Now does your program do what you want it to do? Maybe not.

        Comment

        • Andrew V
          New Member
          • May 2012
          • 16

          #5
          uhm...why not? if that line is there the program it will not work for negative power.

          Comment

          • Mariostg
            Contributor
            • Sep 2010
            • 332

            #6
            Well I am not sure what you are trying to achieve.
            But consider this:
            Code:
            if (y<0) 
            { y=-y;//HERE I AM
            Don't you find this odd? Your code is saying that if y<0 then make y=-y.

            Comment

            • Andrew V
              New Member
              • May 2012
              • 16

              #7
              Originally posted by Mariostg
              Well I am not sure what you are trying to achieve.
              But consider this:
              Code:
              if (y<0) 
              { y=-y;//HERE I AM
              Don't you find this odd? Your code is saying that if y<0 then make y=-y.
              Yes but how can i do it without that?

              Comment

              • Mariostg
                Contributor
                • Sep 2010
                • 332

                #8
                Well, you need to explain what your problem is. It is the first time you mention an issue with "negative power". This has nothing to do with your first question. What do you want to do with the variable "y"?. You want to make it positive if smaller than 0?

                Comment

                • Andrew V
                  New Member
                  • May 2012
                  • 16

                  #9
                  well,i have to do a program that will do A to the power B.
                  B can be 1,0,positive or negative.I have done this program.And that line
                  Code:
                  y=-y
                  i have writed it because i had no ideas how i can do it to work with negative powers.And i i erease that line the program it will not work for negative power and if i keep it it works.I want to know what that line means and why i can't do it without it.

                  Comment

                  • divideby0
                    New Member
                    • May 2012
                    • 131

                    #10
                    say y is -2 it can be rewritten as

                    y = -y
                    y = - (-2)
                    y = +2

                    math property: two like signs become positive.

                    you only need one loop; it should work whether or not the power is pos, 0, or neg. so long as you initialize result to 1.

                    Code:
                    while(i < (power > 0 ? power : -power)) {
                       result *= base;
                       ++i;
                    }
                    the ? operator is handy because it behaves like an if/else. if power is greater than 0 use it as it is, and if it's less than 0, "make" it positive. when you ouput the result, you can use the same test.

                    Code:
                    std::cout << base << "^" << power << " = "
                              << (power > 0 ? result : 1/result);
                    or use an if/else when outputting the result depending on power's sign.

                    Comment

                    Working...