Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 14th, 2006, 7:13 PM   #1
splitterdevildoll
Newbie
 
Join Date: Aug 2005
Posts: 12
Rep Power: 0 splitterdevildoll is on a distinguished road
Having trouble compiling a SAW routine

I am trying to a sample self-avoiding random walk routine, but my compiler doesn't compile and it gives me error messages due to this line.

Quote:
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:

Quote:
#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[i] ) {
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[i][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.
splitterdevildoll is offline   Reply With Quote
Old Jun 14th, 2006, 7:14 PM   #2
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
Try replacing with this?
int ipos[4] = '\0';

I'm only guessing you're trying to null the position by having "int ipos[4] = {};", and the above code would be the correct way.

Or if you mean to null the entire array..

int ipos[4];
for (int i = 0; i < 4; i++) ipos[i]='\0';
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Jun 14th, 2006, 7:24 PM   #3
splitterdevildoll
Newbie
 
Join Date: Aug 2005
Posts: 12
Rep Power: 0 splitterdevildoll is on a distinguished road
It compiles now, but I cannot get the program to work. It says it's a run time error.

Here's a link of what the sample output should look like.

http://hepwww.ph.qmul.ac.uk/sim/sarw...file=sarw.html
It's the very last output in the link.
splitterdevildoll is offline   Reply With Quote
Old Jun 14th, 2006, 8:36 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You need to read a thread on how to post a question. Your first post was excellent, but your second one gives no information to work with. Information is key, both in getting answers and in debugging. A sample of the proper output is essentially worthless, since your program is running off into the weeds and barfing in its shoes. One guess is that your run-time error is a memory violation. There's a good chance that you are violating the dimensions of an array somewhere. You could also be trying to modify memory that isn't your own by abusing a pointer. Collect all the information your system gives you regarding the error. Shoot, you don't even mention your platform, OS, and compiler. If you have a debugger, step through the program or set breakpoints at various places. The latter method allows you to do a binary search to zero in on the offending statements, if you play your cards right.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 14th, 2006, 8:57 PM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
Worked for me under VC2005
Final ouput:
Nsteps: 50 Rsquared: 200 Weight: 0.0438957

As DaWei says, you need to give more information.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:40 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC