help finding area and perimeter of given coordinates

below is what i have so far. My main issue is outputting the area and perimeter of the given coordinates as i need the final coordinate to connect up to the first coordinate. I've tried doing that using numberOfcorners but it says "the expression must have integral or enum type"


#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>
#include <math.h>
using namespace std;

int main() {

float ArrayX[10];
float ArrayY[10];
float numberOfcorners;
float distanceCorners[10];
float perimeter = 0;
float area = 0;

//Ask user to enter number of coordinates
cout << "Please enter number of corners: " << endl;
cin >> numberOfcorners;

while (numberOfcorners < 3 || numberOfcorners>10) {
cout << "Please input a value between 3 and 10" << endl;
cin >> numberOfcorners;
}
//Ask user to enter X coordinates
for (int i = 0; i < numberOfcorners; i++){
cout << "Please enter X coordinate " << i + 1 << endl;
cin >> ArrayX[i];
//Ask user to enter Y coordinates
cout << "Please enter Y coordinate " << i + 1 << endl;
cin >> ArrayY[i];

//Check that coordinates are positive
if (ArrayX[i] < 0 || ArrayY[i] < 0){
cout << "coordinates cannot be negative" << endl;
i--;
}
else
cout << "Coordinate " << i + 1 << " is: " << "(" << ArrayX[i] << "," << ArrayY[i] << ")" << endl;
}

// find distance between each coordinate
for (int i = 0; i < numberOfcorners; i++){

//Distance between first and last coordinate
distanceCorners[i] = sqrt(powf((ArrayX[i + 1] - ArrayX[i]), 2) + powf((ArrayY[i + 1] - ArrayY[i]), 2));

perimeter = perimeter + distanceCorners[i];

area = (numberOfcorners*powf(distanceCorners[i], 2)) / 4 * (tan(M_PI / numberOfcorners));

cout << "The distance between coordinate " << "(" << ArrayX[i] << "," << ArrayY[i] << ")" << " and " << "(" << ArrayX[i + 1] << "," << ArrayY[i + 1] << ")" << " is " << distanceCorners[i] << " m" << endl;

distanceCorners[i] = sqrt(powf((ArrayX[numberOfcorners] - ArrayX[0]), 2) + powf((ArrayY[numberOfcorners - 1] - ArrayY[0]), 2));
}
//distance of connecting first and last coordinate
perimeter = perimeter + distanceCorners[i];


cout << "The total perimeter is: " << perimeter << " m" << endl;
cout << "The total area is: " << area << " m" << endl;


return 0;

}
Last edited on
numberOfcorners is defined as a float - if you want to use it as an array subscript it would need to be an integer.

Also - this line is outside the for loop so i is out of scope.
perimeter = perimeter + distanceCorners[i];
cheers, also how do i add the distance of the last coordinate to first coordinate to the perimeter?
If they have the max number of corners (10), the code is going to go out bounds on the array on the last loop.

1
2
for (int i = 0; i < numberOfcorners; i++)
distanceCorners[i] = sqrt(powf((ArrayX[i + 1] - ArrayX[i]), 2) + powf((ArrayY[i + 1] - ArrayY[i]), 2));


//just cut out some of the code here just to simplify for illustration purposes
distanceCorners[0] = (ArrayX[1] - ArrayX[0])(ArrayY[1] - ArrayY[0])
distanceCorners[1] = (ArrayX[2] - ArrayX[1])(ArrayY[2] - ArrayY[1])
distanceCorners[2] = (ArrayX[3] - ArrayX[2])(ArrayY[3] - ArrayY[2])
distanceCorners[3] = (ArrayX[4] - ArrayX[3])(ArrayY[4] - ArrayY[3])
distanceCorners[4] = (ArrayX[5] - ArrayX[4])(ArrayY[5] - ArrayY[4])
distanceCorners[5] = (ArrayX[6] - ArrayX[5])(ArrayY[6] - ArrayY[5])
distanceCorners[6] = (ArrayX[7] - ArrayX[6])(ArrayY[7] - ArrayY[6])
distanceCorners[7] = (ArrayX[8] - ArrayX[7])(ArrayY[8] - ArrayY[7])
distanceCorners[8] = (ArrayX[9] - ArrayX[8])(ArrayY[9] - ArrayY[8])
distanceCorners[9] = (ArrayX[10] - ArrayX[9])(ArrayY[10] - ArrayY[9])


In any case, that last iteration should be the distance between the last point and the first point?
distanceCorners[9] = (ArrayX[9] - ArrayX[0])(ArrayY[9] - ArrayY[0]
or
distanceCorners[numberOfcorners-1] = (ArrayX[numberOfcorners-1] - ArrayX[0])(ArrayY[numberOfcorners-1] - ArrayY[0]


It's sort of a special case. You could check the value of the index in the for loop to check for this case, or pull it out of the loop and run the loop one fewer times. You just can't use the loop variable (e.g. i) outside of the loop.
Topic archived. No new replies allowed.