Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 打印一块5 x 5的简单电路板,使用随机数进行阵列和循环_C_Arrays_Random - Fatal编程技术网

C 打印一块5 x 5的简单电路板,使用随机数进行阵列和循环

C 打印一块5 x 5的简单电路板,使用随机数进行阵列和循环,c,arrays,random,C,Arrays,Random,在这里之前,我问了一个非常复杂的问题 但是我发现我还不能理解所有的概念,还有太多未知的东西,所以我决定一步一步地学习 因此,现在我正在尝试使用随机数数组创建5x5板。以下是我的代码: #include <stdio.h> #include <stdlib.h> #include <time.h> //Declare the board size and other variables// //Create the r

在这里之前,我问了一个非常复杂的问题

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

因此,现在我正在尝试使用随机数数组创建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
#包括
#包括
#包括
//声明电路板大小和其他变量//
//创建随机数生成器种子
//循环以创建所需的电路板大小
//将随机数植入循环内的电路板中
int main()
{
//初始化变量
整数随机数;
int行;
int列;
//声明电路板尺寸。电路板尺寸为5 x 5
国际板[5][5];
//创建随机数生成器种子
srand(时间(空));
//将1-25之间的随机数指定为可变随机数
randomNumber=rand()%25+1;
//为线路板创建行

对于(rows=1;rows请尝试以下操作:

#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
#包括
#包括
#包括
//声明电路板大小和其他变量//
//创建随机数生成器种子
//循环以创建所需的电路板大小
//将随机数植入循环内的电路板中
int main()
{
//初始化变量
整数随机数;
int行;
int列;
//声明电路板尺寸。电路板尺寸为5 x 5
国际板[5][5];
//创建随机数生成器种子
srand(时间(空));
//将1-25之间的随机数指定为可变随机数
//为线路板创建行
用于(行=0;行<5;行++)
{
//为板创建列
用于(列=0;列<5;列++)
{
//将变量随机数分配到变量板
randomNumber=rand()%25+1;
线路板[行][列]=随机数;
printf(“%d\t”,线路板[行][列]);
}
//换行符位于第5列末尾。
printf(“\n”);
}
}//端干管

您需要使用
=
运算符将生成的数字分配给电路板。您所做的只是在1-25范围内随机撬动一行和一列-因此您可能在大多数情况下会在此处遇到异常,如果它起作用,您的数组将不会填充,因此默认情况下具有值(由于您的数组是在方法内部定义的,其中有一些随机值,但在填充值之前您无法确定其中包含的内容)

请记住数组从位置0开始。因此,类似
int table[5]
的内容包含从
表[0]
表[4]
的项。请勿访问
表[5]
。你好,reox,谢谢你的快速回复。我在理解如何将随机数输入黑板时遇到了困难,现在我明白了,谢谢你!:)干杯!