Compiling but not running, "drunk walk" problem

Hello everybody.

I'm hoping you can help me along a little bit with a problem similar to this one: http://web.mst.edu/~csw6g3/cs78/lab6.txt except I am to use loops and nested loops. Also do less "trips" as they put it.

I plan on changing it slightly (starting location = 3 then 4 then 5 ect) for obtaining the other results. But the code will be the same for each (I presume) so I just need some tips on how to proceed. Right now it compiles in visual studio but does nothing when i run it.

here is the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
# include <ctime>

using namespace std;

void main()
{

					
	
	srand(time(NULL));

	//	start block 2

	
	double random;
	int step;
	int homecount = 0;
	double location ; 
	int moves = 0;
	int total_moves = 0;

	for(int i = 0; i < 500; i++)		//	500 tests to run
	{		moves++;
			location = 2;				//	starting location 

		while(location >= 1 && location <= 8);		
			random = double(rand())/RAND_MAX * 1;			//	random number generated 
		{	step = int (random * 3);	
				if (step == 1) location++;		
				else location--;	
		
				if (location = 8) homecount++;

		}
		
	
		total_moves = moves + total_moves;
	}

	

	cout<<"The average number of moves was: "<<total_moves/500<<endl;		// Results (what I'm required to produce)
	cout<<"The average number of times he ends up at home is: "<<homecount/500<<endl;
	
	
} 



I realize that using function would be advantageous but I would really like to learn how to do this problem by using nested loops and such. Sorry if I'm totally wrong with this...I'm quite new. Thanks.
There might be more, but the syntax on line 33 is wrong.
Also is there a reason you have main as a void?

Edit: The semicolon at the end of the while loop is causing it to run forever.
Last edited on
Thanks for the reply...I tried fixing some things. Added a loop so that I re-started the process and (hopefully) moved to the next spot. The results are surly wrong but I'm at least getting some output. I'll keep working at it...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

#include <iostream>
# include <ctime>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{

					
	
	srand(time(NULL));

	//	start block 2

	
	int step;
	double homecount = 0;
	int moves = 0;
	int total_moves = 0;
	int location = 0;


	for(int i = 2; i <= 7 ; i++)
	{	cout<<"Starting at block "<<i<<endl;
		int blockstart = i;

	for(int i = 1; i < 500; i++)		//	500 tests to run
	{		moves++;
			blockstart = location;				//	starting location 

		while(location >= 1 && location <= 8);		
		{
										//	random number generated 
			step = rand() % 3;	
		
				if (step <= 1.0/3.0) location++;		
				else location--;	
		
				if (location == 8) homecount++;
		}
			
	
		total_moves = moves + total_moves;
	}

	

	cout<<"The average number of moves was: "<<total_moves/500 <<endl;		// Results (what I'm required to produce)
	cout<<"The average number of times he ends up at home is: "<<homecount/500.0<<endl;
	
	}
}
Topic archived. No new replies allowed.