Macro Pitfalls
Points 100 1.0s 256MMany people learning the C++ #define macro misuse it by treating #define F(X) as a function.
Consider the following problem:
You are given two points \(A\) and \(B\) in the 2D plane. \(A\) has coordinates \((A_x,A_y)\), and \(B\) has coordinates \((B_x, B_y)\). Compute the square of the Euclidean distance between \(A\) and \(B\).
We know the distance is \(\sqrt{(A_x - B_x)^2 + (A_y - B_y)^2}\), so its square is \((A_x - B_x)^2 + (A_y - B_y)^2\). The squaring appears twice. Some programmers choose to denote squaring with a macro, writing code as follows:
#include<iostream>
using namespace std;
#define SQR(X) X * X
int main() {
int A_x, A_y, B_x, B_y;
cin >> A_x >> A_y >> B_x >> B_y;
cout << SQR(A_x - B_x) + SQR(A_y - B_y) << endl;
return 0;
}
However, this produces a wrong answer. For example, when \(A=(2, 1)\) and \(B=(-2, -2)\), the program outputs \(13\), but the correct value is \((2-(-2))^2 + (1 - (-2))^2 = 25\).
This happens because the compiler replaces \(SQR(A\_x - B\_x) + SQR(A\_y - B\_y)\) with \(A\_x - B\_x * A\_x - B\_x + A\_y - B\_y * A\_y - B\_y\). The correct macro should be
#define SQR(X) ((X) * (X))
In this problem, you will reproduce the behavior of both the incorrect and the correct macro. Given the coordinates of points \(A\) and \(B\), output the value produced by the incorrect code above, followed by the correct value.
Input
The input contains a single line with four integers \(A_x, A_y, B_x, B_y\). The constraints are \(-10,000 \le A_x, A_y, B_x, B_y \le 10,000\).
Output
Output one line with two integers: the first is the result printed by the incorrect code in the statement, and the second is the correct value.
Scoring
This problem is worth \(20\) points.
Sample Input 1
2 1 -2 -2
Sample Output 1
13 25
Log in to write and submit code.
Log in