user3675773
user3675773

Reputation:

generating linear equation with one unknown (c++)

Im sitting here for two hours and my mind is blowing. Im trying to code a generator for linear equation with one unknown. It has 3 combinations: ax+b=c (ex. 2x-3=1) a+bx=c (ex. 3-2x=1) a+b=cx (ex. 4-6=2x)

So i did some simple math, made an algorithm (x=(c-b)/a), excluded zero from denominator etc. The whole point is that to make the result as integer. That's why there is this code:

while (((c - b) % (a) != 0))
    {
        do
        {
            a = rand() % 24 - 12;
        } while (a == 0);
        b = rand() % 24 - 12;
        c = rand() % 24 - 12;

    }

First two combinations are working perfectly, but third one isn't. The example of an output from the third one is: 5-7=6x AND the calculated solution is x = 2. It would be x=2 if there was a + between 5 and 7. And it always goes this way. This one digit is wrong. The problem situated not in printing in the console, but in calculations.

If you have any idea, please help me to solve this. Here is the code:

#include <stdio.h>
#include <iostream>
#include <time.h>
#include <stdlib.h> 

using namespace std;

int level_1()
{
/* LEVEL 1 - ROWNANIE Z 1 NIEWIADOMA*/

int a = 5, b = 123, c = 32;
double x;
double answer;
double licznik, mianownik;
cout << "level 1" << endl;
// losujemy kombinacje (1 z 3) dla roznorodnosci
int losowanie = 3;//= rand() % 3 + 1;
if (losowanie == 1) {
    cout << "Rolled: 1" << endl << endl; // x jest przy wspolczynniku a
    a = 5;
    while (((c - b) % (a) != 0))
    {
        do
        {
            a = rand() % 24 - 12;
        } while (a == 0);
        b = rand() % 24 - 12;
        c = rand() % 24 - 12;

    }
    if (a == 1) cout << "x";
    else if (a == -1) cout << "-x";
    else cout << a << "x";

    if (b > 0) cout << "+" << b;
    else if (b == 0) cout << "";
    else cout << b;
    cout << "=";
    if (c >= 0) cout << c;
    else cout << c;

    x = (c - b) / a;
    cout << endl << "Type x = ";
    cin >> answer;
    if (answer == x)
    {
        cout << endl << "good answer" << endl;
        return 1;
    }
    cout << endl << "bad answer." << endl;
    return 0;
}
else if (losowanie == 2){
    cout << "Rolled: 2" << endl << endl; // x jest przy wspolczynniku b
    a = 5;
    while (((c - b) % (a) != 0))
    {
        do
        {
            a = rand() % 24 - 12;
        } while (a == 0);
        b = rand() % 24 - 12;
        c = rand() % 24 - 12;

    }

    if (b!=0)
    cout << b;
    if (a < 0 || a == 0) cout << "";
    else if (a>0) cout << "+";
    if (a == 1) cout << "x";
    else if (a == -1) cout << "-x";
    else cout << a << "x";
    cout << "=" << c;
    x = (c - b) / a;

    cout << endl << "Type x = ";
    cin >> answer;
    if (answer == x)
    {
        cout << endl << "good answer" << endl;
        return 1;
    }
    cout << endl << "bad answer." << endl;
    return 0;
}
else
{
    cout << "You rolled: 3" << endl << endl; // x jest przy wspolczynniku c
    a = 5;
    while (((c - b) % (a) != 0))
    {
        do
        {
            a = rand() % 24 - 12;
        } while (a == 0);
        b = rand() % 24 - 12;
        c = rand() % 24 - 12;

    }
    cout << a << endl << b << endl << c << endl;



    if (c != 0) cout << c;
    if (b != 0)
    {
        if (b > 0) cout << "+";
        else cout << "";
        cout << b;
    }
    cout << "=";
    if (a == 1) cout << "x";
    else if (a == -1) cout << "-x";
    else cout << a << "x";

    x = ((c - b) / a);
    cout << endl<< "zzz" << x << endl;
    cout << endl << "type x = ";
    cin >> answer;

    if (answer == x)
    {
        cout << endl << "good answer" << endl;
        return 1;
    }
    cout << endl << "bad answer." << endl;
    return 0;



}


return 1;
}


int main()
{
//cout << y << endl;
srand(time(NULL));

while (1){
    level_1();
}

}

Upvotes: 1

Views: 269

Answers (1)

Kevin
Kevin

Reputation: 76234

x=(c-b)/a only holds true for equations of the first form. The other ones would be x = (c-a)/b and x = (a+b)/c. You should change your while loops accordingly.

Upvotes: 1

Related Questions