How do I input a quadratic equation in C++?

We were given an assignment to program the following, which I have been trying to figure out how to solve for the last 2 hours but to no avail.

How do you actually solve a complex formula having different operations in one mathematical expression?

For you to properly understand which of the operations are to be solved in order, try recreating the quadratic equation, ax^2 + bx + c, in C++ on your own!

Instructions:

  1. The value of a, b, c, and x are already provided for you in the code editor. Remake the formula using C++'s math functions and operators and store it into one variable.
  1. Print the value of the variable that stores the formula. To understand which to actually solve first in the equation, try tracing it out by yourself and manually solve it and see if your answer match that of the sample output.

Sample output: 16

TL;DR I am told to recreate the quadratic equation on my own using the given variables with their values.

Here is my current output which failed:

#include<iostream>
#include <cmath>

int main(void) {
    
    int a = 2;
    int b = 2;
    int c = 4;
    int x = 2;

    // TODO:
    // 1. Compute for the result of the quadratic equation
    //    using the variables provided above and the math library
    
    double result;
    result = (a * x + b * x) pow(2, 2) + c;

    // 2. Print the output required by the output sample
    std::cout << result;

    return 0;
}

It prints out 4 when it should be 16.

1 answer

  • answered 2021-09-23 11:49 Truth

    You use this code:

    result = a * pow(x, 2) + b * x + c;
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum