In: Computer Science
c++ programming:
Given:
Struct myq
{
double a,b,c
};
X1= -b + sqrt(b*b-4 * a* c)/2 *a;
X2= -b- sqrt(b*b- 4 * a * c)/2 * a;
X0f v= (-b)/( 2*a);
a>0 if maximum otherwise it is minimum
b*b – 4*a*c >0 two unrepeated roots
b*b- 4 * a* c =0 two repeated roots
b*b- 4 * a* c <0 two complex roots
Find the code for the above question below, read the
comments provided in the code for better understanding. If found
helpful please do upvote this.
Please refer to the screenshot of the code to understand the
indentation of the code.
Code
#include <iostream>
#include <cmath>
using namespace std;
//structure defintion
struct myq
{
double a, b, c;
};
//method which returns value of x1
double x1(myq st)
{
return (-st.b + sqrt(pow(st.b, 2) - 4 * st.a * st.c)) / 2 * st.a;
}
//method which returns value of x2
double x2(myq st)
{
return (-st.b - sqrt(pow(st.b, 2) - 4 * st.a * st.c)) / 2 * st.a;
}
//method which returns value of x of a vertex
double x_vertex(myq st)
{
return -st.b / 2 * st.a;
}
//method which returns 1 for max and 0 for min
int max_min(myq st)
{
return st.a > 0 ? 1 : 0;
}
//method to determine the roots
void determine_roots(myq st)
{
double val = (pow(st.b, 2) - 4 * st.a * st.c);
if (val > 0)
{
cout << "Two unrepeated roots";
}
else if (val == 0)
{
cout << "Two repeated roots";
}
else
{
cout << "Two complex roots";
}
}
int main()
{
//create a structure
myq s1 = {2.0, 10.0, 3.0};
//call the various methods
cout << "\nX1 : " << x1(s1);
cout << "\nX2 : " << x2(s1);
cout << "\nX of vertex : " << x_vertex(s1);
int max_mi = max_min(s1);
if (max_mi == 0)
{
cout << "\nMinimum";
}
else
cout << "\nMaximum";
cout << "\nRoot Type : ";
determine_roots(s1);
}
Screenshot
Output