i am really bad at programing and i need help with this problem:
Program Description:
A somnambulist begins walking aimlessly, starting at his bed. He takes N steps. Each step taken is either north, east, south, or west. How far will the somnambulist be from his bed after N steps?
Write a program that asks the user to enter the parameter N —the number of steps— and then simulates the motion of a random walker for N steps. The direction of each step must be decided randomly by your program. (Hint: Use the random number generator to determine the direction of each step. Refer to pages 133-134 of your textbook. ) After each step, print the location of the random walker, treating the bed as the origin (0, 0). Also, print the distance from the point where he stopped (x, y) —his final position after the N steps— to the origin. (Hint: Use the distance formula.) Print the location of each step and the final distance to a file. Prompt the user for the file path. At the end of each simulation, ask the user if he wishes to perform another simulation. If he answers yes, then the program should do another simulation, otherwise, it should exit. All the simulations should be written to the same file. The file should be closed only after the program terminates.
Example: N = 26

Simulated Path:
1. (0, 1) 14. (4, 2)
2. (-1, 1) 15. (4, 1)
3. (-2, 1) 16. (3, 1)
4. (-2, 2) 17. (2, 1)
5. (-2, 3) 18. (2, 0)
6. (-1, 3) 19. (3, 0)
7. (-1, 2) 20. (3, -1)
8. (0, 2) 21. (4, -1)
9. (1, 2) 22. (4, -2)
10. (1, 3) 23. (4, -3)
11. (2, 3) 24. (4, -4)
12. (2, 2) 25. (3, -4)
13. (3, 2) 26. (3, -3)
Distance: 4.24 rounded to two decimal places.
Remark: Your program must produce a different sequence of steps for every simulation.
Input Validation:
 Continue to prompt the user for parameter N, until a valid number is entered.
 Continue to prompt the user for a file path, until a valid file path is provided.

Recommended Answers

All 2 Replies

Why do people want to become programmers and go to school trying to learn it, when they have absolutely no logical sense in life. You don't even consider general forum rules.

Me and friends made games doing this kind of stuff when we were 10 years old oh god, and you don't even seem to try!?

Consider this:

const int n = ??;
struct
{
    int x;
    int y;
} position;

position.x = 0;
position.y = 0;

for(int i = 0; i < n; i++)
{
    int direction = rand() % 4;
    switch(direction)
    {
        case 0:
            position.x++; // east
            break;

        case 1:
            position.y++; // north
            break;
  
        case 2:
            position.x--; // west
            break;
        
        case 3:
            position.y--; //south
            break;
    }
}

Edit: Removed #include, seed, cout'ing and other stuff - I can't believe I almost served a completed code, but finally something easy that I can answer xD. But the bastard wouldn't learn anything.

Try to figure out what to add to solve your task.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.