Thread: Simple Program for the noobs

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

    Simple Program for the noobs

    Started C programming again, but I am stuck on a relatively very simple problem. The problem is stated in the comments.

    Code:
    /*  Programming Projects
    Problem 5
    Write a program that asks the user to enter the value for x and then displays the value of the following polynomial: 3x^5+2x^4-5x^3-x^2+7x-6
    */
    #include <stdio.h>
    int main (void)
    {
    	float x = 0, poly = 0;
    
    	printf("Enter a value for x: ");
    	scanf("%f", & x);
    
    	poly = 3 * (x*x*x*x*x) + 2 * (x*x*x*x) - 5 * (x*x*x) - (x*x) + (x*x*x*x*x*x*x) - 7;
    
    	printf("Value obtained: %.4f", poly);
    	return 0;
    }
    No matter what I enter for x the value of the polynomial is -7. I realize that would only happen if the value of x = 0, but doesn't the scan function replace the value of x with the one I wish to enter?

    I just realized I inputed an incorrect equation from what the problem was asking for, but it still should produce a result. Why not?
    Last edited by zerokernel; 02-25-2011 at 01:18 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Make your float initialize to 0.0, instead of just 0.

    The easiest way to debug this, is to simplify the arithmetic, and watch your errors. If you're using Turbo C, you have no linked with the floating point library, and should see that error, in the output.

    Are you receiving any warnings?

    I just ran your program in Turbo C, and for x=2, received 205.0000 as the answer (poly). No warnings or errors from the compiler or linker.

    Even the floating point library linked, as it was! < Yeah! >

    These numbers will rapidly increase. Try it with 2 for x, and see how it works. You may just be going out of range of an int, on your system.
    Last edited by Adak; 02-25-2011 at 01:33 AM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    Quote Originally Posted by Adak View Post
    Make your float initialize to 0.0, instead of just 0.

    The easiest way to debug this, is to simplify the arithmetic, and watch your errors. If you're using Turbo C, you have no linked with the floating point library, and should see that error, in the output.

    Are you receiving any warnings?
    I changed it to 0.0 and it worked, but the weird thing was when I changed it back to just 0 with no decimal point it worked as well. What just happened?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your floating point library didn't get linked in. You should have gotten an error or warning message about that, in your output.

    Sometimes, our compilers and linkers, are only human <sigh>.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    Quote Originally Posted by Adak View Post
    Your floating point library didn't get linked in. You should have gotten an error or warning message about that, in your output.
    I had no warnings I'm using gcc from linux. Should I be concerned, should I report this bug? Is it a bug?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by zerokernel View Post
    I had no warnings I'm using gcc from linux. Should I be concerned, should I report this bug? Is it a bug?
    Unless you can reproduce it, I would see it as an anomaly, and not report it. If you can reproduce it, then I'd report it, because it's a bug.

    note that the bug may be in your kernel, or distro, and not in GCC, at all, but still it should be reported - cause somebody needs to get out the bug spray, if it can be reproduced.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's wrong anyway: 7 * x which would be right, is not x^7. Polynomials are arranged by degree so don't make this mistake in the future. This and since -6 is the constant, not -7...

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    Quote Originally Posted by whiteflags View Post
    It's wrong anyway: 7 * x which would be right, is not x^7. Polynomials are arranged by degree so don't make this mistake in the future. This and since -6 is the constant, not -7...
    Thanks for catching my mistake, unfortunately I do that quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a Battleship Program
    By HBlakeH in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 11:13 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM