Question: I have this assignment for C++ and the goal is to create a program that solves quadratic equations in vertex form, that is, a (x

I have this assignment for C++ and the goal is to create a program that solves quadratic equations in vertex form, that is, a (x + h)2 + k where a, h, and k are three known values. In order to find the two possible values of x that solve the equation we are instructed to use the square root principle: x = -h k/a .

The values corresponding to a, h, and k must be whole numbers but the value of x must be a double precision real number with two decimal digit.

I have done all of the required steps, according to the comments my professor gave me but when I test out the numbers we are required to test out: a: 4, h: 2, k: 3

it gives me x1: -2.00 and x2: -2.00

when I'm supposed to get

x1: -1.13 and x2: -2.87

Can someone please help me figure out what I'm missing in my code or doing wrong? I provided the code I have so far below:

#include // to be able to use std I/O (cin, cout) #include // to be able to use operator typeid #include // Include here all the other libraries that required for the program to compile #include #include

using namespace std;

// Ignore this; it's a little function used for making tests inline void _test(const char* expression, const char* file, int line) { cerr << "test(" << expression << ") failed in file " << file; cerr << ", line " << line << "." << endl << endl; } // This goes along with the above function...don't worry about it #define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__))

int main( ) { string name; // Declares variable Name that holds text

int a, h, k; // Declares variables a, h, k that hold whole numbers

double x1, x2; // Declares variables x1 and x2 that hold double precision real numbers

cout << "Please enter your name: "; // Prompts the user to "Please enter your name: "

cin >> name; // Reads the name from keyboard and stores it in the corresponding variable

cout << endl << endl << "Please enter the known terms for the quadratic equation: " << endl; // Displays title "Please enter the known terms for the quadratic equation:"

cout << endl << "a: "; // Prompts the user to enter a

cin >> a; // Reads the value from the keyboard and stores it in the corresponding variable

cout << "h: "; // Prompts the user to enter h

cin >> h; // Reads the value from the keyboard and stores it in the corresponding variable

cout << "k: "; // Prompts the user to enter k

cin >> k; // Reads the value from the keyboard and stores it in the corresponding variable

cout << endl << endl << "Thanks " << name << endl << endl << endl; // Displays "Thanks ", name

x1 = -h + sqrt(k/a); // Calculates x1 using the formula -h + square root(k/a)

x1 = floor((x1 * 10.0) + .05) / 10.0; // Rounds x1 to the second decimal digit and reassigns it to x1

cout << fixed << setprecision(2);

x2 = -h - sqrt(k/a); // Calculates x2 using the formula -h - square root(k/a)

x2 = floor((x2 * 10.0) + .05) / 10.0; // Rounds x2 to the second decimal digit and reassigns it to x2

cout << fixed << setprecision(2);

// Formats the output to display the solutions in fixed format with two decimal digits

// Prints a message like the one below: // "The solutions for the equation are:" // "x1: ", x1 // "x2: ", x2 cout << "The solutions for the equation are:" << endl << endl; cout << setw(37) << "x1: " << setprecision(2) << floor((x1 * 10.0) + .05) / 10.0 << endl; cout << setw(37) << endl << "x2: " << setprecision(2) << floor((x2 * 10.0) + .05) / 10.0 << endl << endl;

system("pause");

// Do NOT remove or modify the following statements cout << endl << "Testing your solution" << endl << endl; test(typeid(name) == typeid(string)); // Incorrect data type used for name test(typeid(x1) == typeid(double)); // Incorrect data type used for x1 test(typeid(x2) == typeid(double)); // Incorrect data type used for x2 test(typeid(a) == typeid(int)); // Incorrect data type used for a test(typeid(k) == typeid(int)); // Incorrect data type used for k test(typeid(h) == typeid(int)); // Incorrect data type used for h if (a == 4 && h == 2 && k == 3) // Incorrect value of x1 (check expressions for calculating x1 and for rounding x1) test(fabs(fabs(x1) - 1.13) < 0.001); if (a == 4 && h == 2 && k == 3) // Incorrect value of x2 (check expressions for calculating x2 and for rounding x2) test(fabs(fabs(x2) - 2.87) < 0.001);

system("pause"); return 0 ; // Successful completion }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!