-1

Maybe I'm dumb but I've been searching for a while and I can't seem to find an answer to this question: I'm creating a simple program to do my math homework faster, but I'm struggling in using variables such as X and Y.

Let's say the following:

price = 50q + 80

earnings = price * q (q is the quantity of produced items)

the result would be earnings = 50q^2 + 80q , but how exactly can I store a value such as 50q^2 and then use it as a number?

I have tried storing the coefficients into float variables, that way the multiplication would become:

earningsA = priceB , earningsB = priceC (the letters represent the coefficients)

, but that creates a mess when working with more than 1 variable, just think about dividing the result by X.

  • 1
    A good [C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) would help. – Michael Chourdakis May 16 '22 at 20:24
  • For the task at hand, I'm not fully convinced that C++ is the best language for the job. Something interpreted makes more sense to me. But that's just me. Regardless of the language, this is a programming 101, day 1 type thing. I applaud the decision to learn programming, but what have you tried learning so far? – sweenish May 16 '22 at 20:26
  • 1
    Could you show us the actual code that you've tried? Your explanation is confusing. – jjramsey May 16 '22 at 20:26
  • Normally, you want to store coefficients for a polynomial in an array, vector or similar container instead of individual variables. _e.g._ `double coeffs[3] = { 0, 80, 50 };` would represent your `50q^2 + 80q` equation, where the array position corresponds to the power of `q`. – paddy May 16 '22 at 20:31
  • @sweenish I started coding about a year ago but then kinda stopped, I know the basic programming (loops, conditions), the last stuff I remember using was some 2d arrays for coding a battleship simple game and studying pointers, the problem here is using characters as numbers, it really could be the simplest thing (reading other comments) but I can't seem to find anything useful – dsdsds sdsds May 16 '22 at 20:32
  • @paddy yes, storing coefficients into arrays is what I tried, but that works when there is only 1 unknown variable, what if my function was y = 3qx^2 + 60q – dsdsds sdsds May 16 '22 at 20:35
  • Well, that's a multivariate polynomial, which is a completely different game in terms of finding a solution. But sure, if you want to define a datatype that itself is an algebraic expression, you can do that, and then make your co-efficients an "expression" type instead of a numeric type. Based on your description of your C++ skills, I'd say this is a difficult uphill challenge for you right now, especially without a clear description of the actual problem to solve. If you only need a generic tool to solve arbitrary math equations, then why re-invent the wheel? Maybe look at R, or even Prolog! – paddy May 16 '22 at 20:41
  • @paddy Thank you so much, I'll look into Prolog and R! I didn't really think it would've been this hard to translate this math problems into c++. – dsdsds sdsds May 16 '22 at 20:52
  • 1
    Why would you expect that? Mathematics and programming are very different. A mathematical expression is completely different from the algorithm required to find solutions. – paddy May 16 '22 at 20:55
  • @paddy yeah you are right, I just started this project while doing my math homework and took it as a "challenge", should've reflected more about it lol – dsdsds sdsds May 16 '22 at 21:01
  • It certainly is a challenge. If you want to do it for fun / interest then go for it. But if you're doing it to save time then consider the amount of hours you will spend on the problem versus the time cost of just solving these by hand. A simpler approach could be to set up different problem types in a spreadsheet and just enter your values there. – paddy May 16 '22 at 21:05
  • Writing an equation-solving program to help with your algebra homework is like building a calculator to help with your arithmetic homework: if you're not careful, you'll make something that will let you pass the course without learning the skill. *"Tools can be the subtlest of traps"* – Beta May 17 '22 at 02:11
  • so you want to input an expression as string and solve it? Then the keyword is [symbolic math](https://www.google.com/search?client=firefox-b-d&q=+c%2B%2B+symbolic+math) and it's not easy – phuclv May 17 '22 at 03:30
  • @Beta Wouldn't you need to actually know how to do something in order to create a working tool that does that automatically? Anyways I wouldn't use it to solve them for me, just to check if my solutions are correct – dsdsds sdsds May 17 '22 at 05:53

0 Answers0