簡體   English   中英

無法從C中的數組中打印出值

[英]Trouble printing out values from an array in C

給定一組輸入值時,我正在編寫此程序來計算多邊形的面積和周長。 到目前為止,一切工作都已經成功完成,並且我對直到第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;
}

如果有人可以指出我的代碼中的錯誤或操作提示,不勝感激!

您需要在最終的for循環中使用npoints_largest變量:

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

這是有趣的。

因此,您要從用戶輸入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.
    }  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM