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


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









