Report the quadrant of a point, or whether it lies on an axis or at the origin.
Solution
main.c
#include <stdio.h>
const char *where(int x, int y) {
if (x == 0 && y == 0) return "origin";
if (x == 0) return "on the y-axis";
if (y == 0) return "on the x-axis";
if (x > 0) return y > 0 ? "quadrant I" : "quadrant IV";
return y > 0 ? "quadrant II" : "quadrant III";
}
int main(void) {
int p[][2] = {{3, 4}, {-2, 5}, {-1, -1}, {4, -7}, {0, 3}, {5, 0}, {0, 0}};
for (int i = 0; i < 7; i++)
printf("(%d, %d): %s\n", p[i][0], p[i][1], where(p[i][0], p[i][1]));
return 0;
}
Output Verified by compiling and running
(3, 4): quadrant I
(-2, 5): quadrant II
(-1, -1): quadrant III
(4, -7): quadrant IV
(0, 3): on the y-axis
(5, 0): on the x-axis
(0, 0): origin
The special cases (origin, axes) are handled first, so the remaining tests only need the signs. The most specific case – both zero – must come before the single-axis checks.
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!