-4

Write a program to take in the size of the array and an integer N that simulates a “random” walk. using a coin flip for N steps. The initial position is the middle of the array and if the coin flip is heads (0) move 1 step to the right, tails (1) move one cell to the left. Each time they move into a cell the cell is incremented. After flipping the coin a given number of times then print out the total number of times the cell was occupied. Print out”*” for each visit so that they can see the random walk graphically.

^This is the task I have been assigned, I am really not sure where to start, neither I am I sure how to do the coin flip. Or how to flip a coin or increment the cell, I am not asking for you to write the program. Just to give me an idea of how to write the program, using HSA console, or give me other pointer links in the forum. Thanks in advance.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Split it up into smaller tasks. I.e. a generator for a sequence of indices to visit and a visitor-function that handles the steps on the array based on those indices. The [Random](http://docs.oracle.com/javase/7/docs/api/java/util/Random.html)-class from the Java-API should do the trick for the coin-flip. Never the less you should narrow down the question to something more specific ([How to ask a good question](http://stackoverflow.com/help/how-to-ask)). –  Apr 30 '17 at 16:54
  • Please see: [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – EJoshuaS - Stand with Ukraine Apr 30 '17 at 18:05

1 Answers1

0

I would start by creating an integer array and finding the middle. You can flip a coin by generating a random number between say 0 and 1, (int)(Math.random()*2) and then add or subtract 1 from your starting place in the array corresponding to the coin flip. Then as you enter a new cell, increase that value by 1 every time, testArray[location]++. That will allow you to know how many times to print "*" per cell.