Homework6-3

Homework6-3 - output_corners(fpout x y n/calls output...

This preview shows pages 1–3. Sign up to view the full content.

Homework #6 /* * Program HW6_3 * Written by Justin Waters * Inputs: values from a seperate file * Outputs:coordinates and area of a polygon to an output file * Description: The program takes values from a source file and uses them to calculate the area of a polygon * */ #include <stdio.h> #include <math.h> #include <stdlib.h> #define max_size 20 int get_corners(FILE *fpin, double x[], double y[]); void output_corners(FILE *fpout, double x[], double y[], int n); double polygon_area(double x[], double y[], int n); int main (void) { FILE *fpin; //opens files FILE *fpout; int n; double x[max_size], y[max_size], area; printf("\nWelcome to my polygon program\n\n"); fpin = fopen("in.txt","r"); fpout = fopen("out.txt","w"); if (fpin==NULL) //checks for opened files { printf("File does not exist\n"); exit(1); } n = get_corners(fpin, x, y); //calls get corners function

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: output_corners(fpout, x, y, n); //calls output corners function area = polygon_area(x, y, n); //calls area solving function printf("\nThe area is %.3lf", area); fclose(fpin); //closes files fclose(fpout); return (0); } int get_corners(FILE *fpin, double x, double y) { int p=0; while (fscanf(fpin, "%lf %lf", &x[p], &y[p])!= EOF) { p++; } return (p-1); } void output_corners(FILE *fpout, double x, double y, int n) { int i; fprintf(fpout, "\n x y"); fprintf(fpout, "\n ______________\n"); for(i=0;i<=n;i++) fprintf(fpout, "\n %5.2lf %5.2lf", x[i], y[i]); } double polygon_area(double x, double y, int n) { int t, z=0, temp, i; for(i=0;i<n-2; i++) { temp = (x[i+1] + x[i]) * (y[i+1] - y[i]); z = z + temp; } t = 0.5 * (abs(z)); return (t); }...
View Full Document

{[ snackBarMessage ]}

Page1 / 3

Homework6-3 - output_corners(fpout x y n/calls output...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online