我正在尝试做一个二维数组,其中有2列和一些行。第一列是用扫描输入的半径,而第二列依赖于第一列是区域。
我已经试过将它们排除在循环之外(手动输入),然后立即输出它们,但不知怎么的,只有最后和第一个输入是正确的。
#define circlecol 1
#define circlerow 1
int main() {
float circles[circlerow][circlecol];
for(int x = 0; x <= circlerow; x++) {
scanf("%f", &circles[x][0]);
circles[x][1] = 3.14*circles[x][0]*circles[x][0];
}`
输入为8和3,我希望这是输出。
你的圈子: 8.000000 200.960000 3.000000 28.260000
但我明白了
你的圈子: 8.000000 0.000000 0.000000 28.260000
格式是
你的圈子:11
发布于 2019-08-06 17:43:47
这个数组
float circles[circlerow][circlecol];
实际上被宣布为
float circles[1][1];
也就是说,它有一个行和一个列,只有一个元素可以使用表达式circle[0][0]
访问。
看来你的意思是
#define circlecol 2
#define circlerow 2
int main( void ) {
float circles[circlerow][circlecol];
for(int x = 0; x < circlerow; x++) {
scanf("%f", &circles[x][0]);
circles[x][1] = 3.14*circles[x][0]*circles[x][0];
}
}
也就是说,数组应该有两行两列。
发布于 2019-08-06 17:39:56
改变这一点:
for(int x = 0; x <= circlerow; x++)
对此:
for(int x = 0; x < circlerow; x++)
因为数组索引从0开始,以数组- 1的大小结束。
类似地,您将执行for(int j = 0; j < circlecol; j++)
。
通常,如果将数组声明为:
array[rows][cols]
那么它的尺寸是rows x cols
。array[0][0]
是第1行和第1列中的元素,array[rows - 1][cols - 1]
是最后一列和最后一行中的元素。
最小完整示例:
#include <stdio.h>
#define circlecol 1
#define circlerow 1
int main(void) {
float circles[circlerow][circlecol];
for(int x = 0; x < circlerow; x++) {
scanf("%f", &circles[x][0]);
circles[x][1] = 3.14*circles[x][0]*circles[x][0];
}
for(int i = 0; i < circlerow; i++)
for(int j = 0; j < circlecol; j++)
printf("%f", circles[i][j]);
return 0;
}
https://stackoverflow.com/questions/57381237
复制相似问题