Yea, but I wasn't talking about the roots. I was talking about the min and max. In other words, the point on the equation with the max value of y, and the min value of y.
In math you differentiate the equation and set the answer to zero, to find the min or max. I wanted to know if there is something similar that can be done in c++
#include<iostream>
usingnamespace std;
int main()
{
double value,min,max,avg,sum;
int count;
int Degree;
count=0;
min=0.0;
max=0.0;
sum=0.0;
cout<<"put your value ^_^ ,if you want stope put -1 like a value please "<<endl;
cin>>value;
while(value!=-1)//if you want stope put -1 like a value please
{
sum +=value;
count++;
if(value>max)
max=value;
elseif(value<max)
min=value;
cin>>value;
}
cout<<" the max value is "<<max<<endl;
cout<<"the min value is "<<min<<endl;
cout<<"the sum is "<<sum<<endl;
cout<<"the count is "<<count<<endl;
avg=sum/count;
cout<<"the avg is "<<avg<<endl;
cout<<"enter your Degree please "<<endl;
cin>>Degree;
if(Degree>=avg)
cout<<"pass"<<endl;
else
cout<<"Failed"<<endl;
system("pause");
return 0;
}
I started out posting regarding the roots (which is a different topic).
What you need is to find where the slope of the curve is zero. I suppose you could input the coefficients a,b and c, then follow some simple rules to get the derivative.
f(x) = a*x^2 + b*x + c
differentiate f'(x) = 2*a*x + b
Max or min when 2*a*x + b = 0
Hence x = -b/ (2 * a)
Then find the value of the original function f(x) for that value of x.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
class quadratic {
double a, b, c;
public:
quadratic(double A, double B, double C) : a(A) , b(B), c(C) {};
double fx(double x) { return a*x*x + b*x + c; }
double maxminX() { return -b / (2.0 * a); }
double maxmin() { return fx(maxminX()); }
};
int main()
{
quadratic equation(-2, 36, -177);
std::cout << "max or min at x = " << equation.maxminX() << std::endl;
std::cout << "max or min is " << equation.maxmin() << std::endl;
}