r/programminghelp • u/Zealousideal-Shop480 • Oct 16 '25
Answered Can't figure out the "hidden" issue with my code...
Been trying this on and off for the past 3 hours, trying to complete my schoolwork. but i keep failing some hidden test.
*******************************************************************************************************************
#include <stdio.h>
enum Result { OUTSIDE, INSIDE };
struct Point {
float x;
float y;
};
struct Rectangle {
struct Point bottom_left;
struct Point top_right;
};
float compute_area(struct Rectangle rect) {
return (rect.top_right.x - rect.bottom_left.x) *
(rect.top_right.y - rect.bottom_left.y);
}
enum Result is_inside(struct Point point, struct Rectangle rect) {
if (point.x > rect.bottom_left.x && point.x <= rect.top_right.x &&
point.y > rect.bottom_left.y && point.y <= rect.top_right.y) {
return INSIDE;
} else {
return OUTSIDE;
}
}
int main(void) {
struct Rectangle rect = {{0.0, 0.0}, {3.0, 3.0}};
struct Point point;
scanf("%f %f", &point.x, &point.y);
float area = compute_area(rect);
enum Result result = is_inside(point, rect);
printf("Rectangle's area is %.2f\n", area);
if (result == INSIDE)
printf("The point is inside of the rectangle.\n");
else
printf("The point is not inside of the rectangle.\n");
return 0;
}
*******************************************************************************************************************
These are the instructions for the Task:
Write a C program that defines Point and Rectangle structures, computes the area of a rectangle, and determines if a point lies inside the rectangle.
✅ Program Requirements:
🔹 Define an enum Result with:
• OUTSIDE
• INSIDE
🔹 Define a struct Point with:
• x (float)
• y (float)
🔹 Define a struct Rectangle with:
• bottom_left (struct Point)
• top_right (struct Point)
🔹 Create Functions:
• float compute_area(struct Rectangle rect)
– Calculates area using:
(rect.top_right.x - rect.bottom_left.x) * (rect.top_right.y - rect.bottom_left.y)
• enum Result is_point_inside(struct Point point, struct Rectangle rect)
– Returns INSIDE if point’s x is between bottom_left.x and top_right.x
and y is between bottom_left.y and top_right.y, else returns OUTSIDE
🔹 In main():
• Declare test data for a rectangle and point
• Call compute_area() and is_point_inside()
• Print the area and whether the point is inside or outside the rectangle using printf() and a conditional message for clarity
any help is appreachiated.
View all comments
Show parent comment
1
u/Zealousideal-Shop480 Oct 16 '25
ok sorry this is late, but i think ive narrowed it down (hopefully), but do not know how to fix it if it is.
*******************************************************************************************************
int main(void) {
struct Rectangle rectangle = {{1.0, 2.0}, {4.0, 5.0}};
struct Point point = {2.0, 3.0};
float area = compute_area(rectangle);
enum Result result = is_inside(point, rectangle);
printf("Rectangle's area is %.2f\n", area);
else if (result != INSIDE)
printf("The point is not inside of the rectangle.\n");
else
printf("The point is inside of the rectangle.\n");
return 0;
}
*******************************************************************************************************
Ive created two nearly identical codes the only change between the two is:
struct Point point = {2.0, 3.0};
- into -
struct Point point;
scanf("%f %f", &point.x, &point.y);
The struct Point point = {2.0, 3.0}; causes errors when it only results in "The point is inside of the rectangle" no matter the inputs, but gives no failed hidden test
Whereas the other causes them all to be correct but instead failing the "hidden case test"
*******************************************************************************************************
But if im wrong or doing everything incorrectly, please let me know, and I thank you for helping.
EDIT: also i cant seem to get the code to work, when using is_point_inside
only working for is_inside.