Lab 5 Ex2: Quadratic Functions in Standard Form
You cannot submit for this problem because the homework's deadline is due.
Ex2. Quadratic Functions in Standard Form
When Li is programming, his little sister comes, asking him about quadratic functions. While Li is a top coder, his knowledge in that part of mathematics is close to zero, so he turns to you for help. As a student taking VE280, you can use your knowledge about abstract data types (ADT) to help Li and his sister play with quadratic functions.
Related Topics: ADT.
Problem: Li wants to represent a quadratic function in a standard form, which is \(f(x) = ax^2+bx+c\) (\(a\neq 0\)). He decides that the following operations should be allowed on quadratic functions:
Evaluate \(f(x)\) at a given int \(x\) value.
Get the root(s) of \(f(x)\), which is the value of \(x\) such that \(f(x)=0\)
Check if two quadratic functions (\(f\) and \(g\)) intersects, which means whether there exists some real \(x\) such that \(f(x)=g(x)\).
Therefore, he designed this interface to represent a quadratic function
class QuadraticFunction {
// OVERVIEW: the standard form of a quadratic function f(x) = ax^2 + bx + c
float a;
float b;
float c;
public:
QuadraticFunction(float a_in, float b_in, float c_in);
// REQUIRES: a_in is nonezero
// EFFECTS: creates a quadratic function in standard form
float getA() const;
// EFFECTS: returns the value of a
float getB() const;
// EFFECTS: returns the value of b
float getC() const;
// EFFECTS: returns the value of c
float evaluate(float x);
// EFFECTS: returns the value of f(x)
Root getRoot();
// EFFECTS: returns the roots of the quadratic function
bool intersect(QuadraticFunction q);
// EFFECTS: returns whether g and this intersect
};
Here, the constructor takes 3 inputs a_in
, b_in
and c_in
and uses them to represent the quadratic function \(f(x)=ax^2+bx+c\). Also, the output function for this exercise is provided.
Requirements:
- Look through file
rootType.h
, to make the output simple, we make the following restrictions:- if \(f(x)\) has two different real roots, then the smaller \(x_1\) should be in
roots[0]
and the bigger \(x_2\) should be inroots[1]
. - If \(f(x)\) has one real root, then \(x_1=x_2\) should be in both
roots[0]
androots[1]
. - If \(f(x)\) has two complex roots, then \(x_1=m-ni\) should be in
root[0]
and \(x_2 = m+ni\) should be inroots[1]
, where \(n>0\).
- if \(f(x)\) has two different real roots, then the smaller \(x_1\) should be in
- Look through
standardForm.h
and implement the methods for QuadraticFunction class instandardForm.cpp
. -
ex2.cpp
is used to test your ADT, you can just read it and run it.
Input Format: Since you only need to implement the methods of this ADT, we just provide a sample input. And there will not be cases where \(a=0\).
1 -3 2
1
2 -4 2
Output Format: Since you only need to implement the methods of this ADT, we just provide a sample output. NOTE that although in some cases it may be weird to have x1 = 1.0 + -1.0i
, just ignore it.
f(x)=1.0x^2+-3.0x+2.0
f(1.0)=0.0
f(x) has 2 real roots.
x1 = 1.0 + 0.0i
x2 = 2.0 + 0.0i
1
Hint:
\(\Delta = b^2 - 4ac\), if \(\Delta \geq 0\), \(x=\frac{-b\pm \sqrt{\Delta}}{2a}\). Else, \(x=\frac{-b\pm i\sqrt{-\Delta}}{2a}\)
\(a\) for \(g(x)\) can be the same as \(a\) for \(f(x)\).
Lab Five
- Status
- Finished
- Problems
- 3
- Open Since
- 2022-06-14 00:00
- DDL
- 2022-06-21 23:59
- Extension
- 72.0 hour(s)