Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中从数组打印值时出现问题_C_Arrays_For Loop - Fatal编程技术网

在C中从数组打印值时出现问题

在C中从数组打印值时出现问题,c,arrays,for-loop,C,Arrays,For Loop,我写这个程序是为了在给定一组输入值时计算多边形的面积和周长。到目前为止,一切都很顺利,我对第三阶段的结果很满意,我遇到的问题似乎很小,但我似乎找不到它。在第3阶段,我试图计算最大的多边形面积,我需要打印出指定给该多边形的x,y坐标。我有一种确定最大多边形的方法,它可以工作并输出正确的多边形,但是当我尝试将该多边形的x,y坐标读入另一个数组并打印这些值时,我没有得到任何回报,即使它编译得很好。我主要关心的是打印出数组中的值,而不是其他值 这是我需要的输出: Stage 3 ======= Larg

我写这个程序是为了在给定一组输入值时计算多边形的面积和周长。到目前为止,一切都很顺利,我对第三阶段的结果很满意,我遇到的问题似乎很小,但我似乎找不到它。在第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_最大变量:

for (k = 0; k < npoints_largest; ++k) {
这很有趣

那么您正在输入用户的5个多边形输入? 那么为什么poly_id_最大值这么高呢

请先将最大_区域设置为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.
    }  

我尝试了这个,它打印了一个输出,但是所有的点都是0.0。输入上可以有1到100个多边形,多边形id是每个多边形的id,以知道它是哪一个。poly_id_max只是表示这是最大多边形的id。这段代码就是我用来找到最大多边形的。它已成功识别出哪个多边形,但尚未将该多边形的x、y坐标放入新数组。还有其他方法吗?是的,在这条线上,双周长,面积=0,eccen,最大面积;将其更改为双周长,面积=0,eccen,最大面积=0;如果area>Maximum_area{Maximum_area=area;poly_id_Maximum=poly_id;npoints_Maximum=npoints;Ax[0]=x[k];//这里是我试图将最大面积Ay[0]=y[k];//读入另一个数组的地方。}因此,你只能有一个面积最大的多边形,这里也是k=0;k<1++k{printf%2.1f%2.1f\n,Ax[k],Ay[k];}对于您可能要查看的所有x,y坐标,我得到的只是0.0值。这与这个问题惊人地相似。