Like Tree1Likes
  • 1 Post By stahta01

How to solve a polynomial given "x" in C.

This is a discussion on How to solve a polynomial given "x" in C. within the C Programming forums, part of the General Programming Boards category; I am 17 and trying to learn C, so I bought "Programming in C" by Stephan Kochan. I have made ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    8

    Exclamation How to solve a polynomial given "x" in C.

    I am 17 and trying to learn C, so I bought "Programming in C" by Stephan Kochan. I have made it to the 4th chapter on the 6th exercise and I cannot get this simple question:

    Write a program to evaluate the polynomial shown here:
    3x^3 - 5x^2 + 6
    for x = 2.55
    I did this:
    Code:
    //Polynomial Solver
    
    #include <stdio.h>
    
    int main(void)
    {
        double x = 2.55;
        double answer = 3 * (x * x) - 5 * (x * x) + 6;
    
        printf ("3x^3 - 5x^2 + 6 = %d\n", answer);
    
        return 0;
    }
    but it will not give me the correct answer.


    I know this is really basic, but please help!

    Thanks

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,745
    double answer = 3 * (x * x) - 5 * (x * x) + 6;

    How many x are there bold above?
    How many should there be?

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    Quote Originally Posted by stahta01 View Post
    double answer = 3 * (x * x) - 5 * (x * x) + 6;

    How many x are there bold above?
    How many should there be?

    Tim S.
    I see what you mean, but when I run it, it does run, I get this error in codeblocks:
    Process terminated with status -1073741510 (0 minutes, 5 seconds)

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,745
    %d means integer
    %f means float/double in printf

    Turn your warnings on when your compile!

    Code:
    H:\SourceCode\Projects\TestProjects\testc\main.c|8|warning: format '%d' expects argument of type 'int', but argument 2 has type 'double' [-Wformat]|
    Tim S.
    Salem likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [HELP] How to solve this error "Lvalue required"?
    By nh0ck0n10b in forum C Programming
    Replies: 8
    Last Post: 01-29-2011, 10:49 AM
  2. One Easy" C " Question. Please Solve and Explain.
    By RahulDhanpat in forum C Programming
    Replies: 18
    Last Post: 03-24-2008, 02:39 PM
  3. Replies: 46
    Last Post: 08-24-2007, 05:52 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21