Having trouble compiling a SAW routine

  • Thread starter laminatedevildoll
  • Start date
In summary, the conversation is about a person trying to compile a self-avoiding random walk routine but is facing errors due to a line of code. The program includes a function called "do_walk" that has a loop to check for available sites and assigns a weight based on the number of available sites. The conversation ends with a suggestion to remove the " = {}" in the line of code causing the errors.
  • #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.

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
  • #2
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
 
  • #3


It looks like the issue is with the line "int ipos[4] = {};". In C++, an array must have a size specified within the square brackets, so the correct syntax would be "int ipos[4] = {0};". Additionally, the line "weight *= npos/3.0;" may also cause an error, as the division is between an integer and a double. To fix this, you can change it to "weight *= (double)npos/3.0;". Finally, make sure to include the "using namespace std;" line at the top of your program to avoid having to use "std::" before every standard library function.

I hope this helps. If you continue to have trouble compiling, please provide the specific error messages and we can work through them together. Also, make sure to check for any typos or missing semicolons in your code. Good luck!
 

1. Why am I having trouble compiling my SAW routine?

There could be several reasons for having trouble compiling a SAW routine. Some common reasons include errors in the code, missing dependencies or libraries, or incorrect compiler settings. It is important to carefully review the code and troubleshoot any errors or issues that may be causing the problem.

2. How can I fix errors in my SAW routine code?

To fix errors in your SAW routine code, you will need to carefully review the code and look for any syntax errors or missing elements. It may also be helpful to consult the documentation or seek assistance from other programmers or forums to identify and resolve the errors.

3. What are some common dependencies or libraries needed for compiling a SAW routine?

Some common dependencies or libraries needed for compiling a SAW routine may include specific programming languages, frameworks, or toolkits. It is important to check the documentation or instructions for the specific SAW routine you are trying to compile to ensure you have all the necessary dependencies and libraries installed.

4. How can I ensure my compiler settings are correct for compiling a SAW routine?

To ensure your compiler settings are correct for compiling a SAW routine, it is important to carefully review the instructions or documentation for the specific routine. You may also need to adjust the settings in your compiler or IDE (integrated development environment) to match the requirements of the SAW routine.

5. What should I do if I am still having trouble compiling my SAW routine after troubleshooting?

If you have tried troubleshooting and are still having trouble compiling your SAW routine, it may be helpful to seek assistance from other programmers or support forums. It is also important to have a clear understanding of the requirements and instructions for the specific SAW routine you are trying to compile. Additionally, double-checking your code and dependencies may also help to identify and resolve any issues.

Similar threads

  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
11
Views
995
  • Programming and Computer Science
Replies
1
Views
640
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top