Concept
How can a PHP program prompt for the coefficients of a quadratic polynomial?
ComputerScienceOne / Computing Quadratic Roots
"Some programs require the user to enter multiple inputs. The prompt-input process can be repeated. In this example, consider asking the user for the coefficients, a, b, c to a quadratic polynomial, ax^2 + bx + c and computing its roots using the quadratic formula, x = (−b ± √(b^2 − 4ac)) / 2a. As before, we can create a basic program with PHP tags and start filling in the details. In particular, we’ll need to prompt for the input a, then read it in; then prompt for b, read it in and repeat for c. Thus, we have:\n\nprintf(\"Please enter a: \");\n\n$a = floatval(trim(fgets(STDIN)));\n\nprintf(\"Please enter b: \");\n\n$b = floatval(trim(fgets(STDIN)));\n\nprintf(\"Please enter c: \");\n\n$c = floatval(trim(fgets(STDIN)));\n\nThis program was interactive. As an alternative, we could have read all three of the inputs as command line arguments, taking care to convert them to floating point numbers. Lines 8–13 in the program could have been changed to:\n\n$a = floatval($argv[1]);\n\n$b = floatval($argv[2]);\n\n$c = floatval($argv[3]);"
Related Ideas
- How can a C program compute the roots of a quadratic equation?ComputerScienceOne · Computing Quadratic Roots
- How can PHP pass variables by reference to return multiple quadratic roots?ComputerScienceOne · Quadratic Roots
- How can quadratic-equation inputs be provided as command-line arguments?ComputerScienceOne · Computing Quadratic Roots
- What kinds of user input can cause problems in a quadratic-roots program?ComputerScienceOne · Computing Quadratic Roots
- What section is listed as 2.7?ComputerScienceOne · Quadratic Roots