Problem with arrays

• 11-25-2013, 12:49 AM
soniabobit
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 :

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;

}
}
• 11-25-2013, 10:44 AM
Levian
Re: Problem with arrays
Try to do it one step after another.
First is to create a random number elements, I think it means you need to create array size using random number
Second having the size known, create the array and instantiate it
Create random numbers and assign it as array members (for loop will do)
Now as you have the array (try to list it, like doing arrayName[x]), you can simply add them or it can also be done while assigning it

I hope this helps
• 11-25-2013, 03:29 PM
jim829
Re: Problem with arrays
First, you need to place your code between tags. I have done it for you here.

Code:

```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;         } }```
`double x = 0 ;double [] numbers = {x*(Math.random()*10)};`