+ 2

Regarding to Structure in C

Hi viewers, I have a question where in I have to define a function using structure where in am passing array of rectangle and finding the smallest one from it : #include<stdio.h> struct Point{ float x; float y; }; struct Rectangle{ struct Point lower_left; struct Point upper_right; }; float get_area(struct Rectangle r){ float width,height; float area; width = r.upper_right.x - r.lower_left.x; height = r.upper_right.y - r.lower_left.y; area = width * height; return area; } struct rectangle smallest_area(struct Rectangle r[10]){ struct Rectangle min = r[0]; for(int i = 1; i < n; i++){ } }. I was not able to complete the program

22nd Apr 2025, 12:53 AM
Bhuvanesh Vinayak Sirsikar
Bhuvanesh Vinayak  Sirsikar - avatar
5 ответов
+ 8
I think the program is to find the rectangle with the smallest area from an array of rectangles. You have a lot of typo here in your code. It was struct rectangle in your function but should be struct Rectangle. You need to add the size parameter `n` to the function to make it work with any array size. Your loop is not completed as well. Most importantly you don't have any main() function. This thing is very confusing, that you don't include this in C. Don't make unnecessary threads. Be specific in your question. You can debugged this code very easily by your own. I don't know why you create thread for this. Here is the correct code that I write for you. https://sololearn.com/compiler-playground/cOi4o5SE4CS7/?ref=app
22nd Apr 2025, 3:39 AM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar
+ 7
`ᴴᵗᵗየ good solution. I made some modifications to your code. width and height should be absolute values (abs or fabs). negative values does not make sense and confuse the comparison. used typedef to simplify struct declarations. used p1 and p2 instead of upper_left and lower_right, which might not be correct in case of random point assignments. used computed array length, for easier rect array modification... alternatively, instead of returning a Rectangle, you can return the index of the minimum area. Then it's easier to identify which rectangle it is in the array. https://sololearn.com/compiler-playground/cMA5ioqTlltk/?ref=app
22nd Apr 2025, 5:06 AM
Bob_Li
Bob_Li - avatar
+ 6
Bob_Li Thanks a lot brother 🫰 It's welly debugged now 🥰
22nd Apr 2025, 5:07 AM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar
+ 3
Thankyou once again
22nd Apr 2025, 4:49 AM
Bhuvanesh Vinayak Sirsikar
Bhuvanesh Vinayak  Sirsikar - avatar
+ 1
The smallest_area function takes an array of Rectangle structures and its size as input. It initializes the smallest rectangle and its area with the first element. Then, it iterates through the rest of the array, calculating the area of each rectangle https://www-txtag.com and updating the smallest rectangle and its area if a smaller one is found. Finally, it returns the Rectangle structure with the smallest area. The key missing part in the original code was the array size parameter (n) to control the loop.
24th Apr 2025, 8:48 AM
James015Sims
OSZAR »