这个题是要求产生随机数并把这些数存到2维数组里,第一个选项是控制随机数的产生,
是要求如果在进入这个选项之前程序是可以产生随机数的,就关掉,这时候数组显示连续的数,
如果在进入这个选项之前产生的是连续的数,就打开,产生随机数。数组是nxn。随机数从0~255.
我不会这个阿~~这个选项是没有数据输入的,直接就显示是关还是开。。。。我都快晕了。。。
结果显示为:
menu
1. Seeding
2. Fill array
3. Exit
please enter number: 1
Seeding
-------
Random seeding is turned off.
menu
1. Seeding
2. Fill array
3. Exit
please enter number: 2
Array
-----
enter the size of array: 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
menu
1. Seeding
2. Fill array
3. Exit
please enter number: 1
Seeding
-------
Random seeding is turned on.
menu
1. Seeding
2. Fill array
3. Exit
please enter number: 2
Array
-----
enter the size of array: 4
23 12 253 34
5 36 115 65
10 18 92 244
53 32 9 8
我写的code:
#include <stdio.h>
#include <time.h>
#define ROWS 15
#define COLS 15
void buffer();
void Seeding(int array[][]);
void FillArray(int array[][]);
int main()
{
char choice;
int array[ROWS][COLS] = {0};
int done = 1;
while(done)
{
printf("\n Main Menu ");
printf("\n---------------------------");
printf("\n1) Seeding");
printf("\n2) Fill array");
printf("\n3) Exit");
printf("\n---------------------------");
printf("\nEnter your option [1-3] : ");
choice = getchar();
buffer();
switch(choice)
{
case '1':
Seeding(array);
break;
case '2':
FillArray(array);
break;
case '3':
exit(0);
break;
default :
printf("\nInvalid input! Please try again!\n");
}
}
return 0;
}
//这个不知道怎么写,请各位帮帮我~~~
void Seeding(int array[][])
{
int status;
printf("\nSeeding");
printf("\n--------------");
if(status == 1)
{
srand(time(NULL));
printf("\nRandom seeding is turned on.");
}
else if(status == 0)
{
srand(0);
printf("\n\nRandom seeding is turned off.");
}
}
void FillArray(int array[ROWS][COLS])//这个我只写了能产生随机数的。。。
{
int i, j;
int num;
int done = 1;
printf("\nFill array");
printf("\n----------");
printf("\n\nEnter the size of array : ");
while(done)
{
if(scanf("%d", &num) == 1)
{
buffer();
if(num > 1 && num < 16)
{
for(i = 0; i < num; i++)
{
for(j = 0; j < num; j++)
{
array[i][j] = rand()%255;
printf("%2d ", array[i][j]);
}
printf("\n");
}
return;
}
else
{
printf("\nInvalid input! Please try again!\n");
break;
}
}
else
{
printf("\nInvalid input! Please try again!\n");
buffer();
break;
}
}
}
void buffer()
{
int ch;
while ((ch = fgetc(stdin)) != '\n' && ch != EOF)
;
clearerr(stdin);
}
[此贴子已经被作者于2007-3-20 0:43:32编辑过]