-2

So my question relates to double, I am trying to get an input from the user in decimal point for any value and its exponent also in decimal point to display the result after calculation in another function where the variables will pass values as double and I have used double the output as well but the end result is 1.00000 even though I have used the output specifier as %lf%.

#include <stdio.h>

double pwra (double, double);

int main()
{
    double number, power, xx;

    printf("Enter Number: ");
    scanf("%lf", &number);

    printf("Enter Number: ");
    scanf("%lf", &power);

    xx=pwra (number,power);

    printf("Result: %lf", xx);

    return 0;
} 

double pwra (double num, double pwr)
{
    int count;
    int result = 1;

    for(count=1;count<=pwr;count++)
    {
        result = result*num;
    }
    return result;
}
Paul R
  • 208,748
  • 37
  • 389
  • 560
tryllz
  • 33
  • 1
  • 7
  • 1
    Welcome to StackOverflow, @TryllZ. You'll find that you'll get better help faster if you format you code so that it's easy to read. Also: you never really asked a question here, you just made a statement. Try to be as specific as possible about what it is you are trying to figure out. – Richard Apr 20 '16 at 07:03
  • OK, Sorry, I was under the impression that the same people will reply as to my earlier question, anyway thank you, will edit it now.. – tryllz Apr 20 '16 at 07:07
  • 1
    Those people might, but people (like me!) might also find this question on the front page of the site, or via other means. Each question should be as self-contained as possible to ensure that it is (a) accessible to the widest possible field of potential answerers and (b) as useful as possible to other people who may have the same question later. – Richard Apr 20 '16 at 07:09
  • @Paul, I apologize as I am not aware anybody can edit my code, second that I am new to programming and new to this stuff, please dont bash me :) – tryllz Apr 20 '16 at 07:18
  • I already answered your question a few minutes ago (see below). For future reference please take care with code formatting (no one wants to debug an unreadable mess of code) and also you might want to get a [a good book on C](http://stackoverflow.com/q/562303/253056) and also [learn how to use your debugger](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Apr 20 '16 at 07:20

1 Answers1

1

You have used the wrong type for result in the pwrs() function.

Change:

int result = 1;

to:

double result = 1.0;

Note that this type of simple mistake is easily identified if you learn to use your debugger. Further reading: How to debug small programs.

Also note that pwr should be an int, not a double, since your function only works with integer exponents.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • I tried this and this time the result is in double and decimal value but the result is different from the calculator result..?!, confused..the debugger shows no errors.. – tryllz Apr 20 '16 at 07:22
  • @TryllZ: the debugger doesn't "show errors" (apart from crashes) - you need to learn how to use it to step through your code, inspect variables, and thereby understand why your code doesn't work as you expect - this is essential if you want to learn to be a programmer. – Paul R Apr 20 '16 at 07:30
  • Sir I know the debugging that on mouse over shows the value stored in the variable at that point in time but Qunicy doest have mouse over thing for debugging and Dev C++ craches at times on debugging.. – tryllz Apr 20 '16 at 07:34
  • Well unless you have a working debugger you're going to find writing code quite difficult - the alternative to using a debugger is adding strategic `printf` statements to inspect variables etc, but this is much slower and more tedious than using a decent debugger. – Paul R Apr 20 '16 at 07:36
  • 1
    @TryllZ "... the result is in double and decimal value but the result is different from the calculator result" lacks useful information. Stating the 1) inputs, 2) output and 3) expected output is far more useful. – chux - Reinstate Monica Apr 20 '16 at 15:30
  • I am making a copy of the pow() function from the math.h library, so if the value and exponential valus used is (3.3, 3.3) the result is 51.4157, in my function with similar values, the answer is 35.xxxxx ?!..that is what I'm trying to achieve.. – tryllz Apr 20 '16 at 16:55
  • Well it won't work as it stands, since you are using a crude multiplication method which will only give correct results for integer powers - you'll need to use a better algorithm if you want arbitrary powers like with `pow()`. – Paul R Apr 20 '16 at 17:46
  • My understanding is that the base value multiplied by itself to the times of the power value will give the exponential result as far as mathematics is concerned, so 2^3 means 2*2*2 which is why I have passed my values to the loop, isnt that the right method. – tryllz Apr 21 '16 at 05:46
  • This only works for **integer** powers though, so yes, `pow(2, 3) = 2*2*2`, but how would you expect that to work for `pow(2, 2.5)` ? That would be `2*2*` ? If you want non-integer powers then you need a more sophisticated algorithm. – Paul R Apr 21 '16 at 08:08