Assign element to array only when space is available?

0

I have some vectors that I need to assign in a 20x20 array, but no vector can override the other and not be printed on different lines, they should be assigned vertically or horizontally. For example, I have the vectors:

int a[8] ={1,2,3,4,5,6,7,8};

int b[7] ={7459015};

int c[6] ={5,4,3,7,9,1};

And a 20x20 array I want to insert the vectors a and b horizontally and c vertical, but this in a random order, every time the program runs it will assign a, b and c in different places. However, these numbers can not be overwritten, that is, they must all be in the array completely and in addition they must be vertical or horizontal, without changing the column if it is vertical, and without changing the line if it is horizontal.

I tried to generate a random seed of row and column for this, and I used an array defined with all elements being -1 , so far so good, but how do I check if my vector fits in that position generated by rand ? That without overwriting a number other than -1?

I'm not going to post the code because it's very simple, just a rand for row and column and assigning the array to the array, I did not do any processing to know if there is room for the array in the array, because I did not know it.

In short, it's like generating a random array position and see if you have room in the orientation (vertical or horizontal) to receive my vector. Without overwriting any number that is not -1 .

    
asked by anonymous 09.10.2015 / 06:31

1 answer

2

You need to check if space is available before writing the values. You also need to check if the random position does not leave the existing space.

Suppose you wanted to put 5 values horizontally from (1, 1);

bool disponivel;
do {
    disponivel = true;
    startx = 1; // ou escolha aleatoria
    starty = 1; // ou escolha aleatoria
    for (int k = 0; k < 5; k++) {
        if (a[starty][startx + k] != -1) { disponivel = false; break; )
    }
} while (!disponivel);
// aqui já podes atribuir valores no array
    
09.10.2015 / 12:07