无法在C中打印出数组中的值

时间:2015-04-26 05:09:36

标签: c arrays for-loop

我在编写此程序时,在给定一组输入值时计算多边形的面积和周长。到目前为止,一切都运作成功,我对输出到第3阶段很满意,我遇到的问题似乎相当微不足道,但我似乎无法找到它。在第3阶段,我试图计算最大的多边形区域,我需要打印出分配给该多边形的(x,y)坐标。我有一个确定最大多边形的方法,它可以工作并输出正确的多边形,但是当我尝试从该多边形读取(x,y)坐标到另一个数组并打印这些值时,我得不到任何回报,即使它所有编译都很好。我主要担心的是打印出数组中的值而不是其他所有内容。

这是我要求的输出:

Stage 3
=======
Largest polygon is 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

这是我收到的输出:

Stage 3
=======
Largest polygon is 15643
   x_val   y_val

这是我的书面代码:

#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;
    for (i = 0; i < 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;
    for (i = 0; i < 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)) {
        for (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 is %d\n", poly_id);
    printf("   x_val   y_val\n");
    for (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");
    for (c=0; c<PER_ROW; c++) {
        printf("+-------");
        if(c == PER_ROW-1) {
            printf("+");
        }
    }
    printf("\n|    id |  nval | perim |  area | eccen |\n");
    for (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 is where I attempt to read the largest area
            Ay[k] = y[k];  // into another array.
        }   

    printf("| %5d | %5d | %5.2f | %5.2f | %5.2f |\n", poly_id, npoints,
        perimeter, area, eccen);
    }

    for (c=0; c<PER_ROW; c++) {
        printf("+-------");
        if(c == PER_ROW-1) {
            printf("+\n");
        }
    }

/* ==============================Stage 3===================================== */    

    printf("\nStage 3\n=======\n");
    printf("Largest polygon is %d\n", poly_id_largest);
    printf("   x_val   y_val\n");
    for (k = 0; k < npoints; ++k) {                     // trying to loop and
        printf("    %2.1f     %2.1f\n", Ax[k], Ay[k]);  // print the values
    }                         // prints out nothing though?
    return 0;
}

如果有人能够指出我的代码中的错误或提示该做什么,那么我们将不胜感激!

2 个答案:

答案 0 :(得分:0)

您需要在最终for循环中使用npoints_largest变量:

for (k = 0; k < npoints_largest; ++k) {

答案 1 :(得分:0)

这很有趣。

所以你从用户那里输入5个多边形输入? 那么poly_id_largest为何如此之高?

请先将“Largest_area”设为0。

答案不对,它永远不会进入这段代码。

   if (area > largest_area) {
        largest_area = area;
        poly_id_largest = poly_id;
        npoints_largest = npoints;

        Ax[k] = x[k];  // here is where I attempt to read the largest area
        Ay[k] = y[k];  // into another array.
    }  
相关问题