-1

I want to take a mathematical (single variable algebraic) equation from a user, say x^3 - 4x -9 = 0 and want to evaluate it for different values of x.

I've tried creating two arrays; one for getting the input of powers of x and another for coefficients. The program asks from the user about the number of terms present in the equation and then it makes the arrays of that number-1. But, this algorithm is helpful in only printing the equation and not manipulating or evaluating it.

/* this code can take some relevant input from the user and form an equation on the screen to show it to the user. */

#include<bits/stdc++.h>
#include<conio.h>

using namespace std;

class Bisection
{
    int noofcaparr, *coeffarr, *powerarr, eqn;
    char *eqnprnt;

    public:
        void geteqn();
        void showeqn();
        void setupeqn();

};

void Bisection::geteqn()
{
    int c, i, n;
    system("cls");
    cout<<"\n\n\t\t How many terms do you have in your equation? ";
    cout<<"\n\t For Example:  x^3 - 4x - 9 = 0   , has '3' terms     and ";
    cout<<"\n\t               x^4 + x^3 - 7x^2 - x + 5 = 0   , has '5' terms";
    cout<<"\n\t Enter the number of terms present in your equation:  ";
    cin>>this->noofcaparr;
    n = this->noofcaparr-1;

    this->coeffarr =  new int[n];
    this->powerarr = new int[n];

    for(i=0, c=1; i<=n; i++, c++ )
    {
        cout<<endl<<endl<<"\t\t Please enter the "<<c<<" th/st/nd/rd highest degree of x:  ";
        cin>>this->powerarr[i];

        cout<<endl<<endl<<"\t\t Please enter the coefficient of "<<c<<" th/st/nd/rd highest degree of x (with sign -/+):  ";
        cin>>this->coeffarr[i];
    }

    cout<<endl<<endl<<"\n\n\t Values Set!";
    getch();


}

void Bisection::showeqn()
{
    int i, n;
    n = this->noofcaparr-1;
    system("cls");
    cout<<endl<<endl<<"\t\t Your equation is:   ";

        for(i=0; i<=n; i++ )
    {

        if(this->powerarr[i]==0)
        {
                    if(i==0)
        {
            if(this->coeffarr[i]>= 0)
            {
                if(this->coeffarr[i]==1)
                {
                    cout<<" ";
                }

                else
                {
                    cout<<" "<<(this->coeffarr[i])<<" ";
                }
            }
            else
            {

                if(this->coeffarr[i]== -1)
                {
                    cout<<" -"<<" ";
                }

                else
                {
                    cout<<" "<<(this->coeffarr[i])<<" ";
                }

            }
        }

        else
        {
            if(this->coeffarr[i]>= 0)
            {
                cout<<" +"<<(this->coeffarr[i])<<" ";
            }

            else
            {
                cout<<" "<<(this->coeffarr[i])<<" ";
            }
        }
        }

        else
        {
            if(this->powerarr[i]==1)
            {
                        if(i==0)
        {
            if(this->coeffarr[i]>= 0)
            {
                if(this->coeffarr[i]==1)
                {
                    cout<<"x";
                }

                else
                {
                    cout<<(this->coeffarr[i])<<"x";
                }

            }
            else
            {

                if(this->coeffarr[i]== -1)
                {
                    cout<<" -"<<"x";
                }

                else
                {
                    cout<<(this->coeffarr[i])<<"x";
                }

            }
        }

        else
        {
            if(this->coeffarr[i]>= 0)
            {
                cout<<"+"<<(this->coeffarr[i])<<"x";
            }

            else
            {
                cout<<(this->coeffarr[i])<<"x";
            }
        }
            }

            else
            {
                        if(i==0)
        {
            if(this->coeffarr[i]>= 0)
            {
                if(this->coeffarr[i]==1)
                {
                    cout<<"x^"<<this->powerarr[i]<<"  ";
                }

                else
                {
                    cout<<" "<<(this->coeffarr[i])<<" "<<"x^"<<this->powerarr[i]<<"  ";
                }

            }
            else
            {

                if(this->coeffarr[i]== -1)
                {
                    cout<<" -"<<"x^"<<this->powerarr[i]<<"  ";
                }

                else
                {
                    cout<<" "<<(this->coeffarr[i])<<" "<<"x^"<<this->powerarr[i]<<"  ";
                }

            }
        }

        else
        {
            if(this->coeffarr[i]>= 0)
            {
                cout<<" +"<<(this->coeffarr[i])<<" "<<"x^"<<this->powerarr[i]<<"  ";
            }

            else
            {
                cout<<" "<<(this->coeffarr[i])<<" "<<"x^"<<this->powerarr[i]<<"  ";
            }
        }
            }
        }
    }
    cout<<" = 0";
    getch();
}
int main()
{
    Bisection a;

    a.geteqn();
    a.showeqn();

    getch();
    return(0);
}

Try checking this code. If it asks for input then let's try an example: In the first input, type 3, in second 3, then 1, then 1, then -4, then 0 and then -9. This will print the following equation on the screen: x^3 - 4x - 9 = 0

But, I cannot manipulate or evaluate this equation. If I want to calculate the equation by making it equal to fx and taking different values of x and then evaluate the value of fx, I cannot do that.

I have already tried searching for it on the internet but all of the solutions were either unhelpful or too complex to understand.

I am a very new programmer and I know nothing about data structure, bison or any similar parser. Please explain/help me in a simple way, as simple as possible.

Please do not downvote, if you find anything wrong in the question then, let me know in the comments; I'll take my question down. Thanks in advance!

No Name
  • 41
  • 3
  • Your question is too broad. Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. See the [How to Ask](https://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – L. F. Jul 10 '19 at 07:24
  • 1
    @L.F. I think I have provided all the details that might be required and my question is centralized into only one specific question that is getting an algebraic equation from the user and use it as a mathematical expression in c++ program. – No Name Jul 10 '19 at 07:26
  • "getting an algebraic equation from the user and use it as a mathematical expression in c++ program" that's already two questions. And "getting an algebraic equation from the user" alone is already too broad. – L. F. Jul 10 '19 at 07:28
  • 1
    @L.F. If possible, then check the code that I've written in this post. If it is good for getting an algebraic equation from the user then, my question will be limited only to how to convert it to a mathematical expression in my c++ program. – No Name Jul 10 '19 at 07:33
  • 4
    "Please do not downvote" - sorry, that's not how it works here. The purpose of downvotes is not to be mean for you. There are just a way to hide question that we are consider to not be helpful to someone who tries to search for their problem. That's one of the reason why it is so easy to find an answer here. It doesn't mean we don't like you, so don't worry. – Piotr Siupa Jul 10 '19 at 07:34
  • I would consider having only one array - the one for coefficients, so e.g. coefficient of 3rd power is `coeffarr[3]` and if you don't have this power in the equation, just write `0` to this position in the array. I think the algorithm will be simpler this way. – Piotr Siupa Jul 10 '19 at 07:38
  • @NO_NAME Yeah that's very good. Thank You. This will make the code easier to work on. I am modifying it rn on my code. – No Name Jul 10 '19 at 07:47
  • @NO_NAME What do you think can I use this code to convert the generated equation as a mathematical expression in my c++ program? If Yes then, how will it be possible? If No then, what should be improved in the current algorithm? – No Name Jul 10 '19 at 07:51
  • Well, evaluating the equation isn't hard. A simplified form would be `int result = 0; int power = 1; for (int i = 0; i != n; ++i) { result += power * coeffarr[i]; power *= x; }`. I didn't test this but it should be close to the right solution. – Piotr Siupa Jul 10 '19 at 08:07
  • 1
    You are mixing up _equation_ **x^3 - 4x - 9 = 0** and _function_ **x^3 - 4x - 9**. Saying you _want to calculate the equation by making it equal to fx and taking different values of x and then evaluate the value of fx_ makes little sense - we _solve the equation_ (getting one solution x), while we _calculate the function_ (for _different values of x_). Now, what do you want? – Armali Jul 10 '19 at 08:07
  • @NO_NAME are you online for a span of some minutes? Because I'll try this piece of code and let you know if it worked for me. Thank You! – No Name Jul 10 '19 at 08:10
  • 1
    @Armali I am sorry about that. I want the second thing; i.e. making it equal to *fx* and then evaluate *fx* for different values of ***x*** . – No Name Jul 10 '19 at 08:12
  • @NO_NAME Although your code didn't exactly work the way that I wanted, the hint/clue that your algorithm gave was more than enough to find out the working way. TYSM – No Name Jul 10 '19 at 08:45

1 Answers1

0

As NO_NAME thinks, evaluating the function isn't hard, though with the current data layout we can't compute the power with his suggested iteration, since there isn't a term for every exponent. But this variant works:

double Bisection::evalfun(int x)
{
    double f = 0;
    for (int i = 0; i < this->noofcaparr; ++i)
        f += coeffarr[i] * pow(x, powerarr[i]);
    return f;
}

Example calls for different values of x:

    cout <<endl;
    for (int x = -5; x <= 5; ++x) cout <<a.evalfun(x) <<'\t';
    cout <<endl;

Maybe you want to use double x rather than int x.

Armali
  • 18,255
  • 14
  • 57
  • 171