Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/var-expr/quadratic by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
Given integer inputs
<code>a</code>, <code>b</code> and <code>c</code>, print out the 
roots of <code>ax<sup>2</sup> + bx + c = 0</code>. They are given by
the formula:
$$\Large \frac{-b \pm \sqrt{b^2-4ac}}{2a}$$
A starter solution is given, but it is buggy! Fix it.
<p>Note that we won't be able to detect imaginary roots until
next week when we have <tt>if</tt> statements.
Public permissions remove
Engine
Template / Reference solution
 
\hide[
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wfloat-conversion"
]\
   
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
   int a, b, c;
   cout << "Enter the integer coefficients separated by spaces: " << endl;
   cin >> a >> b >> c;
\[
   int det = b*b - 4*a*c;
   double r1 = (-b + sqrt(det)) / (2 * a);
   double r2 = (-b - sqrt(det)) / (2 * a);
\show:
   int det = b*b - 4*a*c; // GOOD IDEA: reuse complex subexpression as variable
   int r1 = -b + sqrt(det) / 2 * a;
   int r2 = -b - sqrt(det) / 2 * a;
]\
   cout << "The roots are: " << r1 << " and " << r2 << endl;
   return 0;
}
\hide[
#pragma clang diagnostic pop
]\
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[
   {"stdin": "1 -6 8"},
   {"stdin": "2 8 8"},
   {"stdin": "1 -1 -1"}
]
Solution visibility remove
Remove default compiler flag(s)
json list of flags
see default_cppflags
e.g. ["-Wno-write-strings"]
 
["-Wfloat-conversion"]
remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.