×

Warning

Empty password not allowed.
Welcome, Guest
Username: Password: Remember me
Welcome to the Dogfight forum!

Tell us and other pilots who you are, what you like and why you became a Dogfight pilot.
We welcome all new members and hope to see you around a lot!
  • Page:
  • 1

TOPIC:

Help with c++ 11 years 8 months ago #155264

  • [e] Science_Dude
  • [e] Science_Dude's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
  • DONT MESS WITH ME OR ILL USE SCIENCE AGAINST U !
  • Posts: 570
  • Thank you received: 260
i tried making a quadratic equation claculator.

I succesfully did it using turboc++

i used the following code :

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float a,b,c,x1,x2,d;
cout<<"enter values of the coefficients of the quadratic equation";
cin>>a>>b>>c;
d=b*b-4*a*c;
float y=sqrt(d);
if(y==0)
{
x1=x2=(-b+y)/2*a;
cout<<"x1=x2="<<x1<<"\n";
}
else if(y<0)
{
cout<<"the roots are complex";
}
else if(y>0)
{
x1=(-b+y)/2*a;
x2=(-b-y)/2*a;
cout<<"x1="<<x1;
cout<<"x2="<<x2;
}
getch();
}


now...
Ive taken the coefficients as the input .

Is there any way , that i can take the equation as the input and my program will itself find the coefficients ?

And i also want to display the graph of the quadratic equation along with the roots.

How do i do it ??

Please help !

MASTERY BEYOND BELIEF

Please Log in to join the conversation.

. 11 years 8 months ago #155265

  • Slash
  • Slash's Avatar
  • Offline
  • User is blocked
  • User is blocked
  • Posts: 361
  • Thank you received: 452
.
The following user(s) said Thank You: [e] Science_Dude

Please Log in to join the conversation.

Last edit: by Slash.

Help with c++ 11 years 8 months ago #155270

  • [e] Science_Dude
  • [e] Science_Dude's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
  • DONT MESS WITH ME OR ILL USE SCIENCE AGAINST U !
  • Posts: 570
  • Thank you received: 260
Also , i would like to know , how to use complex numbers in c++ so that it can also give me complex roots.

MASTERY BEYOND BELIEF

Please Log in to join the conversation.

Help with c++ 11 years 8 months ago #155352

  • [NLR]TheGreat
  • [NLR]TheGreat's Avatar
  • Offline
  • Senior Member
  • Senior Member
  • Posts: 78
  • Thank you received: 13
Hi,

You can try using this for the complex number issue you are having: forums.devshed.com/c-programming-42/usin...ers-in-c-109488.html

As for your other question, you need to write the equation with the variables the same way you wrote for the other. Just take the inputs for the different values and run it through the equation.

Sorry I wasn't so clear, wrote this on my phone, and my main language is Java. :)

Let me know if you have more questions, I'll try to be more specific.

TheGreat
Enjoy the beam of bullets coming at you! :)
The following user(s) said Thank You: [e] Science_Dude

Please Log in to join the conversation.

Last edit: by [NLR]TheGreat.

Help with c++ 11 years 8 months ago #155371

  • [e] Science_Dude
  • [e] Science_Dude's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
  • DONT MESS WITH ME OR ILL USE SCIENCE AGAINST U !
  • Posts: 570
  • Thank you received: 260

TheGreat wrote: Hi,

You can try using this for the complex number issue you are having: forums.devshed.com/c-programming-42/usin...ers-in-c-109488.html

As for your other question, you need to write the equation with the variables the same way you wrote for the other. Just take the inputs for the different values and run it through the equation.

Sorry I wasn't so clear, wrote this on my phone, and my main language is Java. :)

Let me know if you have more questions, I'll try to be more specific.

TheGreat


Ok ....
I came to know how to use complex numbers in c++.

Now just two more questions...

I didnt get what you were saying about using the equation for inputs rather than using the coefficients...
Please explain that in detail.

And how do i plot the graph of that quadratic equation in c++ ?

MASTERY BEYOND BELIEF

Please Log in to join the conversation.

Help with c++ 11 years 8 months ago #155399

  • |111th|KptnSINGH
  • |111th|KptnSINGH's Avatar
  • Offline
  • Platinum Member
  • Platinum Member
  • Top dolllar paid for ur rusty wings
  • Posts: 1300
  • Thank you received: 705
Haha if u replace the ++ WITH --
You get my cumulative high school gpa :blush:

this is how i fly !!! :)
The following user(s) said Thank You: [e] Science_Dude

Please Log in to join the conversation.

Help with c++ 11 years 8 months ago #155431

  • Hyperdrive
  • Hyperdrive's Avatar
  • Offline
  • Platinum Member
  • Platinum Member
  • built from spare body parts of dead combat pilots
  • Posts: 387
  • Thank you received: 298
What has two thumbs and knows nothing about c++? This guy, right here! :woohoo:
The following user(s) said Thank You: [e] Science_Dude

Please Log in to join the conversation.

Help with c++ 11 years 8 months ago #155488

  • [e]hammer_tool
  • [e]hammer_tool's Avatar
  • Offline
  • Platinum Member
  • Platinum Member
  • ut malleus omnia similis clavum
  • Posts: 1641
  • Thank you received: 1285

not sure on the platform you are using, but try this koolplot.codecutter.org/

you might have some cooler libraries for plotting already available, this is just a basic start.

ut malleus omnia similis clavum
The following user(s) said Thank You: [e] Science_Dude

Please Log in to join the conversation.

Help with c++ 11 years 8 months ago #155494

  • Manfred
  • Manfred's Avatar
  • Offline
  • Platinum Member
  • Platinum Member
  • Just Manfred
  • Posts: 2845
  • Thank you received: 3866
As for taking the formula as the input, I'm sure c++ has text search strings similar to vbasic. Sorry, it's been decades since I've done anything in C. Vbasic a Find function that returns the placement of the first found search string in the text.

One nice thing about the simple algebra quadratic is its form. Maximum power of x is squared, and only one variable. And if you force the inputter to follow a rule of higher order terms first, then it's really easy. In pseudocode with some vbasic references:

Example is 4x^2+3x-6
- Find the length of the string. Vbasic has a function =len ([formulastring])
- Find where "x^2" is. Vbasic, WhereVar=find ([formulastring],"x^2") or something like that, in this case = 2
- Using that value, limit the formula string so that you have only the terms right of "x^2". In vbasic, you use =right ([formulastring], len [formulastring] - WhereVar), in this case = "+3x-6"
- Then do a Find in that string for "x"
- If you find nothing, then there is no term. Same for x^2.
- You can also find locations for pluses and minuses
- With that dataset, you can isolate the coefficients and constants. You will need to do some deltas between sign locations and x^2 and x locations to determine the number of digits in each coeff or constant
- Easy peasy!

But I have no idea what the specific functions are in c++.

Manfred
The following user(s) said Thank You: [e] Science_Dude

Please Log in to join the conversation.

Help with c++ 11 years 8 months ago #155570

  • [e] Science_Dude
  • [e] Science_Dude's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
  • DONT MESS WITH ME OR ILL USE SCIENCE AGAINST U !
  • Posts: 570
  • Thank you received: 260

hammer_tool wrote: not sure on the platform you are using, but try this koolplot.codecutter.org/

you might have some cooler libraries for plotting already available, this is just a basic start.


I dont have a pc :pinch:

i mean , i have it , but its at my native town.

And , im out of town for higher education.
I use an android tab.
There are many compilers available for android , but ... They are costly...... I mean paid....
They are not costly ... But i dont require that high class compilers.

I installed dosbox on my low end android tab.

And ..... Now i use , all time favourite ms dos based TurboC++ ! :woohoo:

So i dont know if that graphs libraries are supported or not :(

i'll try .

Thanx any way. :)

MASTERY BEYOND BELIEF

Please Log in to join the conversation.

Help with c++ 11 years 8 months ago #155582

  • [e]hammer_tool
  • [e]hammer_tool's Avatar
  • Offline
  • Platinum Member
  • Platinum Member
  • ut malleus omnia similis clavum
  • Posts: 1641
  • Thank you received: 1285

Turbo C was what I started on way back in the early 90's cool. Not sure but there should be plot libraries you can use.

ut malleus omnia similis clavum

Please Log in to join the conversation.

Help with c++ 11 years 8 months ago #156591

  • Luna
  • Luna's Avatar
  • Offline
  • User is blocked
  • User is blocked
  • Posts: 1643
  • Thank you received: 1516
I’m not privy to C++ syntax; I’m more more accustomed to Java. It’s what I’ve worked with in high school, haha!

I don’t want to throw you off with what you have so far, but maybe I can help you by showing you what I did when I coded a Quadratic Calculator in Java. I used Netbeans 7.3, btw. It's an open-source program, so feel free to check it out!




It starts off pretty basic. Creating int’s a and b to hold the coefficients, and c as the constant; and creating roots 1 and 2 plus the discriminant. The code in lines 21-28 prompt the user to input using the console.

It starts to get juicy once you hit line 30. Remember, we've got to take this into account:




It's best to find the discriminant first. (b^2-[4ac])

To find the discriminant, I used the method Math.pow (or function, whatever you prefer to call it in C++) to find the square root of b, then multiplied it by 4ac. Set that to the double discriminant.

Once you have the discriminant, you can use it to find roots 1 and 2. Negative b plus or minus the square root (used method Math.sqrt here) of the discriminant divided by 2a. Of course, you’ll do it twice to find both solutions to x (hence, plus or minus the square root of the discriminant).




From there, you print out roots 1 and 2, and you’ve got yourself a quadratic calculator!

Here’s some screenshots of it in action (I used the quadratic equation y = x^2 + 4x - 5):






NaN means Not a Number, it happens when the solutions to your input are imaginary.

Now, in terms of making a program that graphs the solution... that's what your standard TI's are for! Go buy yourself one and save yourself the trouble! :P

Hope this helps!

Please Log in to join the conversation.

Last edit: by Luna.

Help with c++ 11 years 8 months ago #156630

  • [e] Science_Dude
  • [e] Science_Dude's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
  • DONT MESS WITH ME OR ILL USE SCIENCE AGAINST U !
  • Posts: 570
  • Thank you received: 260
ooo :whistle:

Java is similar to c++ :cheer:

but i figured out all those problems and my program , now also gives me complex roots ! YAY !

I figured out that instead of using those complex statements of complex numbers , why not use my brain !!

And...... I used it and i got a solution .

I just took the absolute value of the discriminant , took its square root , divided it by 2*a and assigned that value to a variable x.

Then i divided -b by 2*a and assigned that value to another variable y..

Then just simply display it in x+iy / x-iy format !

This works flawlessly.....

Im still stuck on taking the equation as the input instead of taking coefficients as the input......


Thanx for help......

And , i'm not gonna buy anything...
I just do this for fun !

Im slowly moving towards making more complex programs :)

MASTERY BEYOND BELIEF

Please Log in to join the conversation.

Help with c++ 11 years 8 months ago #156633

  • Manfred
  • Manfred's Avatar
  • Offline
  • Platinum Member
  • Platinum Member
  • Just Manfred
  • Posts: 2845
  • Thank you received: 3866
As for taking the equation as input and breaking it into the correct parts, here are some references to the FIND and LENGTH functions in c++:
www.cplusplus.com/reference/string/string/find/
www.cplusplus.com/reference/string/string/length/

I couldn't find anything on Left or Right text functions, but armed with those functions, you should be able to do dissect the equation from standard format.

Manfred
The following user(s) said Thank You: [e] Science_Dude

Please Log in to join the conversation.

Help with c++ 11 years 8 months ago #156634

  • Luna
  • Luna's Avatar
  • Offline
  • User is blocked
  • User is blocked
  • Posts: 1643
  • Thank you received: 1516
Those links remind me of the hours upon hours of sifting through Java documentation. :pinch:

Please Log in to join the conversation.

  • Page:
  • 1
Time to create page: 0.825 seconds