Random walk program

Pages: 12
Hey guys I'm supposed to create this program for my c++ class but i am at a loss. i have been doing fine in the class up until now.

I am supposed to create a program that simulates a random walk that starts with position 0. If its odd it moves to the right (add one) if the random number is even it moves to the left (subtract 1).

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
#include<iostream>
#include<iomanip>
using namespace std;

 

void main()
{
	int num1, num2, num3, num4, i, rs = 12345 ;
cout << " Please enter the number iterations to be executed: ";
cin >> num1;

num4 = 0;
for (i = 0; i < num1 + 1; i++)

cout << i << ": ";

srand(rs);
num2 = rand();

if (num2 % 2 = 1)
{ 
num3 = num4 - 1;
cout << num3;
}
else
{
num3 = num4 + 1;
cout << num3;
}
system ("pause");
}


I'm getting an error when I try to run the program: error C2106: '=' : left operand must be l-value

So I don't even know if what I have is right.

Any help is greatly appreciated.
Last edited on
You need to use == in your if statement and not just one =
OK this is what I did and I'm still getting an error.

 
if (num2 % 2 == 1)
A few other things to mention are that you probably should use std:: to scope instead of using namespace std; and if you really don't want to type std:: you should put using std::cout; inside your main function. It should be int main() not void main(). Proper indentation won't hurt. Lastly you should try and avoid system.

Well the only reason I have it this way is because the instructor basically gave me this.

1
2
3
4
5
6
#include<iostream>
#include<iomanip>
using namespace std;


void main()


and told me to write the program.

Hows this?
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
#include<iostream>
#include<iomanip>
using namespace std;

 

void main()
{
	int num1, num2, num3, num4, i, rs=12345;
	cout << "Please enter the number iterations to be executed: ";
	cin >> num1;

	num4 = 0;
	for (i=0; i<num1+1; i++)
		cout << i << ": ";

	srand(rs);
	num2 = rand();

	if (num2 % 2==1)
	{
		num3 = num4 - 1;
		cout << num3;
	}
	else
	{
		num3 = num4 + 1;
		cout << num3;
	}
}
That's understandable but they are tips for you in the future. I would suggest to do it their way in class but outside of class do it the proper way.
I'm also still getting the same error:(C2106: '=' : left operand must be l-value) and I don't know why.
Have you recompiled it or did you just run the old one?
Oh OK I feel dumb haha I got it to run but the program I have is far from the example given.

After running it I changed it to this in order to make it more organized.
1
2
	for (i=0; i<num1+1; i++)
		cout << i << ": "<< endl;


How can i get it to show an asterisk in the middle along with the position its moved?

Like this.

0: 0: *
1: 1: * <--(this one is supposed to be moved over to the right 1 space)
2: 0: *

and so forth.
Last edited on
In line 13 do you mean num1 or num4?

You should initialize and declare it inside of your integer list.
Important to comment your code as friendly advice and comment where your problem is. That way I/someone else can be more.

from
int num1, num2, num3, num4, i, rs=12345;

to (i.e.)
int i;
int num1 = 0, num2 =1, num3 =2, etc.

And use shorthand operators. //+=, -=

I also built a similar random code but for a dice game as an example give me a sec.



Last edited on
They can't use compound operators look at them closer.

How can i get it to show an asterisk in the middle along with the position its moved? you could use a for loop to output the spaces then an asterisk or you could use std::setw. There are many approaches you could have taken.

http://www.cplusplus.com/reference/iomanip/setw/


Sorry bout that. Here you go. Pretty straight forward. Ask any questions if you don't follow. I will explain as best as I can. Thanks giblit.

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
#include "stdafx.h" //Don't include. trust me on this
#include <iostream>
#include <ctime>

using namespace std;

class Game1
{
private:

	int dice;
	int Value;

public:
	Game1()
	{
		srand(time(NULL)); // NULL == 0;
		roll();
	}

	void roll()
	{
		int n;
		cout << "Enter the number of sides on your Di: ";
		cin >> n;
		dice = (rand() % n) + 1;
		setdice();
		
	}

	void setdice()
	{
		Value = dice;
	}

	int getdice()
	{
		return Value;
	}

	void display()
	{
		cout << "The Di rolled a " << getdice() << "." << endl;
	}
};

int main()
{
	Game1 G1;
	G1.display();
	return 0;
}
Last edited on
Alright guys looks like I'm going to go figure out how I can do this and make it happen but thank you everybody for all your help.
Last edited on
*cough* code tags *cough*

[code]//some code[/code]


By the way that shouldn't even compile. Considering they are not class functions there is no way they can access class variables and modify them. It also seems a bit overkill using a class for something this small.


Alright guys looks like I'm going to go figure out how I can do this and make it happen but thank you everybody for all your help.


You have included iomanip why not make use of std::setw as I mentioned earlier
Last edited on
I am lol I figured that would be easier than a for loop the only problem I'm going to have is to get it to move to the left or right and show the position it moved.
OK I'm getting closer but still not there how would I go about moving the asterisk to the left or right?

Here is what I have so far.

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
#include<iostream>
#include<iomanip>
using namespace std;

 

void main()
{
	int num1, num2, num3, num4, i, rs=12345;
	cout << "Please enter the number iterations to be executed: ";
	cin >> num1;
	srand(rs);
	num2 = rand();
	num4 = 0;

	for (i=0; i<num1+1; i++)
	cout << setw(3) <<  i  << " : " << setw(4) << num4 << std::setw(15) << "*" << endl;
	
	if (num2 % 2==1)
	{
		num3 = num4 - 1;
		cout << num3;
	}
	else
	{
		num3 = num4 + 1;
		cout << num3;
	}
	system("pause");
}
Last edited on
std::setw(15)This should be num3 [edit]instead of 15[/edit]. Though you probably want to put line 13 inside of the for loop unless it is a linear movement.
Last edited on
When i do that I get this error: Run-Time Check Failure #3 - The variable 'num3' is being used without being initialized. With the option of break or continue.

 
std::setw(num3) //what I did 
Wait a second. You don't even have braces around your for loop so only the output is repeated. Also it should be after the if statements.

I would suggest something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	for (i=0; i<num1+1; i++)
        {
                num2 = rand();
	        if (num2 % 2==1)
	        {
		        num3 = num4 - 1;
		        cout << num3; //not sure if you need this 
	        }
	        else
	        {
	            num3 = num4 + 1;
		    cout << num3; //not sure if you need this
	        }
	        cout << setw(3) <<  i  << " : " << setw(4) << num4 << std::setw(num3) << "*" << endl;
	}


Another thing you never modify num4 and the variable is not needed.

I would simply put ++num3 and --num3. Though more descriptive variable names would be best practice. Like randomNumber and currentPosition.


[EDIT]

Here is an example of what I think you are trying to do:

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
#include<iostream>
#include<iomanip>
 
int main()
{
    std::srand(static_cast<unsigned>(std::time(nullptr)));
 
    int iterations;
    std::cout << "How many iterations would you like(<0 = quit): ";
    std::cin >> iterations;
 
    const int initialPosition = 5;
    int currentPosition = initialPosition;
 
    for(int i = 0; i < iterations; ++i)
    {
 
        if(std::rand() % 2 == 0) //random & 1 == 1 -- even
        {
            ++currentPosition;
        }
        else
        {
            --currentPosition;
        }
 
        std::cout << "Iteration: " << std::setw(4) << i+1 << ':' << std::setw(currentPosition) << '*' << std::endl;
    }
 
}
Iteration:    1:     *
Iteration:    2:      *
Iteration:    3:       *
Iteration:    4:      *
Iteration:    5:       *
Iteration:    6:        *
Iteration:    7:         *
Iteration:    8:          *
Iteration:    9:         *
Iteration:   10:          *


Iteration:    1:   *
Iteration:    2:  *
Iteration:    3: *
Iteration:    4:  *
Iteration:    5: *
Iteration:    6:*
Iteration:    7:*
Iteration:    8:*
Iteration:    9:*
Iteration:   10:*


Iteration:    1:   *
Iteration:    2:    *
Iteration:    3:   *
Iteration:    4:  *
Iteration:    5: *
Iteration:    6:  *
Iteration:    7: *
Iteration:    8:  *
Iteration:    9: *
Iteration:   10:*

http://coliru.stacked-crooked.com/ (not sure how to do inputs on corilu)
http://ideone.com/TDU7uz
Last edited on
OK Here is what i have now, i added a few things to make it look like the example but I'm still having trouble with making it move left or right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	for (i=0; i<num1+1; i++)
        {
                num2 = rand();
	        if (num2 % 2==1)
	        {
		        num3 = num4 - 1;  //you were right i didn't need those
	        }
	        else
	        {
	            num3 = num4 + 1;
	        }
	        cout << setw(3) <<  i  << ": " << setw(2) << num4 << ": " 
			<< std::setw(num3) << setw (10) << "*" << endl;
	}
Last edited on
Pages: 12