1. Member
Join Date
Nov 2013
Posts
1
Rep Power
0

## 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;

}
}

2. Senior Member
Join Date
Jun 2011
Posts
109
Rep Power
0

## 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

3. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
3,411
Rep Power
5

## Re: Problem with arrays

First, you need to place your code between tags. I have done it for you here.

Java 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;

}
}```
Java Code:
`double x = 0 ;double [] numbers = {x*(Math.random()*10)};`
All that does is allocate a double array of size 1 and set the first element to 0. Your method will always return 0 since x starts out as 0.

Regards,
Jim
Last edited by jim829; 11-25-2013 at 02:35 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•