Write a program that prompts the user to enter the points of a convex polygon an
ID: 3548627 • Letter: W
Question
Write a program that prompts the user to enter the points of a convex polygon and display its area. ASuume that the polygon has six end points and the points are entered clockwise. For the definition of a convex polygon, see www.mathopenref.com/polygonconvex.html. Hint: the total area of a polygon is the sum of the areas of the small triangles.
Write a function called areaPolygon that solves the problem.
(2) Write a main function that:
? enters the coordinates of the points of a polygon;
? calls the areaPolygon function;
? outputs the area of the polygon.
Important note: No global variables and arrays should be declared.
(3) Test your program with appropriate input data (at least three set of input data)
Need the code in c++
Explanation / Answer
#include <iostream>
using namespace std;
double areaPolygon(int x[6], int y[6])
{
double area;
int sum1 = 0;
int sum2 = 0;
for(int i=5; i>=0; i--)
{
if(i == 0)
{
sum1 = sum1 + x[i]*y[5];
cout<<"sum1: "<<sum1<<endl;
sum2 = sum2 + y[i]*x[5];
cout<<"sum2: "<<sum2<<endl;
}
else
{
sum1 = sum1 + x[i]*y[i-1];
cout<<"sum1: "<<sum1<<endl;
sum2 = sum2 + y[i]*x[i-1];
cout<<"sum2: "<<sum2<<endl;
}
}
area = 0.5 * (sum1 - sum2);
return area;
}
int main()
{
int xCoord[6];
int yCoord[6];
double polygonArea;
cout << "Enter 6 points of convex Polygon clockwise" << endl;
for(int i =0 ;i<6; i++)
{
cout<<"x co-ordinates: ";
cin>>xCoord[i];
cout<<"y co-ordinates: ";
cin>>yCoord[i];
}
polygonArea =areaPolygon(xCoord,yCoord);
cout<<"Area of Polygon: "<<polygonArea;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.