1. Member Join Date
Mar 2012
Posts
2
Rep Power
0

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

PHP Code:
```public class ConsolaApp {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

double array1[];
array1= new double;
int suma = 0,dividido =0;
for (int i=0;i<array1.length;i++)
{
array1[i]= new Double(Math.random()*100);

{

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.  Reply With Quote

2. Senior Member Join Date
Jun 2007
Location
Bali, Indonesia
Posts
762
Rep Power
13

## 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  Reply With Quote

3. Member Join Date
Mar 2012
Posts
2
Rep Power
0

## Re: Help with arrays Originally Posted by wsaryada 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
The sum is not the problem, it does exactly what I need, it actually just add the numers higher than 49, about the point 2, thats my question about, how do I count how many numers are higher than 49 and save it in a variable, yes I created that 'dividido' variable for that but I don't know how to assing it the numer of numers that were higher than 49.  Reply With Quote

4. Senior Member Join Date
Jun 2007
Location
Bali, Indonesia
Posts
762
Rep Power
13

## Re: Help with arrays

You can increment the dividido each time you found the number is greater that 49 in your if block.  Reply With Quote

#### Posting Permissions

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