# My code so far Need Help

• July 21st, 2014, 02:47 AM
Jay123
My code so far Need Help
Code Java:

```public static void main(String[] args) int randNum = 0;     int arr[] = new int [20];   System.out.print("Array: ");   for (int a =0; a<arr.length;a++)   { randNum = (int)(Math.random()*(4)+1); arr[a] = randNum;   System.out.print(" " +randNum); }       System.out.println(" "); System.out.print("Random position: " + randNum);   }   }```
This is my Output

Array: 3 4 4 4 4 4 3 4 2 1 3 2 3 4 4 3 3 3 2 2
Random position: 2

the program will generate a random number between 0 and 19, which represents the location in the array (i.e. index number). Then, the 3 numbers to the left and right of this location should be reset to the value 0. If there isn’t 3 numbers to the left and right you may assume a lesser number depending on the boundaries of the array.

Any advice on how I can generate example this
Final Array: 1 2 0 0 0 5 0 0 0 4 4 2 1 1 3 2 1 4 3 2 1
• July 21st, 2014, 02:54 AM
GregBrannon
Re: My code so far Need Help
Please post your code correctly using code or highlight tags which are explained near the top of this link.
• July 21st, 2014, 07:40 AM
Jay123
Re: My code so far Need Help
sorry about that I am new. I hope this is better

--- Update ---

Anyone can help me out I just don't quite understand how to get the final output which the question ask.
• July 21st, 2014, 09:08 AM
GregBrannon
Re: My code so far Need Help
I don't understand the assignment. Can you copy and paste all of the directions?
• July 21st, 2014, 09:52 AM
Jay123
Re: My code so far Need Help
ok sure this is what they want from my assignment.

Write a program to create an integer array of size 20. Then, the program should generate and insert random integers between 1 and 5, inclusive into the array. Next, the program should print the array as output.

A tremor is defined as a point of movement to and fro. To simulate it, the program will generate a random number between 0 and 19, which represents the location in the array (i.e. index number). Then, the 3 numbers to the left and right of this location should be reset to the value 0. If there isn’t 3 numbers to the left and right you may assume a lesser number depending on the boundaries of the array.

Then, the final array should be printed as output. There is no user input for this program.

Your program must include, at least, the following methods:

• insertNumbers, which will take as input one integer array and store the random numbers in it.
• createTremor, which will generate the random number as the location and return it.

A sample run of the program is shown below:

Sample output #1:
Array: 1 2 2 3 1 5 4 2 3 4 4 2 1 1 3 2 1 4 3 2 1
Random position: 5
Final Array: 1 2 0 0 0 5 0 0 0 4 4 2 1 1 3 2 1 4 3 2 1
• July 21st, 2014, 11:51 AM
GregBrannon
Re: My code so far Need Help
Now that you've posted the entire text of the program's (assignment's) requirements - which are very clear, I'm going to show you how I think you should attack this program. I recommend you start with a skeleton like I've pasted below with the assignment's requirements pasted in as comments. I chopped them up and moved them around a bit to be where I think they belong, but you could do it differently. Once you have this outline that includes all requirements, start coding:
Code java:

```/* A tremor is defined as a point of movement to and fro. To simulate it, * the program will generate a random number between 0 and 19, which represents * the location in the array (i.e. index number). Then, the 3 numbers to the * left and right of this location should be reset to the value 0. If there * isn’t 3 numbers to the left and right you may assume a lesser number * depending on the boundaries of the array. There is no user input for this * program. * * Your program must include, at least, the following methods: * * • insertNumbers, which will take as input one integer array and store the * random numbers in it. * • createTremor, which will generate the random number as the location and * return it. */ public class TremorDemo { // instance variables // create an integer array of size 20.     // default constructor public TremorDemo() { // generate and insert (initialize the array with) random integers // between 1 and 5, inclusive, into the array.   // the program should print the array as output.   // the final array should be printed as output.   } // end default constructor   public static void main( String[] args ) { new TremorDemo();   } // end method main()   // required and supporting methods   // write the signature for each required method with the // description of the method given in the assignment, like this:   // method insertNumbers() takes as input one integer array and stores the // random numbers in it. private void insertNumbers( int[] tremorArray ) {     } // end method insertNumbers()     } // end class TremorDemo```
Then the key to getting the program right is to code each piece, testing and fixing each piece until it's right, then move to the next piece and repeat until done.

Good luck!
• July 21st, 2014, 04:48 PM
Jay123
Re: My code so far Need Help
Thank you. can I ask the part where they ask to reset 3 numbers from left to right is there any like advice on what video or code I should be using I cant figure that part out
• July 21st, 2014, 05:08 PM
GregBrannon
Re: My code so far Need Help
Show the code you've written to generate random number 1 to 5, inclusive, the insertNumbers() method. The code you showed originally was not correct.

Use your brain, not a video: To define the point at which the tremor begins, a random number is generated 0 to 19, exclusive. A for() loop could be used to 'visit' the 3 elements to the left of the index (less than) defined by the random number, the element at the index, and the 3 elements to the right of the index (greater than). That's just simple math, startIndex = index - 3, endIndex = index + 3. Care must be taken to ensure that startIndex >= 0 and endIndex <= 19. The whole thing: a couple simple equations, a couple if() statements, and a for() loop. Easy stuff.