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

  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
    4,183
    double answer = 3 * (x * x) - 5 * (x * x) + 6;

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

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  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
    4,183
    %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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

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, 01:39 PM
  3. Replies: 46
    Last Post: 08-24-2007, 04: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