• Log in or sign up for an account here: https://www.sythe.org/login

need help with c++ program

froxy

Newcomer
Joined
Sep 19, 2009
Sythe Gold
0
Messages
1
need help with c++ program
I need some help with a c++ program for my class, i can't figure out whats going on.

Input: from the user

Output: standard output AND file named "program3.txt"

Write a program that will calculate both roots of a quadratic equation. The program should ask the user to input a, b, and c and then write both roots to standard output (the screen) and a file named "program3.txt".

Your program should print the roots ordered from least to greatest.


If the quadratic equation has a negative discriminant your program should output "NO REAL ROOTS".


If the two roots are the same only report one root.


ax2 + bx + c = 0


8c58ae2d322a33f3036800d96db0e91a.png


Code:
Sample Input 	Sample Output
1, -3, -10 	-2, 5
3, 6, 3 	-1
2, -2, -12 	-2, 3
3, -2, 20 	NO REAL ROOTS

Here is so far what ive got

Code:
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double a, b, c, d;
    double root1, root2, temp;
    
    //output
    
    cout << "enter a, b, c";
    cin >> a >> b >> c;
    
    //process
    
    d = pow(b,2) - 4 * a * c;
    if(d >= 0)
    {
          root1 = (-b + sqrt(d))/(2 * a);
          }
          if(d > 0)
          {
                 root2 = (-b + sqrt(d))/(2*a);
                 }
                                  if(root1 > root2)
                 {
                                temp = root1;
                                root1 = root2;
                                root2 = temp;
                                }

                                
                                // output
                                
                                if(d >= 0)
                                {
                                        cout << root1 << " ";
                                        }
                                        if(d > 0)
                                        {
                                               cout << root2 << " " <<endl;
                                        }
                                        if(d < 0)
                                        {
                                               cout <<"No Real Roots" << " ";
                                               }
                                               cin.get();
                                               cin.get();
                                               return 0;
                                               }

Can someone help me out with the sample output. i get the 2nd integer, but i dont get the first.
 
need help with c++ program
Erm... i'm not that advanced in programming, but have studied quadratic formulas abit...

Tell me if i'm wrong, but you have worked out root one and root two as the same.

root1 = (-b + sqrt(d))/(2 * a);
root2 = (-b + sqrt(d))/(2*a);

doesn't one of them need to be a minus?

root2 = (-b - sqrt(d))/(2*a);

If not it's probably me just reading it wrong.

As for your output i'm not entirley sure... sure.

Ill have a try of making this my self, looks fun =]
 
need help with c++ program
I know you want to do this your self, as most people do. But i found this if you want to try see where you have gone wrong. Or look for a few hints.

// Determine real and complex roots of a quadratic equation and check results
// ax^2 + bx + c = 0

#include <iostream>
#include <math.h>
#include <complex>
using namespace std;

int main(void)
{
float a, b, c, x1, x2, root;
complex<float> z;

cout << "Roots of quadratic equation, Enter a, b and c ? ";
cin >> a >> b >> c;

root = b * b - 4.0f * a * c; // b*b - 4ac
if (root >= 0.0f) // if +
{
root = sqrt(root); // real roots
x1 = (-b + root) / (2.0f * a);
x2 = (-b - root) / (2.0f * a);
cout << "Real roots " << x1 << " and " << x2 << endl;
cout << "Test " << a * x1 * x1 + b * x1 + c // check result
<< " and " << a * x2 * x2 + b * x2 + c << endl;
}
else
{ // complex roots
root = sqrt(-root) / (2.0f * a);
x1 = -b / (2.0f * a);
cout << "Complex roots " << x1 << " +- i " << root << endl;
z = complex<float>(x1, root);
cout << "Test " << a * z * z + b * z + c; // check result
z = complex<float>(x1, -root);
cout << " and " << a * z * z + b * z + c << endl;
}
system("pause");
return 0;
}
 
need help with c++ program
yes because someone just learning simple algebra definitely understands how complex and imaginary numbers work.
 
Runescape Gamblingrunescape gambling
Back
Top