-1

I want to make a 2d field from a two-dimensional array, the task is to randomly arrange the icons (for example: ^, @ ...), of course, I also need to make sure that every time there is a column where there are no icons, this is later, it doesn’t work for me, maybe I wrote something wrong here, but help.

`

void generator(const int rows, const int columns, char field[rows][columns]){
    rows = 4;
    columns = 6;
    int f = 0;
    int r, c, r_, c_;
    char bud;
        field = { 
                    {'@', '^', ' ', ' ', '*', '+'},
                    {'@', '^', ' ', ' ', '*', '+'},
                    {'@', '^', ' ', ' ', '*', '+'},
                    {'@', '^', ' ', ' ', '*', '+'}            
        };

    while(f != 40){
        srand(time(NULL));
        r = 1 + rand()%(4 - 1 + 1);
        c = 1 + rand()%(6 - 1 + 1);
        r_ = 1 + rand()%(4 - 1 + 1);
        c_ = 1 + rand()%(6 - 1 + 1);
        if(field != ' '){
            bud = field[r][c];
            field[r][c] = field[r_][c_];
            field[r_][c_] = bud;
        }
        f++;
    }
}

` here I randomly indicate that you need to generate numbers from 1 to 4 to insert into rows and from 1 to 6 to insert into columns

for me the code is logically correct, but I'm just starting to learn the C language, please help

Karaoki
  • 11
  • 2
  • 2
    Please move `srand(time(NULL));` to the beginning of `main` calling it just once. It is not a part of obtaining a pseudo-random number. – Weather Vane Nov 22 '22 at 19:54
  • 2
    `srand(time(NULL));` should be called once at the beginning of your program. The way you have it now you're resetting the seed every time which generates the same number every time it's called in the same second. – Retired Ninja Nov 22 '22 at 19:54
  • You are adding `1` to the values obtained from `rand` to use as an array index, but arrays are indexed from `0`. – Weather Vane Nov 22 '22 at 19:56
  • Why does your function overwrite the `rows` and `columns` arguments provided, and worse, ignore them when computing array indexes? For example, `r = 1 + rand()%(4 - 1 + 1);` should be `r = rand() % rows;`. Simple. – Weather Vane Nov 22 '22 at 19:57
  • `if(field != ' '){` is obviously wrong. It is same as `if(&(field[0][0]) != ' '){`, meaning comparing address to character code of space (32 in practice). Doesn't make any sense. – hyde Nov 22 '22 at 20:03
  • I removed rows and columns by writing field[4][6], srand(time(NULL)); - put before while, added the variable g and equated it to 40, but I have an error in main.c:9:23: error: expected expression before '{' token 9 | field[4][6] = { – Karaoki Nov 22 '22 at 20:04
  • and the question is how can I make sure that those places in the array where nothing is specified, it does not change places? – Karaoki Nov 22 '22 at 20:06
  • Welcome! Having posted questions for some time you should already know it is expected that you post a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), the shortest *complete* code that shows the problem. The best way to do that is by copy/paste. May I suggest you take the [Tour](https://stackoverflow.com/tour) and read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) You still have not read the introductory material. Stackoverflow isn't a "rolling tutorial service". – Weather Vane Nov 22 '22 at 20:08
  • Similar question [here](https://stackoverflow.com/q/74509722/2505965). Must be a popular class, as there have been a few of these this week[.](https://stackoverflow.com/q/74511065/2505965) – Oka Nov 22 '22 at 20:46
  • I think applying a filter would great to collect like features, sort of like [Conway's](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life). – Neil Nov 22 '22 at 23:57

1 Answers1

0

Swap column 2 and 3 of the symbols to figure out where the columns where spaces should end up. Then swap non-space fields.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define ROWS 4
#define COLUMNS 6
#define SWAPS 40

void swap(char *a, char *b) {
    char tmp = *a;
    *a = *b;
    *b = tmp;
}

char *rand_non_space(size_t rows, size_t columns, char field[rows][columns]) {
    for(;;) {
        char *ch = &field[rand() % rows][rand() % columns];
        if(*ch != ' ') return ch;
    }
}

void generator(size_t rows, size_t columns, char field[rows][columns]){
    char symbols[] = "@^  *+";
    for(size_t i = 2; i <= 3; i++)
        swap (
            symbols + i,
            symbols + rand() % columns
        );
    for(size_t row = 0; row < rows; row++) {
        memcpy(field[row], symbols, columns);
    }
    for(size_t i = 0; i < SWAPS; i++) {
        swap(
            rand_non_space(rows, columns, field),
            rand_non_space(rows, columns, field)
        );
    }
}

int main(void) {
    srand(time(NULL));
    char field[ROWS][COLUMNS];
    generator(ROWS, COLUMNS, field);
    for(size_t row = 0; row < ROWS; row++) {
        printf("%.*s\n", COLUMNS, field[row]);
    }
}

and sample output (cherry-picked to avoid the first and last column from being space to illustrate it the space columns more clearly):

^ ^^ *
@ +@ *
^ @@ *
* ++ +
Allan Wind
  • 23,068
  • 5
  • 28
  • 38