Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

THIS IS FOR C++ ONLY A quadratic equation is a polynomial equation of degree 2,

ID: 3844192 • Letter: T

Question

 THIS IS FOR C++ ONLY  A quadratic equation is a polynomial equation of degree 2, commonly written in the form p(x) = ax 2 + bx + c.  Contents of coeffs.txt = a b c  Contents of roots.txt = the equation ax^2+bx+c=0 has roots, root and root2  Contents of coeff.txt = 2.5 6.7 -3     And here is the pseudocode,  function main ()  int  define ifstream object named fin  fin.open("coeffs.txt")  a  read_coef(fin)  b  read_coef(fin)  c  read_coef(fin)  fin.close()  root1  calc_root1(a, b, c) root2  calc_root2(a, b, c) output(a, b, c, root1, root2)  return 0  end function main   function read_coef (ifstream& fin)  double  define double variable coef  read from fin into coef  return coef  end function read_coef   function discriminant (double a, double b, double c)  double  return b 2  4ac  end function discriminant   function calc_root1 (double a, double b, double c)  double  return (b + discriminant(a, b, c)) ÷ (2a)  end function calc_root1   function calc_root2 (double a, double b, double c)  double  return (b - discriminant(a, b, c)) ÷ (2a)  end function calc_root2   function output (double a, double b, double c, double root1, double root2)  void  define ofstream object named fout  fout.open("roots.txt")  configure fout so real numbers are displayed in fixed notation with five digits after the decimal point  send to fout "The equation ", a, "x^2 + ", b, "x + ", c, " = 0 has roots ", root1, " and ", root2 followed by newline fout.close()  end function output  ACTUAL PROGRAMMING CODE  #include ???         // For sqrt() #include ???         // For ifstream and ofstream classes #include ???         // For setprecision() #include ???         // For cin, cout, fixed, and endl  using namespace std;  //------------------------------------------------------------------------------ // Implement read_coeff(). Make sure to specify that the parameter fin is // defined as ifstream& (we will discuss what the & means later in the course). //------------------------------------------------------------------------------ ???  //------------------------------------------------------------------------------ // Implement discriminant(). //------------------------------------------------------------------------------ ???  //------------------------------------------------------------------------------ // Implement calc_root1(). //------------------------------------------------------------------------------ ???  //------------------------------------------------------------------------------ // Implement calc_root2(). //------------------------------------------------------------------------------ ???  //------------------------------------------------------------------------------ // Implement output(). //------------------------------------------------------------------------------ ???  //------------------------------------------------------------------------------ // Implement main(). //----------------------------------------------------------------------------- ??? 

Explanation / Answer

Here is the code for you:

#include <cmath> // For sqrt()
#include <fstream> // For ifstream and ofstream classes
#include <iomanip> // For setprecision()
#include <iostream> // For cin, cout, fixed, and endl

using namespace std;

//------------------------------------------------------------------------------
// Implement read_coeff(). Make sure to specify that the parameter fin is
// defined as ifstream& (we will discuss what the & means later in the course).
//------------------------------------------------------------------------------
double read_coeff(ifstream &fin)
{
    int x;
    fin>>x;
    return x;
}

//------------------------------------------------------------------------------
// Implement discriminant().
//------------------------------------------------------------------------------
double discriminant(double a, double b, double c)
{
    return b*b - 4*a*c;
}

//------------------------------------------------------------------------------
// Implement calc_root1().
//------------------------------------------------------------------------------
double calc_root1(double a, double b, double c)
{
    return -b + sqrt(discriminant(a, b, c)) / (2*a);
}

//------------------------------------------------------------------------------
// Implement calc_root2().
//------------------------------------------------------------------------------
double calc_root2(double a, double b, double c)
{
    return -b - sqrt(discriminant(a, b, c)) / (2*a);
}

//------------------------------------------------------------------------------
// Implement output().
//------------------------------------------------------------------------------
void output(double a, double b, double c, double root1, double root2)
{
    ofstream fout;
    fout.open("roots.txt");
    fout<<fixed<<setprecision(5);
    fout<<"The equation"<<a<<"x^2 + "<<b<<"x + "<<c<<" = 0 has roots "<<root1<<", and "<<root2<<endl;
}

//------------------------------------------------------------------------------
// Implement main().
//-----------------------------------------------------------------------------
int main()
{
    ifstream fin;
    fin.open("coeffs.txt");
    double a = read_coeff(fin);
    double b = read_coeff(fin);
    double c = read_coeff(fin);
    fin.close();
    double root1 = calc_root1(a, b, c);
    double root2 = calc_root2(a, b, c);
    output(a, b, c, root1, root2);
    return 0;
}