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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
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
Transcribed Image Text: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
Expert Solution
Step 1

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.
Step 2

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);

}

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY