lundi 6 avril 2015

End condition of an IF loop that depends on a 3D array

I'm writing a program which will move a particle about a cube, either left,right,up,down, back or forward depending on the value randomly generator by the program. The particle is able to move with cube of dimensions LxLxL. I wish the program to stop when the particle has been to all possible sites and the number of jumps taken output.


Currently I am doing this using an array[i][j][k] and when the particle has been to a position, changing the value of the array at that corresponding point to 0. However, in my IF loop I have to type out every possible combination of i,j and k in order to say if they are all equal to 0 the program should end. Would there be a better way to do this?


Thanks, Beth



#include<stdio.h>
#include<math.h>

int randomint(int max);

main()
{
int i, lmax, lmin, start, max, seed, x, n, y, finish, row, z, N, newrow,
square, row, column, page, j, k;

start=1;
max=6;
lmin=1;
finish=0;
lmax=4;
square=pow(lmax,2);
N=pow(lmax,3);
//printf("Enter number of sites:");
//scanf("%d", &lmax);
printf("Enter random seed:");
scanf("%d", &seed);


srandom(seed);

for(i=1; i<21; i++)
{
n=randomint(max);
printf("random- %d\n", n);

if(n==0)
{

z=start%lmax;
row=(start-z);
row=row/lmax;

if(z==0)
{
row=row-1;
}

start=start-1;
z=start%lmax;
newrow=(start-z);
newrow=newrow/lmax;

if(z==0)
{
row=row-1;
}

if(start<=0)
{
newrow=-1;
}

if(newrow!=row)
{
start=start+lmax;
}
}

if(n==1)
{
z=start%square;
row=(start-z);
row=row/square;

start=start+lmax;
z=start%square;
newrow=(start-z);
newrow=newrow/square;

if(z==0)
{
newrow=newrow-1;
}



if(row!=newrow)
{
start=start-square;
}

}
if(n==2)
{
z=start%N;
row=(start-z);
row=row/N;
row=row+1;

start=start+square;
z=start%N;
newrow=(start-z);
newrow=newrow/N;
newrow=newrow+1;

if(newrow!=row)
{
start=start-N;
}
}

if(n==3)
{

z=start%lmax;
row=(start-z);
row=row/lmax;

if(z==0)
{
row=row-1;
}


start=start+1;
z=start%lmax;
newrow=(start-z);
newrow=newrow/lmax;

if(z==0)
{
newrow=newrow-1;
}




if(newrow!=row)
{
start=start-lmax;
}

}

if(n==4)
{
z=start%square;
row=(start-z);
row=row/square;

if(z==0)
{
row=row-1;
}


start=start-lmax;
z=start%square;
newrow=(start-z);
newrow=newrow/square;

if(z==0)
{
row=row-1;
}

if(start<=0)
{
newrow=-1;
}

if(row!=newrow)
{
start=start+square;
}

}

if(n==5)
{
z=start%N;
row=(start-z);
row=row/N;
row=row+1;


start=start-square;
z=start%N;
newrow=(start-z);
newrow=newrow/N;
newrow=newrow+1;
if(start<0)
{
newrow=-1;
}

if(newrow!=row)
{
start=start+N;
}

}

printf("postition: %d\n", start);
column=start%lmax;

row=(start-column);
row=row/lmax;

if(column==0)
{
row=row-1;
}

z=start%square;
page=(start-z);
page=page/lmax;

if(z==0)
{
page=page-1;
}

i=page;
j=column;
k=row;

list[i][j][k]=0;
counter=counter+1;

for(i = 0; i < 15; i++)

{
for(j= 0; j < 15; j++)

{
for(k = 0; k < 15; k++)

{
printf("%d ", list[i][j][k]);
}
}
}

**if((list[0][0][0]=0)&&(list[1][0][0]==0)&&(list[0][0][1]=0)&&(list[0]
[1]
[0]==0)&&(list[1][1][0]==0)&&(list[1][0][1]==0)&&(list[0][1][1]==0)&&
(list[1][1][1]==0)&&(list[2][0][0]==0)&&(list[2][0][1]==0)&&(list[2][1]
[0]==0)&&(list[2][1][1]==0)&&(list[2][2][0]==0)&&(list[2][2][1]==0)&&
(list[2][1][2]==0)&&(list[2][2][2]=0)&&(list[0][2][0]==0)&&(list[0][2]
[1]==0)&&(list[1][2][0]==0)&&(list[1][2][1]==0)&&(list[0][2][2]==0)&&
(list[2][2][2]==0)&&(list[0][0][2]==0)&&(list[2][1][1]==0))**
{
finish=1;
}





}

}
int randomint( int max)
{
return (random() % max);
}

Aucun commentaire:

Enregistrer un commentaire