# Problem with arrays

• November 23rd, 2013, 06:14 AM
Problem with arrays
Good evening everyone ,
I am writing this post to ask you guys if you can help me on this very basic program .
I am still a student in programming and a beginner with arrays and i am asked to create a program that creates random elements of arrays between 0 and 9 , and inside every box of these arrays a random number between 0 and 100 and finally to calculate the sum of these numbers , example
6 random elements of an array are generated , inside each one of these elements a certain value is created , then i shall add these value and print the value.
Now as i said i am still a beginner in java so i know this program is messed up but at least i tried i hope you guys can help me , thank you :
Code java:

```public class arrays { public static void main(String[] args) {   double x = 0 ; double [] numbers = {x*(Math.random()*10)}; double result = sumLessThanAverage(numbers); System.out.println(result); } public static double sumLessThanAverage(double[]a){ double x = 0; double sum = 0; double count=0; x = x * Math.random()*101; for ( int i = 0 ; i<= a.length-1 ; i++){ a[i] = x; sum += x; x = x * Math.random()*101; count ++; } return sum;     } }```
• November 23rd, 2013, 06:16 AM
Norm
Re: Problem with arrays
[code=java]
[/code]
to get highlighting and preserve formatting.
• November 23rd, 2013, 07:41 AM
ChristopherLowe
Re: Problem with arrays
Code Java:

```double x = 0 ; double [] numbers = {x*(Math.random()*10)};```

What do you get when you multiply a number by 0?
• November 23rd, 2013, 08:07 AM
Re: Problem with arrays
Quote:

Originally Posted by ChristopherLowe
Code Java:

```double x = 0 ; double [] numbers = {x*(Math.random()*10)};```

What do you get when you multiply a number by 0?

Oh god , what a foolish mistake , thank you !
• November 23rd, 2013, 09:05 AM
ChristopherLowe
Re: Problem with arrays
All good. There are other problems here. Are you trying to create an array with 10 elements?

Code Java:

`double [] numbers = {(Math.random()*10)};`

This is creating an array with exactly one element. Instead, use this to create an array with a desired length:

Code Java:

```int length = 9; double[] numbers = new double[length]```
• November 23rd, 2013, 09:08 AM