My First Coding Project
My First Coding Project
This program is not perfect, so it has a few bugs. Before, I did not know about concepts
such as truncation and roundoff error. The figure below shows this program working as intended,
but it will not always correctly find critical points for every quadratic, cubic, or quartic equation.
Despite its downfalls, this is a program I am proud to display. Combining my interests in
calculus and computer programming was challenging, and overcoming each little problem I
encountered was greatly rewarding.
/* This class will help store needed information about the equation */
class Polynomial
{
private:
static const int CS = 6;
static const int DS = 5;
const string term[6] = { " ", "x", "x^2", "x^3", "x^4", " " };
const int exponent[5] = { 4, 3, 2, 1, 0 }; // Holds the degree of each term
int coefficient[CS]; // Holds the coefficient of each term
int derivative[DS]; // Helps to store the derivative of the user's
equation
int numPS; // The number of possible solutions
int fndSol; // The number of solutions that have been found
vector<int>numerator; /* These two vector will help to store possible solutions */
vector<int>denominator;
public:
Polynomial();
void setCoe(int); // This function helps the user to create their
equation
void setDer(); // This function finds the derivative
void setNum(int); // This function will find factors of the leading
coefficient
void setDen(int); // This function will find factors of the constant
void setNumPS(int); // This function will store the number of possible
solutions
int getNumPS(); // This function returns numPS
int getFndSol(); // This function returns fndSol
int findC(int[], int); // This function will find the constant
int findLC(int[], int); // This function will find the leading coefficient
void displayEq(int[], int, int); // This function will use an array to display an
equation
};
/* Function prototypes */
int validateInput(int, int);
int displayMenu();
int gcd(int, int);
int findGCD(int[], int);
int main()
{
Polynomial eq;
Solution sol;
int choice;
choice = displayMenu();
eq.setNumPS(choice);
eq.setCoe(choice);
return choice;
}
int displayMenu()
{
Polynomial eq;
system("CLS");
cout << "This program will test to to see if possible critical points exist\nfor any of the
following functions using the first derivative test." << endl;
cout << "A critical point is any point on a graph where the slope is equal to zero or
undefined." << endl;
cout << "Quadratic and quartic functions are the most likely to contain critical points\nand
linear functions do not have any critical points." << endl;
cout << "\nFeel free to check any answers provided with a separate graphing utility." << endl;
cout << "\nPlease choose one of the following:" << endl;
cout << "1. Linear\n2. Quadratic\n3. Cubic\n4. Quartic" << endl << endl;
int choice = validateInput(1, 4); // The user has 4 options to choose from
eq.setNumPS(choice);
system("CLS");
return choice;
}
numerator.clear();
denominator.clear();
numPS = fndSol = 0;
}
void Polynomial::setCoe(int c) {
system("CLS");
cout << "Time to create your equation." << endl << endl;
switch (c)
{
case 1:cout << "Linear:\n parent function : f(x) = x\n standard form : f(x) = mx + b"
<< endl; break;
case 2:cout << "Quadratic:\n parent function : f(x) = x^2\n standard form : f(x) = ax^2
+ bx + c" << endl; break;
case 3:cout << "Cubic:\n parent function : f(x) = x^3\n standard form : f(x) = ax^3 +
bx^2 + cx + d" << endl; break;
case 4:cout << "Quartic:\n parent function : f(x) = x^4\n standard form : f(x) = ax^4 +
bx^3 + cx^2 + dx + e" << endl; break;
}
cout << "\nPlease enter " << (numPS + 1) << " integers between " << min << " and " << max <<
"." << endl;
cout << "For the best results, your last value should be greater than the first" << endl <<
endl;
switch (c)
{
case 4: {
cout << "Enter the number of x^4 you would like: ";
int num = validateInput(min, max);
coefficient[1] = num;
}
case 3: {
cout << "Enter the number of x^3 you would like: ";
int num = validateInput(min, max);
coefficient[2] = num;
}
case 2: {
cout << "Enter the number of x^2 you would like: ";
int num = validateInput(min, max);
coefficient[3] = num;
}
case 1: {
cout << "Enter the number of x you would like: ";
int num = validateInput(min, max);
coefficient[4] = num;
}
default: {
cout << "Enter a constant: ";
int num = validateInput(min, max);
coefficient[5] = num;
setDer(); break; // Calculate the derivative once the user has entered all their
values and break
}
}
setNum(derivative[findC(derivative, DS)]);
setDen(derivative[findLC(derivative, DS)]);
}
void Polynomial::setDer() {
for (int i = 1; i < DS; i++)
{
derivative[i] = (exponent[i - 1] * coefficient[i]);
}
}
void Polynomial::setNum(int c) {
for (int i = 1; i <= c; i++) {
if ((abs(c) % i) == 0) {
numerator.push_back(i);
}
}
}
void Polynomial::setDen(int lc) {
for (int i = 1; i <= lc; i++) {
if ((abs(lc) % i) == 0) {
denominator.push_back(i);
}
}
cout << "\nThere are " << 2 * (numerator.size() * denominator.size()) << " possible critical
points. Checking solutions..." << endl;
Solution sol;
sol.displayPCP();
}
void Polynomial::setNumPS(int ns) {
numPS = ns;
}
int Polynomial::getNumPS() {
return numPS;
}
int Polynomial::getFndSol() {
return fndSol;
}
int Polynomial::findC(int list[], int size) {
int index = (size - 1); // Used as a subscript to search array
int position = -1; // Used to record position of search value
// Flag to indicate if the value was found
bool found = false; // Flag to indicate if the value was found
if (sum == 0) {
return true;
}
else {
return false;
}
}
void Solution::setNSol(int n) {
nSol.push_back(n);
}
void Solution::setDSol(int d) {
cout << "; adding argument to the solution set";
dSol.push_back(d);
}
void Solution::displayPCP() {
if (nSol.empty())
cout << "\nThere are not any possible critical points" << endl;
else {
cout << "\nPossible critical points include:" << endl;
if (result == 1)
{
return 1;
}
}
return result;
}