Bifalgorithm post · Computer science one · Computing Quadratic Roots

How can a PHP program prompt for the coefficients of a quadratic polynomial?

How can a PHP program prompt for the coefficients of a quadratic polynomial? (Computer science one · Computing Quadratic Roots) — "Some programs require the us…

Ideas in this flowchart

  • Programs can collect multiple user inputs to perform a computation.
  • The prompt-input process can be repeated for each required value.
  • Example: collect the coefficients a, b, and c of the quadratic polynomial ax^2 + bx + c.
  • The program computes the roots using x = (−b ± √(b^2 − 4ac)) / 2a.
  • A basic PHP program can be created with PHP tags and filled in with the input logic.
  • The three inputs can be collected interactively or provided as command-line arguments.
  • Interactive input uses prompts and reads values from standard input.
  • Prompt for a and read it as a floating-point value.
  • Prompt for b and read it as a floating-point value.
  • Prompt for c and read it as a floating-point value.
  • As an alternative, read all three inputs from command-line arguments.
  • Convert the command-line arguments to floating-point numbers.
Feed/

How can a PHP program prompt for the coefficients of a quadratic polynomial?

No cover

Channel

Computer science one

1 page

"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]);"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.