# Help with one-dimensional array

• April 25th, 2012, 10:48 AM
brandon66
Help with one-dimensional array
Hey everyone, new to java here some troubles getting this down mostly step 4 i believe i have everything else correct not sure though.

1. Create a one-dimensional array of 99 double values. Then, use a for loop to add a random number from 0 to 100 to each element in the array. For each value, use the random method of the Math class to get a double value between 0.0 and 1.0, and multiply it by 100.

2. Use an enhanced for loop to sum the values in the array. Then, calculate the average value and print that value on the console followed by a blank line. Compile and test this class.

3. Use the sort method of the Arrays class to sort the values in the array, and print themedian value (the 50th value) on the console followed by a blank line. Then, test this enhancement.

4 .Print the 9th value of the array on the console and every 9th value after that. Then,test this enhancement

Code :

```import java.util.Arrays;   public class ArrayTestApp { public static void main(String[] args) { double[] testArray = new double [99];   //add a random number to each element in the array for (int i=0; i < testArray.length; i++) testArray[i] = 100.0*Math.random();       //sum the values and print out the average double average = 0.0; for(int i = 0; i < testArray.length; i++) average += testArray[i]; average /= 99; System.out.println("Average is: " + average); System.out.println();   //sort the values and print the median Arrays.sort(testArray); System.out.println("Median is: " + testArray[testArray.length/2]); System.out.println();   //print the 9th value and every 9th value after       } }```
• April 25th, 2012, 11:16 AM
andreas90
Re: Help with one-dimensional array
Hello brandon66!
Quote:

Originally Posted by brandon66
1. Create a one-dimensional array of 99 double values. Then, use a for loop to add a random number from 0 to 100 to each element in the array. For each value, use the random method of the Math class to get a double value between 0.0 and 1.0, and multiply it by 100.

2. Use an enhanced for loop to sum the values in the array. Then, calculate the average value and print that value on the console followed by a blank line. Compile and test this class.

3. Use the sort method of the Arrays class to sort the values in the array, and print themedian value (the 50th value) on the console followed by a blank line. Then, test this enhancement.

4 .Print the 9th value of the array on the console and every 9th value after that. Then,test this enhancement

For task 2, it says it needs an enhanced for loop. Check the java tutorial and an example.
For task 4, since you know how to loop through an array what is the problem you are facing?
You need to start from the 9th element and then print every 9th element of your array instead of starting in the first element (index 0) and then do something with the next element until the last one, like you do in the other for loops.
• April 25th, 2012, 12:01 PM
brandon66
Re: Help with one-dimensional array
so the enhanced for loop would look like this: ?

Code :

```import java.util.Arrays;   public class ArrayTestApp { public static void main(String[] args) { double[] testArray = new double [99];   //add a random number to each element in the array for (int i=0; i < testArray.length; i++) testArray[i] = 100.0*Math.random();       //sum the values and print out the average double sum = 0.0; for(double a: testArray) { sum += a; } double average = sum/testArray.length; System.out.println("Average is: " + average); System.out.println();   //sort the values and print the median Arrays.sort(testArray); System.out.println("Median is: " + testArray[testArray.length/2]); System.out.println();   //print the 9th value and every 9th value after```
• April 25th, 2012, 12:11 PM
andreas90
Re: Help with one-dimensional array
Quote:

Originally Posted by brandon66
so the enhanced for loop would look like this: ?

Yes, this is the correct syntax for the enhanced for loop.
• April 25th, 2012, 12:30 PM
brandon66
Re: Help with one-dimensional array
for step 4 would i create a for loop like this:?

Code :

``` for(int i = 9; i < testArray.length; i++) { System.out.println(testArray[i]); }```
• April 25th, 2012, 12:51 PM
andreas90
Re: Help with one-dimensional array
Quote:

Originally Posted by brandon66
for step 4 would i create a for loop like this:?

Code :

``` for(int i = 9; i < testArray.length; i++) { System.out.println(testArray[i]); }```

That way your loop will print every element in the array with an index >=9. This is because in each iteration it goes to the next index (i++ means that i is incremented by one in each iteration). But you want it to print every 9th element.
• April 26th, 2012, 02:19 AM
Parranoia
Re: Help with one-dimensional array
Quote:

Originally Posted by brandon66
for step 4 would i create a for loop like this:?

Code :

``` for(int i = 9; i < testArray.length; i++) { System.out.println(testArray[i]); }```

To get it to print every 9th element after that, you could either check if i is divisible by 9, or increment i by 9 each time.
Examples:
Code :

```for (int i = 9; i < testArray.length; i++) { if (i % 9 == 0) System.out.println(testArray[i]); }```

Code :

```for (int i = 9; i < testArray.length; i = i + 9) { System.out.println(testArray[i]); }```
• April 26th, 2012, 04:59 PM
brandon66
Re: Help with one-dimensional array
Thanks alot guys for the help :) here is the finished code

Code :

```import java.util.Arrays;   public class ArrayTestApp { public static void main(String[] args) { double[] testArray = new double [99];   //add a random number to each element in the array for (int i=0; i < testArray.length; i++) testArray[i] = 100.0*Math.random();       //sum the values and print out the average double sum = 0.0; for(double a: testArray) { sum += a; } double average = sum/testArray.length; System.out.println("Average is: " + average); System.out.println();   //sort the values and print the median Arrays.sort(testArray); System.out.println("Median is: " + testArray[testArray.length/2]); System.out.println();   //print the 9th value and every 9th value after for (int i = 8; i < testArray.length; i = i + 9) { System.out.println("9th values: " + testArray[i]); } } }```