【发布时间】:2014-06-10 23:43:10
【问题描述】:
所以我创建了一个代码,允许用户输入数组的大小。然后将每个值设置为 0。然后程序获取此代码,然后将随机 1 添加到数组中。然后它输出数组,打印 '.'代替 0 和 '*' 代替 1,但每次我打印出来时它不会随机化 1 和 0 的位置。我只是保持相同的 1 和 0 布局。我错过了一行代码,还是只需要重新排列?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
solid()
{
int width, height;
int* myEnvironment(int width, int height, int numWoodchips);
printf("\nEnter rows and columns : ");
scanf("%d %d", &width, &height);
int myArray[width][height];
void printEnvironment(int* environment, int width, int height);
int i,j;
printf("\n>Creating grid of size [%d][%d]\n", width, height);
for (i = 0; i < width; i++)
{
for (j = 0; j < height; j++)
{
myArray[i][j] = rand() %2;
if(myArray[i][j]==0)
printf(".");
else if(myArray[i][j]==1)
printf("*");
}
}
printf("\n");
printf("\n>grid of size [%d][%d] created \n", width, height);
return (myArray[i][j]);
我需要返回不同的东西吗?
【问题讨论】:
-
您展示的代码对于您展示的功能甚至都不完整。你在初始化随机数生成器吗?