数组中随机数的问题
想在一个二维数组中随机赋值1和0。比如说在6*6的二维数组中随机赋值三个1,其他都为0,没有思路 数组中原来有值吗 #include<stdio.h>#include <stdlib.h>
#include <time.h>
#define MAX_COL (6)
#define MAX_ROW (6)
#define CNT_1 (3)
int main()
{
int array = {0};
int index = 0;
int count = 0;
int col = 0;
int row = 0;
srand(time(NULL));
do
{
index = rand() % (MAX_COL * MAX_ROW);
col = index / MAX_COL;
row = index % MAX_ROW;
if(array == 0)
{
array = 1;
count++;
}
if(count >= CNT_1)
{
break;
}
}while(1);
for(col=0 ; col<MAX_COL ; col++)
{
for(row=0 ; row<MAX_ROW ; row++)
{
printf("%d,",array);
}
printf("%\n");
}
return 0;
}
jitianmoshen 发表于 2020-12-9 23:51
数组中原来有值吗
没
页:
[1]