3

I am fairly new to C programming and am running into a little issue.

I am inputting 0.05 as a double float and am running it through the formula given in my solution picture to acquire the answer 0.06242. However, I keep getting 0.000000 no matter what I type as my input. Can someone explain to me if there is something wrong with my code or maybe explain if I am using "%lf" correctly in both scanf and printf? Thanks.

Solution to my problem

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

main(){
    double guess, pow1,pow2,pow3, estimate;
    printf("Type in an initial guess of the root(for this case type 0.05):  ");
    scanf("%lf", &guess);

    printf("Calculating the root estimate using formula from solution...\n");
    pow1 = pow(guess, 3);
    pow2 = 0.165*pow(guess, 2);
    pow3 = 3*pow(guess, 2);

    estimate = guess - ((pow1 - pow2 + 0.0003993) / (pow3 - 0.33*guess));
    printf("Estimate = %lf\n", estimate);
}
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
DAGonz
  • 41
  • 4
  • 2
    Debug the code line by line and see where the value is going wrong – CinCout Sep 13 '19 at 06:31
  • [Can't reproduce.](https://onlinegdb.com/ByAawn_Lr) – Blaze Sep 13 '19 at 06:31
  • Try to split your complex expressions and calculations into smaller and simpler ones. For example `3*pow(guess, 2)` could be done as `double temp = pow(guess, 2); pow3 = 3 * temp;`. That way it will be much easier for you to see intermediate results when *debugging* to see where the error might be. – Some programmer dude Sep 13 '19 at 06:32
  • 3
    What is a "double integer"? – Lundin Sep 13 '19 at 06:32
  • Tip: Whenever you are unsure if your input method is correct, replace it with hardcoded values and see if you get the same results. For example, replace `scanf("%lf", &guess);` with `guess = 0.05;`. – user694733 Sep 13 '19 at 06:32
  • 1
    It's not double integer, it's double float – Barmar Sep 13 '19 at 06:33
  • @Lundin Basically getting my input integer to be a double value so that when I run my equation, the output can be a double. – DAGonz Sep 13 '19 at 06:33
  • 2
    @DAGonz It's not integer input, it's floating point input. Do you understand the difference between integer and floating point? – Barmar Sep 13 '19 at 06:35
  • I figured it out! Turns out I only had to edit my very last printf statement as: printf("%f") and not printf("%lf") – DAGonz Sep 13 '19 at 06:36
  • @DAGonz That's only because your compiler is so outdated. See my answer below. – Lundin Sep 13 '19 at 06:38
  • `scanf` expects `%lf` for double (pointer). But `printf` just use `%f` for double. – selbie Sep 13 '19 at 06:47
  • Related https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings – n. m. could be an AI Sep 13 '19 at 07:18

1 Answers1

7

The presence of main() instead of int main (void) means you are using an ancient version of C known as C90, instead of using standard C, since main() will only compile cleanly in ancient C90.

C90 did not support %lf for printf other than as a compiler extension. It only supported %f for all floating point types. This could explain why you get weird output. See Correct format specifier for double in printf.

Solve this by getting an updated compiler and an updated source of learning C.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • I am running a GNU GCC Compiler on the 'latest' download of codeblocks – DAGonz Sep 13 '19 at 06:40
  • @DAGonz The Mingw gcc port that Codeblocks uses isn't necessarily updated since forever - it could be older than gcc 5.0. It's still a somewhat modern compiler, but it defaults to C90. See [this](https://stackoverflow.com/a/34220462/584518) for a description how you can force it to run modern standard C. – Lundin Sep 13 '19 at 06:44
  • I compiled this code without problems with `gcc -std=c18`. It gave a warning, but compiled and ran fine. – klutt Sep 13 '19 at 06:50
  • @klutt Unlike the OP, so what's your point? `-std=c18` is likely not even available in the default Codeblocks installation. With some luck they updated beyond gcc 5.0 in recent years though. – Lundin Sep 13 '19 at 06:52
  • @klutt Or if this is another "it compiled fine just with warnings" debate, I'm not having that yet again. After you read C17 5.1.1.3 start using `-pedantic-errors`. – Lundin Sep 13 '19 at 06:55
  • @Lundin I'm not aiming for a debate. I was looking for a clarification. I got it, so thanks. But I think you should edit the answer, because there might be more than me that wonders over this. – klutt Sep 13 '19 at 08:31