[TC] Gaming Forums
C++ Help - Printable Version

+- [TC] Gaming Forums (https://forum.city-driving.co.uk)
+-- Forum: Community Area (/forumdisplay.php?fid=14)
+--- Forum: Technology Area (/forumdisplay.php?fid=82)
+---- Forum: Tech Support (/forumdisplay.php?fid=85)
+---- Thread: C++ Help (/showthread.php?tid=19915)



C++ Help - FolloweR - 2015-10-12 15:49

Eyo people,

I need some help with C++ based stuff. I have to make a problem that solves a biquadratic equation (ax4 +bx2+c=0), but I can only figure out how to make a program that solves a quadratic equation (ax2+bx+c=0). If someone is willing to help me how to make a program which solves a biquadratic equation, I would be very thankful.

P.S. If you need it, that's the code for the program that solves a quadratic equation.

[Image: Ccode.png]


RE: C++ Help - Barney - 2015-10-12 16:12

oh god... didnt see this crap for a long time.
dont u basically just need to do a substitution. replace x2 with y. than u get a quadratic equation what u r alrdy able to solve. in the end you re-substitute. thats all i remember lol xD
also format your code properly. will get messy longterm. tab all lines after a bracket.


RE: C++ Help - FolloweR - 2015-10-12 16:25

I tried doing that, but couldn't manage to get it working. I was able to find y1,y2, but then couldn't re-substitute because it was showing some errors.


RE: C++ Help - Gutholz - 2015-10-12 16:56

post the non-working code (as text, not picture) plus the errors


RE: C++ Help - FolloweR - 2015-10-12 17:44

Hmm, I thought it would work with that, but I guess I've done something wrong again.. I tried substituting y=x*x, but it said x isn't initialized. [Image: c.png]

That's the code:

Code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
        double a,b,c,y1,y2,D;
        cout<<"Enter a,b,c:"<<endl;
        cin>>a>>b>>c;
        if(a==0&&b==0&&c==0) cout<<"Every x is solution"<<endl;
        if(a==0&&b!=0)cout<<"y="<<-c/b<<"x1="<<sqrt(-c/b)<<"x2="<<-sqrt(-c/b)<<endl;
    if(a!=0) {
        D=b*b-4*a*c;
        y1=(-b-sqrt(D))/(2*a);
        y2=(-b+sqrt(D))/(2*a);
    if(D>0) {
        cout<<"y1="<<y1<<"x1="<<sqrt(y1)<<"x2="<<-sqrt(y1)<<endl;
        cout<<"y2="<<y2<<"x3="<<sqrt(y2)<<"x4="<<-sqrt(y2)<<endl;}
    if(D==0)cout<<"y1=y2="<<-b/(2*a)<<"x1="<<sqrt(-b/(2*a))<<"x2="<<-sqrt(-b/(2*a))<<endl;
    if(D<0)cout<<"No real roots"<<endl;
    }
    return 0;
    }



RE: C++ Help - Gutholz - 2015-10-12 19:37

It prints x1=NAN (not a number) because y1 is negative and sqrt() function can not calculate squareroot of negative numbers.

In first post you wrote:
Quote:ax4 +bx2+c=0
I assume you meant:
a(x^4) +b(x^2)+c=0
?

With the input from your screenshot:
a=1
b=4
c=1
The graph looks like this:
[Image: MTdVd4S.png]
=> This function has no solution.
Try entering some other values, plot the graph, and compare, might be that your program is already correct?


RE: C++ Help - FolloweR - 2015-10-13 16:07

Okay, I've finally done it. Figured out a way to use as many if and else's as possible, and it seems to work. That's the code:
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
        double a,b,c,y1,y2,D;
        cout<<"Enter a,b,c:"<<endl;
        cin>>a>>b>>c;
        if(a==0&&b==0&&c==0) cout<<"Every x is solution"<<endl;
        if(a==0&&b!=0)cout<<"y="<<-c/b<<"x1="<<sqrt(-c/b)<<"x2="<<-sqrt(-c/b)<<endl;
    if(a!=0) {
        D=b*b-4*a*c;
        y1=(-b-sqrt(D))/(2*a);
        y2=(-b+sqrt(D))/(2*a);
    if(D>0) {
        cout<<"y1="<<y1<<"x1="<<sqrt(y1)<<"x2="<<-sqrt(y1)<<endl;
        cout<<"y2="<<y2<<"x3="<<sqrt(y2)<<"x4="<<-sqrt(y2)<<endl;}
    if(D==0)cout<<"y1=y2="<<-b/(2*a)<<"x1="<<sqrt(-b/(2*a))<<"x2="<<-sqrt(-b/(2*a))<<endl;
    if(D<0)cout<<"No real roots"<<endl;
    }
    return 0;
    }

Cheers for the help guys!