If you want to incorporate randomness into a programming environment, use the include header, if using C++, called -->> "time.h" << -- . If you don't know about it it's very simple. You seed the random integer/etc. using -->> srand(time(0)); <<-- and then a switch statement usually handles the rest. I think this site helps with that certain problem though.
If you want specific help from me about it just ask.
if I generate 0 or 1 for movement how are they interpreted? this is an if statement to me. How are you tracking the ant's position? I don't care about backwards or forwards from your code, unless I am counting them. The problem on the web site you gave didn't indicate you needed to.
mybe need to set default on 4.....the step could be more than 9! mybe up to 60 step....
the 0 will be the forward movement
the 1 will be the backward movement....
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main()
{
int antPosition = 4; // ant's default position.
int steps = 0; // the steps counter...
int movement = 0; // the movement direction indicator
srand((unsigned)time(NULL));
for(steps = 0; steps < 10; steps++)
{
// dump out the two lines of the fancy graph...
// the top bar of each row and the middle line
// this can be done in another for loop.
// there will be an if the counter of the for is equal to the ants position.
// now we do a movement
movement = rand()%2;
if(movement == 1) // we will move foward
{
antPosition++; // forward we increment the ants position
}
else // we will move back wards.
{
antPosition--; // backwards we decrease the ants position
}
// I will want to make sure the ant is still in bounds for our move
// this will be an if of antPosition for the ranges give in your problem
// if we are out of bounds we reset to the upper or lower limit.
} // the end of the for loop.
// now dump out the bottom bar of the graph using another for loop.
return 0;
}// end of main
Yes I have seen it, its pretty easy for me a 20 years of c++. If you read the comments up there I give you in a previous one it tells you a little bit about how to do it.
// dump out the two lines of the fancy graph...
// the top bar of each row and the middle line
// this can be done in another for loop.
// there will be an if the counter of the for is equal to the ants position.
// dump out the header line header
// start with the step count box.
cout << "+-----------+";
// loop through to create the cap of the other boxes. I don't know if I have the count right.
for(int nIndex =0; nIndex < 9; nIndex ++)
{
cout << "-------+";
}
// once the line is complete dump a endl for the new line.
cout << endl;
// print out the first part of the step line. I would have to work at it to make it line up
cout << "| Step #" << steps << " |";
// the rest here looks simalar to what is before this but I need to test for the antPosition against nIndex if I use the variable again.
I am not responding to anymore.. You need to figure this out on your own. I think I have given you most of what you need to finish the program. I have given you all the clues in all the code segments in the code. Most of this should compile on a standard c++ compiler, which borland c++ build is.
The original code I gave you had no output lines so if it ran you wouldn't see anything output.
did you create a console application?