我正在尝试在这里完成我的作业,即写一个程序,它显示了一定数量点的距离(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));
}
Run Code Online (Sandbox Code Playgroud)
这就是要做的事情:
编写程序,首先询问应创建多少个点,然后询问用户点的x和y值.然后程序应该给出一个表,显示Point和到(0,0)的距离.必须创建/使用以下函数:"point getpoint()" - 要求输入坐标"void printpoint(point p)" - 它打印点"double distanceToO(point p)"的坐标 - 返回距离(0,0)创建一个结构点,它有两个成员,一个点的x坐标和y坐标.
有人可以给我一个关于错误的提示吗?
int number, i;
struct point coord[number];
Run Code Online (Sandbox Code Playgroud)
number尚未初始化,您正在使用它来声明coord数组的大小.您将在堆栈上生成一个有效随机大小的数组,这可能会导致崩溃.