gpt4 book ai didi

c - 无法从 C 中的数组中打印出值

转载 作者:太空宇宙 更新时间:2023-11-04 04:36:54 25 4
gpt4 key购买 nike

我正在编写这个程序来计算给定一组特定输入值时多边形的面积和周长。到目前为止,一切顺利,我对第 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) {

关于c - 无法从 C 中的数组中打印出值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29874009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com