Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a problem getting all of my arrays to work correctly. I have to write a p

ID: 3623082 • Letter: I

Question

I have a problem getting all of my arrays to work correctly. I have to write a program with three separate functions to be called all inside of main. The first equation reads in a set of numbers, the next calculates the area of the polygon with the points scanned in from the first function, and the last function that ouputs the numbers to a separate file. I can get the numbers to scan in fine, i just can't figure out how to pass them correctly.

Here is the entire program now as i have it.

//This program will find the area of a polygon
#include<math.h>
#include <stdio.h>
#define SIZE 7
int get_corners(void);
int polygon_area(void);
int output_corners(void);
int main(void)
{
printf(" This program will calculate the area of a polygon. ");
get_corners();
polygon_area();
output_corners();

return(0);
}

int get_corners(void)
{
int i=0; //declaring variables
double x[SIZE], y[SIZE];
FILE *inp;

inp = fopen("C:corner.txt","r"); // file pointer for input file

for(i=0; i<SIZE; i++)
{
fscanf(inp, "%lf %lf", &x[i], &y[i]);
}

return (0);
}

int polygon_area(void)
{
int i; //declaring variables
double x[SIZE], y[SIZE], a, sum=0.0, area;

for (i=0; i<SIZE-2; i++)
{
a = (x[i+1] + x[i]) * (y[i+1] - y[i]);
sum = sum + a;
}

area = sum / 2;
printf(" The area of the polygon is %.1lf square units ", fabs(area));

return(0);
}

int output_corners(void)
{
int i; //declaring variables
double x[SIZE], y[SIZE];
FILE *outp;

outp = fopen("C:corneroutput.txt","w"); //output file pointer

for (i=0; i<SIZE; i++)
{
fprintf(outp, "%.1lf %.1lf ", x[i], y[i]);
}

return(0);
}

Explanation / Answer

please rate - thanks

without your data I'm unable to test it

try this

//This program will find the area of a polygon
#include<math.h>
#include <stdio.h>
#define SIZE 7
void get_corners(double[],double[]);
double polygon_area(double[],double[]);
void output_corners(double[],double[],double);
int main(void)
{double x[SIZE], y[SIZE],area;
printf(" This program will calculate the area of a polygon. ");
get_corners(x,y);
area=polygon_area(x,y);
output_corners(x,y,area);

return(0);
}

void get_corners(double x[],double y[])
{
int i=0; //declaring variables

FILE *inp;

inp = fopen("C:\corner.txt","r"); // file pointer for input file
for(i=0; i<SIZE; i++)
{
fscanf(inp, "%lf %lf", &x[i], &y[i]);
}

}

double polygon_area(double x[],double y[])
{
int i; //declaring variables
double a, sum=0.0, area;

for (i=0; i<SIZE-2; i++)
{
a = (x[i+1] + x[i]) * (y[i+1] - y[i]);
sum = sum + a;
}

area = sum / 2;
printf(" The area of the polygon is %.1lf square units ", fabs(area));

return area;
}

void output_corners(double x[],double y[],double area)
{
int i; //declaring variables
FILE *outp;

outp = fopen("C:\corneroutput.txt","w"); //output file pointer
for (i=0; i<SIZE; i++)
{
fprintf(outp, "%.1lf %.1lf ", x[i], y[i]);
}
printf(" The area of the polygon is %.1lf square units ", fabs(area));

}