Task 2: Point on Parabola?
Task
Consider a parabola \((\m{P})\) defined as \(y = g(x)\), with \(g(x) = 0.9 \cdot x^2 + 1.3 \cdot x - 0.7\).
Write a program that determines if a point \((x,y)\) lies on parabola \((\m{P})\) or not. The input is provided by two decimal numbers in the sequence \(x,y\). The program must output `yes`, if the point lies on the parabola, otherwise `no`. Use datatype `double` for all variables and numbers used in the calculation of \(g(x)\).
You will notice that a straight forward approach (comparing for equality) does not work, i.e., for some points that clearly should be on parabola $g$ such an approach returns result `no`.
Hint: Look at the difference between the exact values of the function and the values that your program calculates. Change the program so that it works properly for all points the submission system uses as test input without hard-coding the points. Expect an epsilon within the range \([ 10^{-6}, 10^{-3}]\)$. Experiment yourself to find the epsilon required to pass the test cases.
Note: Output only with `std::cout << "no" << std::endl;` or `std::cout << "yes" << std::endl;`, as the autograder will only accept output that exactly matches `yes\n` or `no\n`. For all other messages, use `std::cerr` as in:
std::cerr << "This is a test message\n"
Those will be ignored by the autograder.
Solution
#include <iostream>
int main() {
double x = 0; // x inputed by user
double yin = 0; // y inputed by user
double ycon = 0; // y control
double emax = 0.0001;
double emin = -0.0001;
std::cin >> x;
std::cin >> yin;
ycon = 0.9 * x * x + 1.3 * x - 0.7;
std::cerr << ycon;
std::cerr << yin - ycon;
if (yin == ycon || (yin - ycon <= emax && yin - ycon >= emin))
std::cout << "yes";
else
std::cout << "no";
return 0;
}
Made by JirR02 in Switzerland 🇨🇭