c - 使用数组和随机数循环打印一个简单的 5 x 5 板

标签 c arrays random

我之前在Generating Unique Random Numbers in an Array using Loop问过一个非常复杂的问题

但是我发现我还不能理解所有的概念,未知的东西太多,所以我决定一步步学习。

所以现在我正在尝试使用带有随机数的数组创建一个 5x5 板..这是我的代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    //Declare the board size and other variables//

    //Create the random number generator seed

    //Loop to create the wanted board size

    //Plant the random numbers into the board within the loop

    int main()

    {
    //Initialize Variables
    int randomNumber;
    int rows;
    int columns;

    //Declare board size. Size of board is 5 x 5
    int board[5][5]; 

    //Create the random number generator seed
    srand(time(NULL));

    //Assign the random numbers from 1 - 25 into variable randomNumber
    randomNumber = rand() %25 + 1;

    //Create the rows for the board
        for ( rows = 1; rows <= 5 ; rows++ )
        {
            //Create the columns for the board
            for ( columns = 1; columns <= 5 ;  columns++ )
             {
             //Assign variable randomNumber into variable board
             board[randomNumber][randomNumber];
        }
            //Newline after the end of 5th column.
            printf("\n");
    }

    //Print the board
    printf("%d\t", board[randomNumber][randomNumber]);

}//end main

最后一部分board[randomNumber][randomNumber];是我认为我真的很困惑的地方。我真的不知道该怎么办。

我正在尝试将随机数分配到棋盘中,但我犯了严重错误。

各位有什么指点吗?

最佳答案

尝试这样的事情:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//Declare the board size and other variables//

//Create the random number generator seed

//Loop to create the wanted board size

//Plant the random numbers into the board within the loop

int main()

{
//Initialize Variables
int randomNumber;
int rows;
int columns;

//Declare board size. size of board is 5 x 5
int board[5][5]; 

//Create the random number generator seed
srand(time(NULL));

//Assign the random numbers from 1 - 25 into variable randomNumber

//Create the rows for the board
    for ( rows = 0; rows < 5 ; row++ )
    {
        //Create the columns for the board
        for ( columns = 0; columns < 5 ;  columns++ )
         {
         //Assign variable randomNumber into variable board
         randomNumber = rand() %25 + 1;

         board[rows][columns] = randomNumber;
         printf("%d\t", board[rows][columns]);

    }
        //Newline after the end of 5th column.
        printf("\n");
}

}//end main

您需要使用=运算符将生成的编号分配给板。您所做的只是在 1-25 范围内随机排列行和列 - 所以大多数时候您可能会在这里得到异常,如果它有效,您的数组不会被填充,因此默认情况下会有值(因为您的数组是在方法内部定义的,里面有一些随机值,但你不能确定里面有什么,直到你用值填充它)

关于c - 使用数组和随机数循环打印一个简单的 5 x 5 板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19492456/

相关文章:

arrays - Google Sheets 公式将列添加到数组?

c++ - 以通用方式选择有效的随机枚举值

c++ - 每次arduino启动时如何防止颜色数组成为相同的随机数组

c - 如何在 C 中创建具有可变字符串长度的转换/查找表

Java 菜鸟,codewars 类(class) #1 - 数组的最小值

java - 有人能告诉我如何在 java 中创建线程组中 n 个线程的数组吗?

c - 随机数生成器代码问题

c - 如何在 cJSON 中测试 bool 值是否为 TRUE/FALSE

c - 如何将 char * 放入数组中,以便我可以在 qsort 中使用它,然后转到下一行

c - 为什么我不能用 C 代码制作这个系列