Code to solve for a linear equation.

I dont know how to approach the problem of finding the variable in the equation format of: x + y = z, when the user inputs the equation with 2 of the variables x and y, x and z, or y and z already filled in. (e.g. input is x + 3 = 5)
Last edited on
There is no magical solution. You need to have different equations for each input combination.

    x = z - y
    y = z - x
    z = x + y

So if the user inputs x and z, use the second equation to get and print y.

I’d love to add some code to play with it, but this is so simple I would basically be giving you a solution. You’ll get it.

Hope this helps.
Algebra. There's a reason they make you suffer through that. This is one of them.
on the off chance you have to do a much harder problem, say with 20 variables instead of 3, you can solve these kinds of things with a linear algebra library. You still have to do a fair bit of work to get an answer, but you don't have to solve it every which way by hand or handle what-ifs like one of the variables is a combination of a few of the others so you have multiple unknowns to find etc...

@jonnin
This is a simple, CS-101, basic logic and flow control program. An if else if else will do. And deciding how to get input. I recommend asking for x, y, and z individually, where the user can either provide a number or just press Enter. A little function to do that helps. If I were to do it with the full C++ powers, I would use std::optional:

1
2
3
4
5
6
7
std::optional <int> ask_int( const std::string& prompt )
{
  std::cout << prompt;
  std::string s;
  if (!getline( std::cin, s ) or s.empty()) return {};
  return std::stoi( s );
}

If the user inputs an invalid number it will properly crash the program (unless you wrap main() in a try...catch() to provide an appropriate error message).
I get it. I just don't like to indicate that the only way to do it is the hard way, in case he runs into it again.
Well this is for an assignment, and it just says that the input will be the equation. I figured out that i can use while(cin >> input) to read the equations until there is no more input. I used
for(unsigned i = 0; i < equation.length; i++){
if(equation[i] == '+'){
x = equation.substr(0, i);.... which read everything before the +, and stores it as a string. It did that same methid for y and z. I just dont know how to determine if that string is a digit or character so that i can use and if statement to determine the variable.
isdigit() and isalpha()
is a digit sufficient? what if its 1234, can that be and if so, can you manage?
stoi (string to int) and substring can help here.
The code below does the job, but it only reads in the first equation. How would I get it to loop around until there is no more input?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string r, l, j;
	int r1, l1, j1;

        getline(cin, r, '+');
        getline(cin, l, '=');
        getline(cin, j);

        if(r.find_first_of("-0123456789") != string::npos &&
           l.find_first_of("-0123456789") != string::npos){
            r1 = stoi(r);
            l1 = stoi(l);
            j1 = r1 + l1;
            cout << j1 << endl;
        }
        if(r.find_first_of("-0123456789") != string::npos &&
           j.find_first_of("-0123456789") != string::npos){
            r1 = stoi(r);
            j1 = stoi(j);
            l1 = j1 - r1;
            cout << l1 << endl;
        }
        if(l.find_first_of("-0123456789") != string::npos &&
           j.find_first_of("-0123456789") != string::npos){
            l1 = stoi(l);
            j1 = stoi(j);
            r1 = j1 - l1;
            cout << r1 << endl;
        }

	return 0;
}
Last edited on
Jdur17, could you please post the exact assignment?
Without knowing more information, the way I would get it to loop around is to, you guessed it, put a loop around it.

More error checking could be done to account for malformed input, but something like:
1
2
3
4
5
6
7
8
while (getline(cin, r, '+'))
{
    getline(cin, l, '=';
    // etc. ... the reset
    
    
}
return 0;
Input:

The input contains several test cases. The only line of each test case contains a string in the form R+L=J. If the value of a variable is known, such value appears in the string instead the variable itself. Otherwise, the letter that indicate the variable appears normally.

It is guaranteed that exactly two of the three values are known. There is no leading zeros in the given values.

The input is read from standard in (cin).

Output

For each test case, print a single line containing the value of the unknown variable.
-----‐------------------------------------------------------------
I managed to figure it out. I just needed to use:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

int main()
{    
    while(getline(cin, equation, '+'){
        r = equation;
        getline(cin, equation, '=');
        l = equation;
        getline(cin, equation);
        j = equation;
        //rest of my earlier code, minus the 3 getlines.
    }
    return 0;
}

Thanks for the input. Glad there's people out there willing to help out!
(Btw, just now saw Ganado's post, and that is pretty much what i did, but it took me a while. Wish i had seen this earlier..
Last edited on
Topic archived. No new replies allowed.