Thread: Program doesn't properly compute simple polynomial.

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    20

    Program doesn't properly compute simple polynomial.

    The program will ask for the user to enter a value for x, then compute the following polynomial: 3x^5 + 2x^4 - 5x^3 - x^2 + 7x - 6.

    However, when I double check it with my calculator I get a wrong answer for random values of x. To simplify my problem I'm using only integers.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int x, polynomial;
    	
    	printf("The program will compute the following polynomial: \n");
    	printf("3x^5 + 2x^4 - 5x^3 - x^2 + 7x - 6 \n");
    	printf("Please enter a value for x: ");
    	scanf("%d", &x);
    	
    	polynomial = (3 * x * x * x * x * x) + (2 * x * x * x * x) - (5 * x * x * x) - (2 * x * x) + (7 * x) - 6;
    	printf("Value = %d \n", polynomial);
    	
    	return 0;
    }
    Last edited by zerokernel; 02-08-2014 at 12:00 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I noticed that you wrote (2 * x * x) instead of (x * x) for x^2.
    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
    Nov 2010
    Posts
    20
    Haha, I just figured it out note x^2 term I accidentally put a -2 in front.

    Actual code looks like this now:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int x, polynomial;
    	
    	printf("The program will compute the following polynomial: \n");
    	printf("3x^5 + 2x^4 - 5x^3 - x^2 + 7x - 6 \n");
    	printf("Please enter a value for x: ");
    	scanf("%d", &x);
    	
    	polynomial = (3 * x * x * x * x * x) + (2 * x * x * x * x) - (5 * x * x * x) - (x * x) + (7 * x) - 6;
    	printf("Value = %d \n", polynomial);
    	
    	return 0;
    }
    Even though I looked over it 5 times I still couldn't see it. I really hate my dyslexia.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    Thanks, you guys are quite fast.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, there are power functions available in <math.h>, but they are for floating point. You could use them and then cast the return values, but you may need to be wary of floating point inaccuracy. Another way is to write your own intpow function, which can initially just loop and compute the power (and later you can make it more efficient if necessary). Anyway, the point is that you can then write:
    Code:
    polynomial = (3 * intpow(x, 5)) + (2 * intpow(x, 4)) - (5 * intpow(x, 3)) - intpow(x, 2) + (7 * x) - 6;
    which would likely be easier to match to your math formula. (Then again, for the x^2 term I would still prefer to write (x * x), and since that was the place where you made the typo, maybe this would not have helped.)
    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

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I'd rarely use power functions to work with a polynomial expression. I'd draw out common factors. In this case (if I've done it right)
    Code:
    polynomial = x*(x*(x*(x*(3 * x + 2) - 5) - 1) + 7) - 6;
    At most, in this case, 5 multiplications ans 6 addition/subtraction operations (and without any reliance on knowing the polynomial's roots). It's hard to see how any usage of a power function can beat that. Okay, it is necessary to think about the expression in a different way, but it is still pretty simple and unambiguous. The error from the OP would have been harder to make too.

    I'd only consider using an actual power function if regularly dealing with polynomials with lots of zero coefficients (say 3*x^23 + 5*x^6 + x*x + 2).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-02-2013, 05:33 AM
  2. Doesn't compute
    By Jjoraisnt32 in forum C Programming
    Replies: 6
    Last Post: 02-27-2011, 06:31 PM
  3. Replies: 1
    Last Post: 12-07-2010, 06:53 AM
  4. Replies: 6
    Last Post: 05-18-2006, 05:35 PM
  5. Loop doesn't seem to function properly
    By TeQno in forum C++ Programming
    Replies: 1
    Last Post: 01-31-2005, 05:25 PM