Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,429 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,905 people online right now. Registration is fast and FREE... Join Now!




Solving a quadradic

 
Reply to this topicStart new topic

Solving a quadradic

captainhampton
15 Jan, 2008 - 11:35 AM
Post #1

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions
Ok for the most part I understand what this question is asking, however there are some parts which seem a little hazy which maybe somebody could help me out with:

Here is the initial question:

Write a C++ program that solves quadratic equations to find its roots.
The roots of a quadratic equation a x2 + b x + c = 0 (where a is not zero) are given by the formula:
(-b ± sqrt (b2 – 4 a c) / 2 a
The value of the discriminant (b2 – 4 a c) determines the nature of the roots. If the value of the discriminant is zero then the equation has a single real root.

If the value of the discriminant is positive then the equation has two real roots. If the value of the discriminant is negative, then the equation has two complex roots. If the roots are real, print out the real roots; otherwise print out a message stating that the roots are complex. The program takes a series of three floating point values (in the order a, b, and c) and outputs the roots for each triplet to another file.

Include a loop that reads each triplet in the file until an end of file is encountered. Please make sure the output file contains appropriate identification to determine the equations for each root. The input file should be named equations.txt and the output file should be named roots.txt.

Now I think the mechanics of it are all down, I am really just confused about the context and maybe just the math surrounding this problem (easy as it is) just the way it is stated makes me second guess what I'm doing...

Here is the code for everything:

CODE


#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cmath>


using namespace std;

//Homework # 2 -- Problem # 1

int main(int argc, char *argv[])
{
    ifstream in_stream;
    ofstream out_stream;
    
    in_stream.open("equations.txt");
    if ( in_stream.fail() ){
         cout << "Input file opening failed." <<endl;
         exit(1);
         }//end if    
        
    out_stream.open("roots.txt");
    if ( out_stream.fail() ){
         cout << "Output file opening failed."<<endl;
         exit(1);
         }//end if
    
    float a, b, c;
    
    while ( ! in_stream.eof()){
    
    in_stream >> a >> b >> c;
          
    out_stream << "The value of the discriminant is: " << (b * b - 4 * a * c) <<endl;
    
    if ( (b * b - 4 * a * c) == 0 ){
       out_stream << "The equation contains a single root" <<endl;
       }//end if
      
    if ( (b * b - 4 * a * c) < 0 ){
         out_stream << "The equation contains two complex roots" <<endl;
         }//end if
        
    if ( (b * b - 4 * a * c) ) > 0 ){
         out_stream << "The equation contains two real roots" <<endl;
         }//end if
    
    
    
    }//end while
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


My actual question is this: How, if the roots are real, do I go about getting them? I can defitinely determine whether or not they are real by seeing if they output a positive discriminant but I have no idea what they are

Any help would be appreciated. Thanks

User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: Solving A Quadradic
15 Jan, 2008 - 11:56 AM
Post #2

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

This will put the roots into variables "root1" and "root2". You'll use slightly different code for each of your three cases (no real roots, one real root, two real roots). This would be your "two real roots" case:

CODE

double discrim = b * b - 4 * a * c;
double sqrtDiscrim = sqrt (discrim);
double root1 = (-1 * b - sqrtDiscrim) / (2 * a);
double root2 = (-1 * b + sqrtDiscrim) /  (2 * a);


User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Solving A Quadradic
15 Jan, 2008 - 11:57 AM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
You may wish to have a look at the following:

http://www.perpetualpc.net/quadsolv_c.html
http://www.dreamincode.net/code/snippet391.htm

User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: Solving A Quadradic
15 Jan, 2008 - 11:59 AM
Post #4

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

Looks like you're using floats, not doubles, so change the doubles to floats. Didn't notice that before. May not matter anyway.

This post has been edited by VernonDozier: 15 Jan, 2008 - 12:00 PM
User is offlineProfile CardPM
+Quote Post

captainhampton
RE: Solving A Quadradic
15 Jan, 2008 - 12:14 PM
Post #5

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions
Thank you both very much I will do so
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:56AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month