# Help with algebra code

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-09-2007
Unlockitall
Help with algebra code
im making a code that will solve a simple algebra problem in the form of y=mx+b, and im not sure how to assign the value of the math done to x
Code:

```#include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) {     int x = 0;     int m = 0;     int b = 0;     int y = 0;     cout << "Y = _\n";     cin >> y;     cout << "M = _\n";     cin >> m;     cout << "B = _\n";     cin >> b;     ((y - b)/m) = x;              //This is the problem spot     cout << "X =\n"; cout << x;     system("PAUSE");     return EXIT_SUCCESS; }```
this is the code, how do i make the result of [(y-b)/m] assign to x?
• 04-09-2007
twomers
>> ((y - b)/m) = x;

Reverse it - x = ((y - b)/m);
• 04-09-2007
h3ro
Try with the x first:
x = ((y - b)/m);

Not sure why this fixes the problem, but it works. Maybe someone else could explain why?

Edit: I even pressed F5 before I replied:P
• 04-09-2007
Unlockitall
thanks peoples, it works great
• 04-09-2007
twomers
>> Not sure why this fixes the problem, but it works. Maybe someone else could explain why?

Think about it. In maths if you were saying something was equal to something else, the value of something else is given to something, right? I know it can work both ways, on paper, but in general that's how you'd think about it - the value on the right is assigned to the parameter on the left.

In C you don't have the luxury of the program being able to mind read and mystically know which way you want it to work, so it only works one way - the value on the right is assigned to the parameter on the left.
• 04-09-2007
Oysterman
Another thing: test what y=5, b=3, m=3 gives you...
• 04-09-2007
h3ro
Ah, that makes perfect sense. Thanks for clarefying it
• 04-09-2007
Unlockitall
thanks for all the help, i now have a finished product to sell to seventh graders at my school
Code:

```#include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) {     int z = 1;     float x = 0;     float m = 0;     float b = 0;     float y = 0;     while (z == 1){     cout << "Y = _\n";     cin >> y;     cout << "M = _\n";     cin >> m;     cout << "B = _\n";     cin >> b;     x =((y - b)/m);     cout << "X =\n"; cout << x;     system ("echo                        .");     system ("echo .");     system ("echo .");     system ("echo .");     system ("echo .");     system ("echo Press 1 to continue");     cin >> z;     system ("echo .");     system ("echo .");     system ("echo .");     system ("echo .");           }     return EXIT_SUCCESS; }```
feel free to use this (im not sure why you would want to, but its ok)
• 04-09-2007
h3ro
Whats the point of all the system("echo")?
Your while loop does not work properly
• 04-09-2007
Unlockitall
the echos create spaces between things, and my while loop works, at least i think it does
• 04-09-2007
Unlockitall
does anyone know if C++ can do square roots? im thinking about making a quadratic function program
• 04-09-2007
h3ro
your loop does not work on my PC...
And the echo thing is really confusing.

Here is an other way to do it:
Code:

```#include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) {     // Initialize values     int z = 0;     float x = 0;     float m = 0;     float b = 0;     float y = 0;         // Print some info     cout << "This program solve the equation y = mx + b\n" << endl;         // Main loop     while (z != 1)     {          cout << " Enter your equation" << endl;          cout << "Y = ";         cin >> y;         cout << "M = ";         cin >> m;         cout << "B = ";         cin >> b;         x =((y - b)/m);         cout << "\tX = " << x << endl;                 cout << "\n -----------------------------------------------" << endl;         cout << "Press any key execpt from 1 to solve a new equation" << endl;         cin >> z;         cout << "\n\n";     }       return EXIT_SUCCESS; }```
• 04-09-2007
brewbuck
Quote:

Originally Posted by twomers
Think about it. In maths if you were saying something was equal to something else, the value of something else is given to something, right?

Absolutely not. In standard mathematical notation, "a = b" means that the two quantities a and b are equal. It does not imply any movements of "data" from one variable to another. The word "variable" in imperative computer programming does not mean the same thing as "variable" in mathematics, and the "=" operator should not be read aloud as "equals" but rather "gets the value of" or something equivalent to that.

It was probably a bad choice to make the assignment operator "=" in C, as it has caused all kinds of confusion between "=" and "==" in addition to the confusion of the poster in this case. But this is how it is. The equals sign is NOT the equality operator, it is an assignment operator which copies data leftward.

Suppose we replaced the "=" with the following symbol: "<--". Now, the line of code reads:

Code:

`x <-- 1234;`
This makes it completely obvious which way the data moves.
• 04-09-2007
Unlockitall
still, does anyone know if C++ has a way to do exponents and square roots?
• 04-09-2007
h3ro
Yes it has.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last