我正在嘗試用字串中的一些選定字符填充一個陣列。2 列必須留空,但這就是我的作業方式。每個符號應等于行數。像球類游戲……有什么建議嗎?謝謝。
void generator(const int rows, const int columns, char field[rows][columns]){
// seed
srand(time(NULL));
int random_index;
// choosing empty columns (they'll be by side)
int clear_column[2];
// choosing random number in range of columns
clear_column[0] = rand() % (columns 1);
// adding 1 index to already choosen number
clear_column[1] = clear_column[0] 1;
// if choosen number is equal to number of columns => second empty column will be on the left side
if ( clear_column[0] == columns)
{
clear_column[1] = clear_column[0]-1;
}
// variable to store all symbols
char store_symbol[10] = "^@#&* -/$";
// variable to store used symbols
int store_index[10] = {0,0,0,0,0,0,0,0,0};
// ** i = columns; k = rows
// loops all columns
for (int i = 0; i < columns; i )
{
// loops all rows
for (int k = 0; k < rows; k )
{
// adding empty columns
if ( i == clear_column[0] || i == clear_column[1])
{
field[k][i] = ' ';
}
else{
int got_symbol = 0;
while (got_symbol == 0)
{
random_index = rand() % rows;
if ( store_index[random_index] <= rows)
{
field[i][k] = store_symbol[random_index];
store_index[random_index] = 1;
got_symbol = 1;
break;
}
}
}
}
}
它應該是這樣的。

uj5u.com熱心網友回復:
要修復此函式中潛在的未定義行為rows,當不等于時columns,此處的索引
field[i][k] = store_symbol[random_index];
需要翻轉
field[k][i] = store_symbol[random_index];
以下允許rows對陣列中的每個字符加一個:
if (store_index[random_index] <= rows) {
更改<=為<以將陣列中每個字符的數量限制為最大rows.
以下
int clear_column[2];
clear_column[0] = rand() % (columns 1);
clear_column[1] = clear_column[0] 1;
if (clear_column[0] == columns) {
clear_column[1] = clear_column[0]-1;
}
引入差一錯誤。如果clear_column[0]等于columns,則唯一要清除的列將是最后一列,通過clear_column[1]( clear_column[1] = clear_column[0] - 1;)。
i永遠不會進入columns回圈。
有幾個解決方案:
確保columns > 1,找到有效范圍內的第一列minus,將下一列拿過來。
int clear_column[2] = { rand() % (columns - 1) };
clear_column[1] = clear_column[0] 1;
或者,在有效范圍內找到要清除的第一列,并在任一方向調整第二列:
int clear_column[2] = { rand() % columns };
clear_column[1] = clear_column[0] (clear_column[0] == columns - 1 ? -1 : 1);
旁白:這會將空列更頻繁地分配到最后兩列(clear_column[0]beingcolumns - 1或columns - 2導致相同的模式。
或者,您可以將第二列換行以清除到陣列的開頭:
clear_column[0] = rand() % columns;
clear_column[1] = (clear_column[0] 1) % columns;
或者,完全隨機化已清除的列。
int clear_column[2] = { rand() % columns };
while ((clear_column[1] = rand() % columns) == clear_column[0]);
不過,最后兩個確實會產生不同的模式。
需求有問題
每個符號應等于行數
當元素數減去空格數(行乘以二)不等于行的平方時,因為您允許rows不同的字符最多rowstimes each 。
相反,您應該允許columns - 2不同的字符rows每次最多出現一次。
以下是應用于您的函式的上述更改的示例:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SYM "^@#&* -/$?"
#define MAX_SYM 10
#define def(c, a, i, d) (c > i ? atoi(a[i]) : d)
void generator(const int rows, const int columns, char field[rows][columns])
{
int clear_column[2] = { rand() % columns };
clear_column[1] = (clear_column[0] 1) % columns;
char store_symbol[MAX_SYM] = SYM;
int store_index[MAX_SYM] = { 0 };
for (int i = 0; i < columns; i ) {
for (int k = 0; k < rows; k ) {
if (i == clear_column[0] || i == clear_column[1]) {
field[k][i] = ' ';
continue;
}
while (1) {
int random_index = rand() % (columns - 2);
if (store_index[random_index] < rows) {
field[k][i] = store_symbol[random_index];
store_index[random_index] = 1;
break;
}
}
}
}
}
int main(int argc, char **argv)
{
int r = def(argc, argv, 1, 4);
int c = def(argc, argv, 2, 6);
if (r < 1 || c < 1 || r > MAX_SYM || c > MAX_SYM) {
fprintf(stderr, "Invalid dimensions %dx%d\n", r, c);
return EXIT_FAILURE;
}
char map[r][c];
srand((unsigned) time(NULL));
generator(r, c, map);
for (int i = 0; i < r; i ) {
printf("|");
for (int j = 0; j < c; j ) {
printf(" %c |", map[i][j]);
}
putchar('\n');
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/536607.html
標籤:数组C2d
