Menu

Trying to make my 'powers' function work.

rattatouie
2007-08-16
2012-09-26
  • rattatouie

    rattatouie - 2007-08-16

    Thanks to all those who have helped me on this forum thus far; now that I'm registered and the program is coming along I thought it would make it easier for any would-be benefactors to start my problem anew. I'm trying to write a program which inputs one floating point and one integer from the user, then sends these to a function which calculates the floating point to the power of the integer. There are no longer any linking errors, but the program just does base * base no matter what the power is (apart for power 0 and power 1, which have been written in seperately and work fine). Please see my program below...

    include <stdio.h>

    float raise_float(float, int);

    int main (void)
    {
    float float_num;
    int int_num;

    printf(&quot;Please enter a floating point number, and hit the enter key: \n&quot;);
    scanf(&quot;%f&quot;, &amp;float_num);
    printf(&quot;Please enter an integer number, and hit the enter key: \n&quot;);
    scanf(&quot;%d&quot;, &amp;int_num);
    
    printf(&quot;\nThe answer is %f\n\n&quot;, raise_float(float_num, int_num));
    system(&quot;PAUSE&quot;);
    

    }

    float raise_float (float float_num, int int_num)
    {float ans;
    int count;

      if (int_num == 0)
         ans = 1;
      else if (int_num == 1)
         ans = float_num;
    
      else
      for (count=2; count&lt;=int_num; count++)
      (float_num*float_num, &amp;ans);
    
      return(ans);
    

    }

    As mentioned before am having a lot of trouble getting the function to work properly, I can sort of see why it's not but I'm not sure how to make it right. If any kind soul floating around here could lend a hand I'd be extremely grateful. Thanks again, chris

     
    • rattatouie

      rattatouie - 2007-08-17

      Hmmm, thanks for the effort guys, but I reckon I might be a lost cause. I'm thinking of just doing a switch statement for powers up to 20 or so (unless I have the stamina to do 999). And to answer your collective wtf?s, my unique grasp of c comes from hardly ever turning up to the lectures on the subject, and occasionally attempting to play catch-up using my text book. It's been ingrained into me how to do basic programs using printf, scanf, and assigning variables etc; but as soon as things get more tricky its painstaking trial-and-error. I basically can't see how to do the cumulative multiply AND stitch the answer back into the program (thanks for the example wayne, but trying to use something like it just ended up with giving nonsense answers for some reason, instead of the wrong numerical answers I was getting before). Thanks anyway all those helpful strangers. chris.

       
    • Soma

      Soma - 2007-08-16

      O_o

      "(float_num*float_num, &ans);"

      Exactly what do you imagine that does?

      Soma

       
      • Osito

        Osito - 2007-08-16

        I was wondering that myself. I figured it was just some weird syntax I'd never seen before. How did it even compile?

         
        • Soma

          Soma - 2007-08-17

          It only makes use of the comma operator, but it isn't the syntax that is problematic; the logic is the issue.

          Soma

           
          • Wayne Keen

            Wayne Keen - 2007-08-17

            Interesting mix of obfuscation, "cleverness" and newbie level logic errors.

            I have accidentally used the comma operator a few times, when hopping between
            Python and C array notation.

            Wayne

             
            • Wayne Keen

              Wayne Keen - 2007-08-17

              We went through a stretch a few months ago where people would post code
              that had a weird combination of "cute" logic with much lower level mistakes
              in it - at the time I wondered if there was a teacher out there who was
              giving assignments to "fix" code samples.

              Its just hard to imagine someone who writes code with these tricks would
              have problems mastering a loop with an cumulative multiply, i.e something like

              y = 1;
              for (i=1; i <= n; i++)
              {
              y *= x;
              }

              Wayne

               
              • Osito

                Osito - 2007-08-17

                LOL, what's up with counting from 1 to n like a human being? I almost forgot you could do that. I only know how to count from zero any more.

                 
                • Wayne Keen

                  Wayne Keen - 2007-08-17

                  I get burned about once a week at work dealing with 3D data where the
                  vertex numbering starts at 1....

                  Waynebozo

                   

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.