How to fill the array using the RAND () and WHILE function?

Hello everybody!
I need to create an array that is populated randomly, considering an element with a value of 1 that can traverse positions in the array. Starting with a value in the middle position and selecting random positions for the other terms, I want it to scroll through the array until it finds the previous value. I made the code from what I could think of, but after requesting the dimensions of the array the program hangs, I even thought it might be because of WHILE conditions. Could someone help me with the resolution? Thank you!

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


int **Alocar_matrix(int m, int n) //function for allocation of memory
{
int i,**v;
//ponteiro para a matriz - pointer for matrix
v = (int **) calloc (m, sizeof(int *));
if (v == NULL) {
printf ("Erro: Memoria Insuficiente"); //just a error test
return (NULL);
}
//alocacao das colunas da matriz
for ( i = 0; i < m; i++ )
{
v[i] = (int*) calloc (n, sizeof(int));

if (v[i] == NULL) {
printf ("Erro: Memoria Insuficiente");
return (NULL);
}
}
return (v); //retorna o ponteiro para a matriz
}

int main()
{
int **matrix;
int i, j, x, y, step, erro, size;
erro=0;

FILE *dla_program; //save the data in dla_program.txt
dla_program = fopen("dla.txt","w");

do{
printf("Informe o tamanho da matriz quadrada desejada. Ela deve ser maior que 100!"); // it's a information from the user: the size of the matrix
scanf("%d",&size);
}while((size<100)|| (size>500));

if ((matrix = Alocar_matrix(size, size))== NULL) erro = 1;
if (erro)
{
printf("\n Falta de memoria! Saindo...\n"); //erro na alocacao ---test of memory
exit(1);
}

for(i=0;i<size;i++)
{
for(j=0;j<size;j++) //zerar a matrix -- define zero value to inicialization
matrix[i][j]=0;
}
matrix[(size-1)/2][(size-1)/2]=1; //particula localizada no centro ---allocation of memory

x=size+1;
y=size+1;
do{
while((x>=size)||(y>= size))
{ // while generalizado
while(1)
{
x=rand() % size;
y=rand() % size;
if(matrix[x-1][y]||matrix[x+1][y]||matrix[x][y-1]||matrix[x][y+1]||matrix[x][y]==0) //escolher uma posição aleatória que não seja vizinha do centro -- a choice of random position
break;
}

if(rand()%1 <0.5) step=1; //condicional para definir a direção do deslocamento -- random movement
else step=-1;
while(matrix[x-1][y]+matrix[x+1][y]+matrix[x][y-1]+matrix[x][y+1]==0||(x<size)||(y<size))
{
if(rand()%1 <0.5) x+=step;
else y+=step;

} //procurar um vizinho
matrix[x][y]=1; //quando encontrar um vizinho dentro de "size" declara na posicao da matrix o valor 1

}
}while(matrix[size-1][y]||matrix[x][size-1]||matrix[0][y]||matrix[x][0]!=0);

for(i=0;i<size;i++)
{ printf("\n");
{
for(j=0;j<size;j++)
fprintf(dla_program, "\t%d ", matrix[i][j]);
}

printf("Dados armazenados em dla.txt \n\n"); //data storage in dla.txt
fclose(dla_program);

}
}





I can't understand your description or your code.

One thing you're doing wrong, though, is taking rand() mod 1 (and then, strangely, comparing it to 0.5!). Mod 1 will always be 0 (i.e., when an integer is divided by 1, there is no remainder). You probably mean something like:
1
2
3
4
if (rand() % 2 == 0)
    x += step;
else
    y += step;

Last edited on
Topic archived. No new replies allowed.