# Help with arrays

• 03-30-2012, 04:10 AM
Help with arrays
Hello there,

I have an assigment due tomorrow and I can't find a solution. What I need to do is create an array of 20, the numers will be generated randomly (with the Math.random class) and will be multiplied by 100, and round them to the floor. so I will get 20 numers from 1 to 100. then, I will sum evey numer higher than 49 and then divide it into the amount of times it was added. My code does everything but the last part, it doesnt divide only by the numers that were higher than 49 but rather by all the array.length (20).

Code:

```public class ConsolaApp {     /**     * @param args the command line arguments     */     public static void main(String[] args) {                         double array1[];         array1= new double[20];         int suma = 0,dividido =0;         for (int i=0;i<array1.length;i++)         {             array1[i]= new Double(Math.random()*100);             double redondeado = Math.floor(array1[i]);             //System.out.println("Numero random generado: "+array1[i]+" Redondeado: "+redondeado);                     if (redondeado>49)             {                                 System.out.println((i+1)+".-"+redondeado);                 suma= (int) (suma+array1[i])/array1.lenght;                                           }                     }                  System.out.println(suma);                     } }```
As I said it adds prefecly only the numbers higher than 49 but it doesnt divide just by them but rather by 20 (the lenght of the array)
Thanks in advance, sorry for the bad English, not my native language.
• 03-30-2012, 06:20 AM
Re: Help with arrays
Here is a couple things from me:

1. In the if block, the suma should be suma + redondeado, because you only want to add number if it is bigger that 49. And why you divide it with the length of the array there?
2. In the if block you also have to count how many number that greater that 49 was there, didn't you create the dividido variable for this purpse?
3. And finally outside the for loop you can divide the suma with dividido.

I hope I understand your problem correctly :D
• 03-30-2012, 06:48 AM
Re: Help with arrays
Quote: