Math equations

This is a discussion on Math equations within the C Programming forums, part of the General Programming Boards category; Hi everyone, I am working on a program to basically multiply some equations. For example: (x + 1) * (x ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    Math equations

    Hi everyone,

    I am working on a program to basically multiply some equations.

    For example:

    (x + 1) * (x + 1)

    How would I code this so that my answer shows

    x^2 + 2x + 1

    I assume I would have to include math.h?? The equation that I have to do is more complex than this, but I think I can handle it if someone gives me the basics for doing this one. Any help would be appreciated.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    You want to do symbolic algebra? No, you cannot use math.h for that. You would have to write your own expression evaulation function. First think of how you would do it manually and then implement it in code.

    In this it could for example be:

    Write it out: x * x + x * 1 + 1 * x + 1 * 1
    Combine: x * x + 2 * x + 1

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    I tried just doing a test with this code
    Code:
    	y = x * x;
    	printf("%i", y);
    This didn't work. If I used this formula, I would want it to print x^2.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What didn't work exactly? Was the correct answer too big for the y variable?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    Currently nothing is assigned to the x value...I'll later want to substitute a number in for x, but not at this stage...When I ran the short code I had above...I got a funky number like y = 65750 or something like that.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Well, if you didn't assign anything to x, it will start life with a random (undefined) value, so no suprise you got a wierd answer!

    Just do:
    Code:
    int x = 10;
    long int y;
    y = x * x;
    printf("y = %ld\n", y);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    As said before, expression evaluation like you want is not implemented directly in C++. All variables have some value, and they are figured out at run-time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math equations not working
    By MaaaTtY in forum C++ Programming
    Replies: 3
    Last Post: 03-09-2009, 05:40 AM
  2. Rendering math equations
    By glo in forum Game Programming
    Replies: 7
    Last Post: 02-01-2009, 04:21 PM
  3. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM
  4. Replies: 7
    Last Post: 02-12-2003, 11:43 PM
  5. equations for a program
    By anthonye in forum C Programming
    Replies: 4
    Last Post: 06-19-2002, 05:38 AM

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