我正在尝试在这里做功课,即编写一个程序,显示一定数量的点到 (0,0) 的距离。但是由于某些原因,一旦我的程序启动,Windows 就会说它已停止工作。我用两个不同的编译器试过了,它们没有给我任何错误消息。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct point {
int x;
int y;
};
struct point getPoint();
void printPoint(struct point);
double distanceToO(struct point p);
void createArray(struct point, int);
int main() {
int number, i;
struct point coord[number];
printf("Type the number of points you want to create: ");
scanf("%d", &number);
printf("\n\n");
for(i=0;i<number;i++)
coord[i]=getPoint();
printf("\n\t\tPoint\tDistance to (0,0)\n");
for(i=0;i<number;i++) {
printPoint(coord[i]);
printf("\t%0.2lf", distanceToO(coord[i]));
}
system("pause");
return 0;
}
struct point getPoint() {
struct point p;
printf("Type the x and the y-value for a point with a space in between: ");
scanf("%d %d", &p.x, &p.y);
return p;
}
void printPoint(struct point p){
printf("\n\t\t(%d,%d)",p.x,p.y);
}
double distanceToO(struct point p) {
return sqrt((0-p.x)*(0-p.x)+(0-p.y)*(0-p.y));
}
具体操作如下:
编写一个程序,首先询问应该创建多少个点,然后询问用户这些点的 x 和 y 值。 然后程序应该给出一个表格,显示点和到 (0,0) 的距离。 必须创建/使用以下函数: "point getpoint()"- 要求输入坐标 "void printpoint(point p)"- 打印点的坐标 "double distanceToO(point p)"- 返回到 (0,0) 的距离 创建一个结构点,它有两个成员点的 x 坐标和 y 坐标。
谁能告诉我哪里出了问题?
最佳答案
int number, i;
struct point coord[number];
number
尚未初始化,您正在使用它来声明 coord
数组的大小。您将在堆栈上生成一个有效随机大小的数组,这很可能会导致崩溃。
关于c - 我的 C 程序停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15248926/