- #1
laminatedevildoll
- 211
- 0
I am trying to compile a sample self-avoiding random walk routine, but my compiler doesn't compile and it gives me error messages due to this line.
error 142 - Void type not permitted here
error 192 - An object of type 'void' cannot be assigned to an object of int
Here's the program:
Any help is appreciated. Thank you.
error 86 - Incomplete expressionint ipos[4] = {};
error 142 - Void type not permitted here
error 192 - An object of type 'void' cannot be assigned to an object of int
Here's the program:
#include <iostream.h>
#include <stdlib.h>
#include <math.h>
void do_walk (int maxstep, int& nstep, double& rsquared, double& weight ){
const int MAXSTEP=30;
int map[ MAXSTEP*2][MAXSTEP*2]={0};
// start point
int completed=0;
weight=1.0;
int x = MAXSTEP;
int y = MAXSTEP;
int npoint = 1;
map[x][y] = npoint;
do {
int xnew=x;
int ynew=y;
// Check which sites are available
int ipos[4] = {};
int npos = 0;
if( map[x-1][y] == 0 ) {
ipos[npos]=0;
npos++;
}
if( map[x+1][y] == 0 ) {
ipos[npos]=1;
npos++;
}
if( map[x][y-1] == 0 ) {
ipos[npos]=2;
npos++;
}
if( map[x][y+1] == 0 ) {
ipos[npos]=3;
npos++;
}
if( npos == 0 ){
completed =1;
} else {
if( npoint > 1) weight *= npos/3.0;
int i = (int)(npos *(double)rand()/(RAND_MAX+1.0));
switch ( ipos ) {
case 0: xnew-= 1; break;
case 1: xnew+= 1; break;
case 2: ynew-= 1; break;
case 3: ynew+= 1; break;
}
npoint++;
map[xnew][ynew] = npoint;
x = xnew;
y = ynew;
if ( npoint == maxstep+1 )completed=1;
}
} while ( !completed );
//
// Print a window around the centre of the array
//
for ( int i=12; i<2*MAXSTEP-12; i++ ){
for ( int j=12; j < 2*MAXSTEP-12; j++ ){
cout.width(3);
cout << map[j];
}
cout << endl;
}
nstep = npoint-1;
rsquared = pow( x-MAXSTEP,2.0) + pow( y-MAXSTEP, 2.0 );
}
int main(){
int maxstep=50,nstep;
double rsquared,weight;
srand(7654321);
for (int i=1; i<10; i++ ){
do_walk(maxstep,nstep,rsquared,weight);
cout << endl << "Nsteps: " <<nstep << " Rsquared: " <<rsquared<<" Weight: " << weight << endl;
}
return 0;
}
Any help is appreciated. Thank you.