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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>
#include <string>

//#include <fstream>  // <--- 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
//#include <fstream>
#include <iomanip>
//#include <string>

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
//#include <fstream>
#include <iomanip>
//#include <string>

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.