the power function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • logicode
    New Member
    • Apr 2007
    • 22

    the power function

    Hi, I usualy try to solve class problems on my own but I am hitting a wall.
    I am supposed to write a power function and call it in main but having problems like
    Code:
    void power(int base,int exponent)
    {
       for( i=0; i<exponent: i++ )
       {
       //This is my trouble area please help. Thanks
    
       }             
    }
    Last edited by sicarie; Apr 5 '07, 02:20 AM. Reason: Please use [code] and [/code] tags around your code.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Ok, sure. Please use code tags to frame code next time - it helps with readability.

    The trick there is to understand what the power function does. So, what does an exponent do, mathematically?

    Comment

    • faceboy
      New Member
      • Apr 2007
      • 4

      #3
      check this out

      code removed per FAQ

      Comment

      • logicode
        New Member
        • Apr 2007
        • 22

        #4
        ok My apologies. the power function raises the base by the exponent
        like x^y
        the x is raised to the y power or
        x^5 = x*x*x*x*x

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Originally posted by logicode
          ok My apologies. the power function raises te base by the exponent like x^y
          the x is raised to the y power or x^5 = x*x*x*x*x
          It's not a big deal, it just helps with readability.

          Right, so look at your for loop (specifically, 'exponent') variable, and think about how you could get that functionality.

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Also, how many times does it loop? What do you have to do each single time it loops to get the answer?

            Comment

            • logicode
              New Member
              • Apr 2007
              • 22

              #7
              Originally posted by sicarie
              It's not a big deal, it just helps with readability.

              Right, so look at your for loop (specifically, 'exponent') variable, and think about how you could get that functionality.
              i"m stuck..... Can you throw me a hint

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                Originally posted by logicode
                i"m stuck..... Can you throw me a hint
                I amended my last post with a reply - see if that helps.

                Comment

                • logicode
                  New Member
                  • Apr 2007
                  • 22

                  #9
                  Originally posted by sicarie
                  Also, how many times does it loop? What do you have to do each single time it loops to get the answer, and how many times specifically does it loop?
                  the loop ends when the counter becomes less then the exponent. I was thinking that base=base*base but I knew that wouldnt work so I need a third variable I think to store the original base in and then multiply it by the base untill the loop ends

                  Comment

                  • faceboy
                    New Member
                    • Apr 2007
                    • 4

                    #10
                    faceboy, do not post code again, I have warned you twice, and PM'd you about this.
                    Last edited by sicarie; Apr 5 '07, 02:43 AM. Reason: poster violating Posting Guidelines

                    Comment

                    • sicarie
                      Recognized Expert Specialist
                      • Nov 2006
                      • 4677

                      #11
                      Originally posted by logicode
                      the loop ends when the counter becomes less then the exponent. I was thinking that base=base+base but I knew that wouldnt work so I need a third variable I think to store the original base in and then multiply it by the base untill the loop ends
                      Well, look at your explanation of

                      x^5 = x*x*x*x*x

                      This needs to multiply x by x five times, correct?

                      In c/c++ you can do a nifty little shortcut of

                      variable += 1 as a shorthand of variable = variable +1

                      Now let me ask you - if you took x^2 * x^3, what do you get?

                      Comment

                      • logicode
                        New Member
                        • Apr 2007
                        • 22

                        #12
                        Originally posted by sicarie
                        Well, look at your explanation of

                        x^5 = x*x*x*x*x

                        This needs to multiply x by x five times, correct?

                        In c/c++ you can do a nifty little shortcut of

                        variable += 1 as a shorthand of variable = variable +1

                        Now let me ask you - if you took x^2 * x^3, what do you get?

                        x^5 and using that method "variable += 1"confuses the crap out of me I rather just use the straight out one.

                        Comment

                        • sicarie
                          Recognized Expert Specialist
                          • Nov 2006
                          • 4677

                          #13
                          Originally posted by logicode
                          x^5 and using that method "variable += 1"confuses the crap out of me I rather just use the straight out one.
                          That's fine - whatever helps you keep it straight.

                          Hmm, ok, let's break down the for loop

                          for (a counter; while the counter is less than 'exponent'; increment the counter)
                          do something here to get the resulting number

                          This is the same as
                          do something here to get the resulting number
                          do something here to get the resulting number
                          do something here....
                          .
                          .
                          .
                          'exponent' number of times.

                          Is that starting to look something like a possible specific example of x^5 = x*x*x*x*x ?

                          Comment

                          • logicode
                            New Member
                            • Apr 2007
                            • 22

                            #14
                            Originally posted by sicarie
                            That's fine - whatever helps you keep it straight.

                            Hmm, ok, let's break down the for loop

                            for (a counter; while the counter is less than 'exponent'; increment the counter)
                            do something here to get the resulting number

                            This is the same as
                            do something here to get the resulting number
                            do something here to get the resulting number
                            do something here....
                            .
                            .
                            .
                            'exponent' number of times.

                            Is that starting to look something like a possible specific example of x^5 = x*x*x*x*x ?

                            ok...i'm on the edge I know it =] just not quit there

                            Comment

                            • sicarie
                              Recognized Expert Specialist
                              • Nov 2006
                              • 4677

                              #15
                              Originally posted by logicode
                              ok...i'm on the edge I know it =] just not quit there
                              So you have a loop that will iterate exponent number of times, what action do you want to do one time for each iteration to get the final number?

                              Comment

                              Working...