c - 在c中填充二维数组的第二列
问题描述
我用 [20,25] 范围内的随机数填充第一列,现在我希望用户用 [20,25] 范围内的随机数填充第二列。我怎样才能做到这一点?
#include <stdio.h>
void main()
{
int Temperature[5][2] = {{20},{21},{22},{23},{24}};
printf("I created a 2D array of size 5x2,");
printf("and I filled the first column with random values in the range [20,25]\n");
for(int i=0; i<5; i++)
{
printf("%d ",Temperature[i][0]);
printf("\n");
}
printf("Please fill the second column with values in the range [0,20]\n");
int i, j;
for(j=0;j<5;j++)
{
printf("Value[%d]:",j);
scanf("%d", &Temperature[0][j]);
}
}
解决方案
对于 C 中的二维数组,array[x][y] ---> 表示第 x行和第y列
由于数组是 0 索引的,因此示例中的第二列意味着列号应为 1(0 是第一列)
修改后的代码
#include <stdio.h>
void main()
{
int Temperature[5][2]={{20},{21},{22},{23},{24}};
printf("I created a 2D array of size 5x2,");
printf("and I filled the first column with random values in the range [20,25]\n");
for(int i=0; i<5; i++)
{
//correction done here
printf("%d ",Temperature[i][0]);
printf("\n");
}
printf("Please fill the second column with values in the range [0,20]\n");
int i, j;
for(j=0;j<5;j++)
{
printf("Value[%d]:",j);
scanf("%d", &Temperature[j][1]);
}
}
推荐阅读
- python-3.x - 打开字幕的 REST API POST /api/v1/download
- kubernetes - ${VARIABLE} 是发布管道吗?
- javascript - 使用 React-Native 在流 Socket(端口 443)中连接
- windows - 使用 Powershell 变量构建 Docker 运行选项字符串
- reactjs - Ionic React 项目版本控制
- spring-boot - 从 springboot 后端中的 javafx bean 加载 localhost 内容
- excel - 如何检测丢失的用户窗体
- android - 如何在 react-native 应用程序中关闭不支持的 Google Play 服务提示
- laravel - 优化 Laravel 查询以根据类型进行计数
- java - Minecraft 统计 Json - Java 或 Python