### How to convert algebraic equations into C++ In one C++ Program translate the following algebraic equations into C++ code. Then run the program with the given values stated below for x and y. Make sure that your results match the output as shown below. The values for x and y should be entered by the user using cin for input.

Equation One:

(Enter the value of 3.73 for x)

R = 3x^2 + 5x + 1

Equation Two:

(Enter the value of 9.385 for x)

S = (x2 - 12x + 27)/(x – 6)^2

 ``1234567891011121314151617181920212223`` ``````#include #include #include #include using namespace std; int main(void) { double x = 0.0; double R; cout << fixed << setprecision(2) << endl; cout << "Enter value for x: " << endl; cin >> x; R = 3 * x * x + 5 * x + 1; cout << endl; return 0; }``````

I have not started the second question but this is what I have for the first Homework question. When I run the program, it asks me for an input and when I type the input in, nothing happens. I am new to C++ coming from python. Try outputting R on line 19 instead of just a line feed. Change line 19 to something like: `cout << R << endl;` Hello gigacapybara,

Have a look at this for what you need:
 ``123456789101112131415161718192021222324`` ``````#include #include #include //#include // <--- Not used in what you have using namespace std; // <--- Best not to use. int main(void) { double x{}; // <--- ALWAYS initialize all your variables. double R{}; cout << fixed << setprecision(4) << endl; cout << "Enter value for x: " << endl; cin >> x; R = 3 * x * x + 5 * x + 1; cout << R <<'\n'; return 0; // <--- Not required, but makes a good break point for testing. }``````

Running this gives an output of `61.3887`. Is that what you expect? Cut to 2 decimals it is: `61.39`.

Andy what does x2 mean?
x^2 probably..?
if you simplify the second one first its a little shorter.
t = x*x-12*x;
result is (t +27)/t+36);
the smallest format for math is not usually the best for c++, sometimes expanding it out makes it easier to translate.
Last edited on Yes sorry about that typo, its x^2. Just a few small changes required to move forward:
 ``1234567891011121314151617181920212223`` ``````#include //#include #include //#include using namespace std; int main(void) { double x = 0.0; double R; cout << "Enter value for x: " << endl; cin >> x; R = 3 * x * x + 5 * x + 1; cout << "R = " << fixed << setprecision(2) << R << endl; cout << endl; return 0; }``````

 ``` Enter value for x: 3.73 R = 61.39 Program ended with exit code: 0 ``` And continuing on with the second equation. There are lots of opportunities that arise to tidy it up. Even check to make sure x is not 6.

 ``12345678910111213141516171819202122232425262728293031`` ``````#include //#include #include //#include using namespace std; int main(void) { double x = 0.0; double R; cout << "Enter value for x: " << endl; cin >> x; R = 3 * x * x + 5 * x + 1; cout << "R = " << fixed << setprecision(2) << R << endl; cout << endl; // S = (x2 - 12x + 27)/(x – 6)^2 cout << "Enter value for x: " << endl; cin >> x; double S = (x * x - 12 * x + 27)/((x - 6)*(x - 6)); cout << "S = " << fixed << setprecision(2) << S << endl; cout << endl; return 0; }``````

 ``` Enter value for x: 3.73 R = 61.39 Enter value for x: 9.385 S = 0.21 Program ended with exit code: 0 ```
Topic archived. No new replies allowed.