在c中填充2d数组的第二列

我用[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++)
    {
        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]);
    } 
}
点赞
评论
David
David
&Temperature[0][j]

The actual element would be under Temperature[j][1].

点赞
评论