Thread: How to do a math equation in c Programming

  1. #1
    Registered User
    Join Date
    Sep 2011

    How to do a math equation in c Programming


    This is my first post so if i break a rule i am really sorry.

    So this is the equation

    50 * s + 40(200-s) <= 9059

    I need to solve for s and i know how to do it on paper but in c programming using code::blocks i am completely lost.

    any help with be appreciated.

    Thank you.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    I don't think there is a standard method beyond applying the logic you can see by looking at the equation.

    In this case, s certainly includes all negative numbers, so use a loop like:

    int result = 0, s = 1;
    while (result <= 9059) {
         result = [your equation with s];
    Will give you the upper bounds of set s.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2009
    If you want to create a generic method where you can write a string like: 50 * s + 40(200-s) = 9059
    and get the computer to solve the equation it's not going to be trivial to do. You need to create a parser to break down the equation, and make the calculation according to precedence rules in math and equations.
    Have a look at recursive decent parsers in wiki: Recursive descent parser - Wikipedia, the free encyclopedia

    There might be other ways to solve it however.

  4. #4
    Registered User
    Join Date
    Sep 2006
    It's the Chicken and Steak Dinner Problem - come home to roost! < ROFL! > Man, I can NOT get away from this thing.

    First thing, you need to assign variable names - short but descriptive variable names, to the parts that are making up this equation.

    You don't have to worry about the < because you'll round down by integer division, and take care of that part, so consider it just as = for now.

    Put up a program (yes CODE!), with your variable names, for numGuests, chickens, steak, chickcost, steakcost, budget, etc. in it, and I'll check back with you in a few hours.

    It doesn't need to compile, but you need to show some work on this, not just post the problem on forums.

    This is a set problem, so you don't need a descent parser. Just decent variable names will be fine.

    He can't use loops on this program, only math. S is the number of steak dinners, so it will never be a negative number, and yes, there is a very standard method for solving it.
    Last edited by Adak; 09-07-2011 at 03:54 PM.

  5. #5
    Registered User
    Join Date
    Sep 2011
    I know how to program, i have taken other programming classes. The problem is the equation i figured it out and i am unsure how to put it into the code without using a loop. I was not looking for an exact answer but help on what i can do.

    Thank you,

    #include <stdio.h>
    int main()
        //This is the commmon space for my variables
        int chickencost = 0;
        int steakcost = 0;
        int guests = 0;
        int budget = 0;
        printf("What is the cost of the chicken, in dollars?\n");
        scanf("%d", &chickencost);
        printf("What is the cost of the steak, in dollars?\n");
        scanf("%d", &steakcost);
        printf("How many guests will there be?\n");
        scanf("%d", &guests);
        printf("What is your spending limit for food, in dollars?\n");
        scanf("%d", &budget);

  6. #6
    Registered User
    Join Date
    Sep 2006
    I believe it's called "using weighted variables", in arithmetic. I've seen the "simple" equation for it, but it's VERY unintuitive, to my mind. I greatly prefer the algebraic format of the problem:

    In this example, not the different budget value.

    Let x = number of steak dinners possible
    Express chicken dinners in terms of the streak dinners:
    50x + 40(200-x) = 9,000
    Distribute the 40 across the quantity (200-x)
    50x + 8000 -40x = 9,000
    Combine the x terms:
    10x + 8000 = 9,000
    Subtract the 8,000 from both sides of the equation:
    10x = 1,000
    And divide by 10:
    x = 100
    steak dinners   = 100 
    chicken dinners = 100
    100 * 50 = 5,000
    100 * 40 = 4,000
    200       $9,000
    Could we squeeze in just one more steak dinner?
    101 * 50 = 5050
     99 * 40 = 3960
    200       $9,010 (nope, over budget)
    I used code tags for the above because it won't line up otherwise.

    So your code would use logic like:

    math equals =, not assignment:

    stkcost * stkdinners + chkcost (guests - stkdinners) = budget

    chkcost * guests = minimum possible to spend (all chicken dinners) //getting there

    900 - (chkcost * guests) = amount left for steak dinner expense

    stkcost - chkcost = extra expense per dinner for steak

    Here's your code equation:

    stkdinners = (budget - chkcost(guests))/(stkcost-chkcost)

    maximum steak dinners, while still meeting budget restraint
    chkdinners = guests - stkdinners

    Give that a shot.
    Last edited by Adak; 09-08-2011 at 06:39 AM.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Thank you very much everyone for your input and help, this was very helpful and i have a better understanding of how to solve equations in the future.

    Once again thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help reducing an equation, math wiz's welcome. =)
    By M.Richard Tober in forum C++ Programming
    Replies: 6
    Last Post: 07-23-2011, 05:45 PM
  2. C++ math equation
    By XodoX in forum C++ Programming
    Replies: 22
    Last Post: 03-02-2009, 12:02 AM
  3. Converting String to Math Equation
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 07-10-2005, 06:59 PM
  4. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM
  5. A math equation
    By dizolve in forum A Brief History of
    Replies: 8
    Last Post: 12-17-2002, 12:56 PM

Tags for this Thread