Trouble printing out values from an array in C -
i'm writing program calculate area , perimeter of polygon when given set of input values. far has worked , i'm happy output stage 3, problem encountering seems quite trivial can't seem locate it. in stage 3 attempting calculate largest polygon area need print out (x,y) coordinates assigned polygon. have method of determining largest polygon, works , outputs correct polygon, when try , read (x,y) coordinates polygon array , print values nothing in return, though compiles fine. i'm concerned printing out values in array on else.
this output require:
stage 3 ======= largest polygon 15643 x_val y_val 1.0 2.0 4.0 5.0 7.8 3.5 5.0 0.4 1.0 0.4
this output receiving:
stage 3 ======= largest polygon 15643 x_val y_val
here written code:
#include <stdio.h> #include <stdlib.h> #include <math.h> #define max_polys 100 #define max_pts 100 #define end_input 0 #define pi 3.141592653589793 #define per_row 5 double eccentricity(double perimeter, double area) { double eccen; eccen = (perimeter*perimeter)/(area*4*pi); return eccen; } double getdistance(int npoints, double x[], double y[]) { double distance = 0.0, dx, dy; int i; (i = 0; < npoints; ++i) { dx = x[(i+1) % npoints] - x[i]; dy = y[(i+1) % npoints] - y[i]; distance += sqrt(dx * dx + dy * dy); } return distance; } double poly_area(int length, double x[], double y[]) { double area = 0.0; int i, j; (i = 0; < length; ++i) { j = (i + 1) % length; area += (x[i] * y[j] - x[j] * y[i]); } area = area / 2; area = (area > 0 ? area : -1 * area); return (area); } int main(int argc, char *argv[]) { int npoints, point, poly_id, c, k; int npoints_largest, poly_id_largest; double x[max_pts], y[max_pts]; double ax[max_polys], ay[max_polys]; double perimeter, area = 0, eccen, largest_area; /* ================================stage 1=================================== */ if(scanf("%d %d", &npoints, &poly_id)) { (point = 0; point < npoints; ++point) { scanf("%lf %lf", &(x[point]), &(y[point])); } } perimeter = getdistance(npoints, x, y); area = poly_area(npoints, x, y); eccen = eccentricity(perimeter, area); printf("\nstage 1\n=======\n"); printf("first polygon %d\n", poly_id); printf(" x_val y_val\n"); (point = 0; point < npoints; ++point) { printf(" %2.1f %2.1f\n", x[point], y[point]); } printf("perimeter = %3.2f m\n", perimeter); printf("area = %3.2f m^2\n", area); printf("eccentricity = %3.2f\n", eccen); /* ================================stage 2=================================== */ printf("\nstage 2\n=======\n"); (c=0; c<per_row; c++) { printf("+-------"); if(c == per_row-1) { printf("+"); } } printf("\n| id | nval | perim | area | eccen |\n"); (c=0; c<per_row; c++) { printf("+-------"); if(c == per_row-1) { printf("+\n"); } } printf("| %5d | %5d | %5.2f | %5.2f | %5.2f |\n", poly_id, npoints, perimeter, area, eccen); while (scanf("%d %d", &npoints, &poly_id) == 2) { for(k = 0; k < npoints; k++) { scanf("%lf %lf", &(x[k]), &(y[k])); perimeter = getdistance(npoints, x, y); area = poly_area(npoints, x, y); eccen = eccentricity(perimeter, area); } if (area > largest_area) { largest_area = area; poly_id_largest = poly_id; npoints_largest = npoints; ax[k] = x[k]; // here attempt read largest area ay[k] = y[k]; // array. } printf("| %5d | %5d | %5.2f | %5.2f | %5.2f |\n", poly_id, npoints, perimeter, area, eccen); } (c=0; c<per_row; c++) { printf("+-------"); if(c == per_row-1) { printf("+\n"); } } /* ==============================stage 3===================================== */ printf("\nstage 3\n=======\n"); printf("largest polygon %d\n", poly_id_largest); printf(" x_val y_val\n"); (k = 0; k < npoints; ++k) { // trying loop , printf(" %2.1f %2.1f\n", ax[k], ay[k]); // print values } // prints out nothing though? return 0; }
if point out mistake in code or tip on it'd appreciated!
you need use npoints_largest variable in final loop:
for (k = 0; k < npoints_largest; ++k) {
Comments
Post a Comment