Thread: Help with algebra code

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    13

    Question 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?

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> ((y - b)/m) = x;

    Reverse it - x = ((y - b)/m);

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    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

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    thanks peoples, it works great

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> 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.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    41
    Another thing: test what y=5, b=3, m=3 gives you...

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Ah, that makes perfect sense. Thanks for clarefying it

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    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)

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Whats the point of all the system("echo")?
    Your while loop does not work properly

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    the echos create spaces between things, and my while loop works, at least i think it does

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    does anyone know if C++ can do square roots? im thinking about making a quadratic function program

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    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;
    }

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by twomers View Post
    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.

  14. #14
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    still, does anyone know if C++ has a way to do exponents and square roots?

  15. #15
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Yes it has.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM