# How to do a math equation in c Programming

• 09-07-2011
jabber5050
How to do a math equation in c Programming
Hello,

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.
• 09-07-2011
MK27
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:

Code:

```int result = 0, s = 1; while (result <= 9059) {     result = [your equation with s];     s++; } s--;```
Will give you the upper bounds of set s.
• 09-07-2011
Subsonics
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.
• 09-07-2011
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.
• 09-07-2011
jabber5050
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,

Code:

```#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); }```
• 09-08-2011
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.

Code:

```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 :p

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

stkcost - chkcost = extra expense per dinner for steak