Thread: Value of a polynomial in a point

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    33

    Value of a polynomial in a point

    I don't know why this doesn't work. It doesn't return any errors, but it does the polynomial equation wrong. I tried using "^" instead of "pow" and it still does it wrong. I'm getting results like "-897123897" instead of "3". This is the code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    int main()
    {
        int i, x[10], e, S = 0;
        double y;
        printf("Plese insert degree of polynomial\n");
        scanf("%d", &e);
        printf("Please insert X\t");
        scanf("%d", &y);
        printf("Please insert coeficient of each degree\n");
        for (i = 0; i <= e; i++)
        {    
            scanf("%d", &x[i]);
        }
        S = 0;
        for (i = 0; i <= e; i++)
        {
            S = S + x[i]*(pow(y,e-i));
        }
        printf("The value of your polynomial is = %d\n", S);
        getch();
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I compiled your program and the compiler reported this warning:
    Code:
    test.c: In function ‘main’:
    test.c:10:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘double *’ [-Wformat=]
         scanf("%d", &y);
         ^
    In other words, this:
    Code:
    scanf("%d", &y);
    should have been:
    Code:
    scanf("%lf", &y);
    By the way, it is good that you posted your expected output and actual output, but next time remember to post your corresponding input too.

    EDIT:
    A few other things to note:
    • <conio.h> and getch are non-standard. You don't need them if you run your program from within a separate command prompt window, or configure your IDE to pause your program at the end.
    • You should check the return value of scanf. Since in each case you expect one assignment of values from the scanf call, you should check that scanf returns 1.
    • Use descriptive names. In the event that you have a mathematical formula that is well known by single letter names, write the formula out in a comment.
    • As you may have noticed, I did not change your use of pow. Indeed, it is likely to be correct here; ^ is the bitwise xor operator.

    Oh, and I am happy to say that your indentation is good
    Last edited by laserlight; 01-03-2015 at 01:09 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    33
    It worked! Thank you very much. I lost at least 3 hours on this

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by OctavianC
    I tried using "^" instead of "pow" and it still does it wrong.
    ^ is not an exponent operator, it's bitwise exclusive or:

    Code:
    1 ^ 1 = 0
    0 ^ 0 = 0
    1 ^ 0 = 1
    0 ^ 1 = 1
    You weren't going to get a sensible answer by mixing bits.

  5. #5
    Registered User
    Join Date
    Jan 2015
    Posts
    33
    I'm kind of proud of how much I've learned in two days .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-27-2011, 04:34 AM
  2. Polynomial addition
    By treenef in forum C++ Programming
    Replies: 1
    Last Post: 12-17-2005, 10:45 AM
  3. Polynomial
    By CompiConQuiso in forum C++ Programming
    Replies: 4
    Last Post: 05-16-2005, 12:13 AM
  4. Polynomial
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-17-2002, 05:00 PM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM

Tags for this Thread