dimanche 20 novembre 2022

Filling 2D ARRAY with characters from string

I'm trying to fill an array with some selected characters from string. 2 columns have to stay empty, but this is how it's working for me. Every symbol should be equal to number of rows. Like ball sort game.. Some advice? Thanks.

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;
                    }
                    
                }
                
            }
            
        }
        
    }

This is how it should look.

result




Aucun commentaire:

Enregistrer un commentaire