Having trouble compiling a SAW routine

  • Thread starter Thread starter laminatedevildoll
  • Start date Start date
AI Thread Summary
The discussion centers around a coding issue related to a self-avoiding random walk routine in C++. The main problem arises from the line "int ipos[4] = {};", which generates multiple compilation errors, including "Incomplete expression" and "Void type not permitted here." The suggested solution is to change this line to "int ipos[4];" instead, as initializing the array to zero is unnecessary in this context. The code includes a function that simulates the random walk, tracking the position on a grid and calculating the number of steps and the squared distance from the starting point. The user seeks assistance with the compilation errors to successfully run the program.
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++ ){
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.
 
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
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
0
Views
362
Replies
22
Views
3K
Replies
23
Views
2K
Replies
5
Views
3K
Replies
23
Views
2K
Replies
5
Views
1K
Back
Top