This program takes in coordinates and then outputs the area, but there seems to be a logic error somewhere. The file it is reading from has this in it.
4
1.0 0.0
13.2 1.25
20.5 18.4
16.37 24.54
Code:
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char **argv[])
{
int size = 0;
char fileName[] = "pointData.txt";
double sum=0;
int i;
FILE *instream=NULL;
instream = fopen(fileName, "r");
fscanf(instream,"%d",&size);
double x[size];
double y[size];
for(i=0; i<size; i++){
fscanf(instream,"%lf",&x[i]);
fscanf(instream,"%lf",&y[i]);}
fclose(instream);
for(i=0; i<size-1; i++){
sum+=(((x[i])*(y[i+1]))-((x[i+1])*(y[i])));}
sum/=2.0;
printf("Area of the polygon is: %.4f\n", sum);
return 0;
}
It outputs 210.1835, it should be 197.9135