5 Write a CC program that solves a quadratic equation as des



5) Write a C/C+ program that solves a quadratic equation, as described by the formulas for roots 1 and 2 below. Use if -elseif loops to determine if the solutions are real or complex, ie if the discriminant is greater than or equal to zero, then roots are real and the roots are complex (ie, a real and imaginary part) when the discriminant is less than zero); then evaluate the corresponding coefficients Ar BC A** x2 B x C 0 DIS (B B-4 A c) If (DIS >m 0) Root 1 B sqrt(DIS))/(2 A) Root2 B sqrt(DIS))/ (2 A); ll you must determine what formulas and print statements needed to print out both the real and imaginary parts of the complex solutions for roots 1 and 2. After writing your program, test for following inputs: 6.) write a clc* function named DISTTthat asks the user to enter two points in (xya) (ie. P1 (x1 v1.a1) and P2 (x2,Y2,zz then carries out the following calculations (a) Distance D between the two points: (b) The amplitudes of the vectors drawn from the origin to the two points

Solution

#include <complex>
#include <iostream>
#include <utility>
#include <vector>
#include <istream>
#include <tuple>

std::vector<std::complex<double>> quadRoot(double A, double B, double C){

auto DIS = (B*B-4*A*C);
std::vector<std::complex<double>> roots;
  
if(DIS >= 0){
DIS = sqrt(DIS);
double r1 = (-B+DIS)/(2*A);
double r2 = (-B-DIS)/(2*A);
if(DIS!=0){
roots = {r1, r2};
}
else{
roots = {r1};
}
  
}
else{
DIS = sqrt(-DIS);
auto r1 = std::complex<double>(-B/(2*A), DIS/(2*A));
auto r2 = std::complex<double>(-B/(2*A), -DIS/(2*A));
roots = {r1, r2};
}

return roots;

}

void checkQuadRoots(){
auto v1 = quadRoot(1, 2, -8);

std::cout << \"Roots 1 \" << std::endl;
for(auto r : v1){ std::cout << r << \"\\t\"; };

  
std::cout << \"Roots 2 : \" << std::endl;
auto v2 = quadRoot(1,-9, 8);
for(auto r : v2){ std::cout << r << \"\\t\"; };

std::cout << \"Roots 3 : \" << std::endl;
auto v3 = quadRoot(4, 2, 5);
for(auto r : v3){ std::cout << r << \"\\t\"; };
}


struct Point3D{
double x;
double y;
double z;

friend std::istream& operator>> (std::istream& is, Point3D& p){
char ch[4];
  

is >> ch[0] >> p.x >> ch[1] >> p.y >> ch[2] >> p.z >> ch[3];

if(ch[0]==\'(\'&&ch[1]==\',\'&&ch[2]==\',\'&&ch[3]==\')\'){
return is;
}
else{
std::cerr << \"error in input\";
return is;
}
}

friend double distance(const Point3D& p1, const Point3D& p2){
auto dx = p1.x-p2.x;
auto dy = p1.y -p2.y;
auto dz = p1.z -p2.z;
return sqrt(dx*dx+dy*dy+dz*dz);
}

friend double amplitude(const Point3D& p){
return sqrt(p.x*p.x+p.y*p.y+p.z*p.z);
}

friend double dotP(const Point3D& p1, const Point3D& p2){
return (p1.x*p2.x + p1.y*p2.y + p1.z*p2.z);
}

friend double angleV(const Point3D& p1, const Point3D& p2){
return std::acos(dotP(p1,p2)/(amplitude(p1)*amplitude(p2)));
}
};

void checkVec(){
Point3D p1, p2;
std::cout << \"\ P1:\";
std::cin >> p1;
std::cout << \"P2:\";
std::cin >> p2;

std::cout << \"Distance : \" << distance(p1,p2) << std::endl;
std::cout << \"Amplitude :\" << amplitude(p1) << \"\\t\" << amplitude(p2) << std::endl;
std::cout << \"DotProducet : \" << dotP(p1, p2) << std::endl;
std::cout << \"Angle : \" << angleV(p1,p2) << std::endl;
  
}

std::vector<std::tuple<double, double, double, double>> table(){
double betaF = 50;
double xF = 0.5;
double beta0 = 50;
double x0 = 0;
std::vector<std::tuple<double, double, double, double>> result;
result.reserve(240);

for(int i = 0; i <= 40; ++i){
for(int j = 0; j <= 6; ++j){
double beta = betaF*j + beta0;
double x = xF*i + x0;
double F = beta*x;
double E = 0.5*beta*x*x;
result.push_back(std::make_tuple(x,beta,F,E));
}
}
return std::move(result);
}

void printTable(){
std::cout << \"\ x\\tBeta\\tForce\\tEnergy\ \";
auto res = table();
for(auto& v: res){
double x, beta, F, E;
std::tie(x, beta, F, E) = v;
std::cout << x << \"\\t\" << beta << \"\\t\" << F << \"\\t\" << E << \"\ \";
}
}


int main(){
checkQuadRoots();
checkVec();
printTable();
}

 5) Write a C/C+ program that solves a quadratic equation, as described by the formulas for roots 1 and 2 below. Use if -elseif loops to determine if the soluti
 5) Write a C/C+ program that solves a quadratic equation, as described by the formulas for roots 1 and 2 below. Use if -elseif loops to determine if the soluti
 5) Write a C/C+ program that solves a quadratic equation, as described by the formulas for roots 1 and 2 below. Use if -elseif loops to determine if the soluti

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site