Homework6-3 - output_corners(fpout, x, y, n); //calls...

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3
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(&quot;\nThe area is %.3lf&quot;, area); fclose(fpin); //closes files fclose(fpout); return (0); } int get_corners(FILE *fpin, double x, double y) { int p=0; while (fscanf(fpin, &quot;%lf %lf&quot;, &amp;x[p], &amp;y[p])!= EOF) { p++; } return (p-1); } void output_corners(FILE *fpout, double x, double y, int n) { int i; fprintf(fpout, &quot;\n x y&quot;); fprintf(fpout, &quot;\n ______________\n&quot;); for(i=0;i&lt;=n;i++) fprintf(fpout, &quot;\n %5.2lf %5.2lf&quot;, x[i], y[i]); } double polygon_area(double x, double y, int n) { int t, z=0, temp, i; for(i=0;i&lt;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

Page1 / 3

Homework6-3 - output_corners(fpout, x, y, n); //calls...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online