首先我想说我是一名学生,并且是编码新手。因此,我为我在这方面普遍缺乏知识而提前道歉......
我正在尝试编写代码来读取整数并将它们存储在多维数组中。整数是图表上的点、x 和 y 值。另外,我希望数组大小是用户将输入的变量(如用户将输入的点数——points[numPoints][2])。这是我到目前为止所拥有的:
#include <stdio.h>
int main()
{
int numPoints=0, row, colm;
int points[numPoints][2];
printf("This program will read in a number of points, and calculate the distance between them.\n");
printf("It will also calculate the total distance between the first point, and the last point.\n");
printf("\nHow many points will you be entering?\n");
scanf(" %d", &numPoints);
printf("Please enter each point individually.\nExample(x & y values shown):\nx y\n3 5\n-2 10\netc...\nPlease enter your points now (press 'Enter' after each point):\n");
for (row = 0; row<numPoints; row++)
{
for (colm = 0; colm<2; colm++)
{
scanf("%d", &points[row][colm]);
}
}
printf("These are the points recorded:\n");
for (row = 0, colm=0; row<numPoints; row++)
{
printf("(%d,%d)\n", points[row][colm],points[row][colm+1]);
}
return 0;
}
我已经运行了几次,这是我得到的输出。它要么没有正确存储数字,要么没有正确打印它们。
This program will read in a number of points, and calculate the distance between them.
It will also calculate the total distance between the first point, and the last point.
How many points will you be entering?
4
Please enter each point individually.
Example(x & y values shown):
x y
3 5
-2 10
etc...
Please enter your points now (press 'Enter' after each point):
1 2
3 4
5 6
7 8
These are the points recorded:
(1,2)
(3,4)
(6,31)
(1,8)
Program ended with exit code: 0
谁能告诉我我做错了什么?
谢谢
最佳答案
我希望这段代码能帮助你:
#include <stdio.h>
int main()
{
int numPoints =0,row, colm;
int points[10][2];
printf("This program will read in a number of points, and calculate the distance between them.\n");
printf("It will also calculate the total distance between the first point, and the last point.\n");
printf("\nHow many points will you be entering?\n");
scanf(" %d", &numPoints);
printf("Please enter each point individually.\nExample(x & y values shown):\nx y\n3 5\n-2 10\netc...\nPlease enter your points now (press 'Enter' after each point):\n");
for (row = 0; row < numPoints; row++)
{
for (colm = 0; colm<2; colm++)
{
scanf("%d", &points[row][colm]);
}
}
printf("These are the points recorded:\n");
for (row = 0, colm=0; row<numPoints; row++)
{
printf("(%d,%d)\n", points[row][colm],points[row][colm+1]);
}
return 0;
}
关于c - 将整数存储在带有变量的二维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21769479/