Having trouble compiling a SAW routine

  • Thread starter Thread starter laminatedevildoll
  • Start date Start date
Click For Summary
SUMMARY

The forum discussion centers on a compilation issue with a self-avoiding random walk routine written in C++. The user encounters multiple error messages related to the line "int ipos[4] = {};". The solution provided is to change this line to "int ipos[4];", as initializing the array is unnecessary and causes compilation errors. The discussion highlights the importance of proper array initialization in C++ to avoid void type errors.

PREREQUISITES
  • Understanding of C++ syntax and compilation processes
  • Familiarity with array initialization in C++
  • Knowledge of random number generation using the rand() function
  • Basic concepts of self-avoiding random walks in computational simulations
NEXT STEPS
  • Research proper array initialization techniques in C++
  • Explore error handling and debugging in C++ compilation
  • Learn about random number generation and its applications in simulations
  • Study advanced topics in computational algorithms for random walks
USEFUL FOR

C++ developers, computer science students, and anyone interested in computational simulations and debugging C++ code.

laminatedevildoll
Messages
211
Reaction score
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.

int ipos[4] = {};

error 86 - Incomplete expression
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++ ){
count.width(3);
count << map[j];
}
count << 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);
count << endl << "Nsteps: " <<nstep << " Rsquared: " <<rsquared<<" Weight: " << weight << endl;
}
return 0;
}


Any help is appreciated. Thank you.
 
Technology news on Phys.org
change
int ipos[4] = {};
to
int ipos[4];

i don't see why the array needs to be zero'd out, if that's what your attempting
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
Replies
11
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
63
Views
6K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
13K
  • · Replies 23 ·
Replies
23
Views
3K