If n points are connected to fom a closed polygon as shown below, the area of the polygon can be compuled as n-2 Area = (%)E (*»1 + x ) (y»1 - y ) =0 Notice that although the ilustrated polygon has only 6 distinct comers, n for his polygon is 7 because the algorithmexpects that the last point (x.ya) will be repeat of the initial point, (Ko.yo). Define a structure for a point. Each point contains x coordinate and y coordinate. The represe ntation of a Polygon must be an array of structures in your program. Write a C program that takes the number of actual points (n-1) from the user. After that, user enters x and y coordinates of each point. (The last point will be repeat of the initial point). Writo a compute Are a function which returns the area of the Polygon. Print he area of the Polygon in main. Display the area with wo digts after the decimal point. Note: The absolute value can be computed with fabs function. Example: double x.50: fabs(x) is 5.0 double x 0.0: fabs(x) is 0.0 double x 5.0 fabs(x) is 5.0 Sample Input Sample Output 16.00
![If n points are connected to fom a closed polygon as shown below, the area of the polygon
can be compuled as
n-2
Area = (%)E (*»1 + x ) (y»1 - y )
=0
Notice that although the ilustrated polygon has only 6 distinct comers, n for his polygon is 7
because the algorithmexpects that the last point (x.ya) will be repeat of the initial point, (Ko.yo).
Define a structure for a point. Each point contains x coordinate and y coordinate. The
represe ntation of a Polygon must be an array of structures in your program.
Write a C program that takes the number of actual points (n-1) from the user. After that, user
enters x and y coordinates of each point. (The last point will be repeat of the initial point). Writo
a compute Are a function which returns the area of the Polygon. Print he area of the Polygon
in main. Display the area with wo digts after the decimal point.
Note: The absolute value can be computed with fabs function.
Example:
double x.50: fabs(x) is 5.0
double x 0.0: fabs(x) is 0.0
double x 5.0
fabs(x) is 5.0
Sample Input
Sample Output
16.00](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F6905f6b3-d5c8-48b6-8948-6ff11d89a52c%2F9272c146-7e56-4d62-bdc3-0757e065e60e%2F8h0486h.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
The algorithm to develop the C program for computing area of the polygon with n sides is as follows:
- Include header files stdio.h and stdlib.h.
- Define the struct point with two double variables x and y. It represents the coordinates of the vertex of the polygon.
- In the main function, input the number of sides from the user. Create an array of point structure of the size n+1. n is the number of sides of the polygon.
- Using a for loop, input the coordinates of the polygon from the user in the point array.
- Using another for loop, calculate the area of the polygon using the given formula:
|1/2 [ (x1y2 + x2y3 + ... + xi-1yi + xiy1) - (x2y1 + x3y2 + ... + xiyi-1 + x1yi) ] |
- Use the abs() function on the result and save it in a double variable area.
- Use .2f format specifier in the printf function to display points of the area.
The C program:
#include <stdio.h>
#include <stdlib.h>
//structure point
//x and y coordinates are the data members
struct point{
//data members
double x;
double y;
};
//main function
int main()
{
//number of sides in polygon
int n;
//input the number of sides
scanf("%d",&n);
//create an array of struct point of size n+1
struct point p[n+1];
//loop variable
int i;
//input the coordinates of the polygon
for(i=0;i<=n;i++){
scanf("%lf",&p[i].x);
scanf("%lf",&p[i].y);
}
double sum=0;
//computing the area using the given formula
for(i=0;i<n;i++){
sum+= (p[i+1].x + p[i].x) * (p[i+1].y-p[i].y);
}
//total area
double area = abs((1.0/2.0)*sum);
//print the area
printf("%.2f",area);
}
Step by step
Solved in 3 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)