Results 1 to 4 of 4
Thread: Help with arrays
- 03-30-2012, 04:10 AM #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).
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)PHP 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); } }
Thanks in advance, sorry for the bad English, not my native language.
- 03-30-2012, 06:20 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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 :DWebsite: Learn Java by Examples
- 03-30-2012, 06:48 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
Re: Help with arrays
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.
- 03-30-2012, 06:51 AM #4
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Help with arrays
You can increment the dividido each time you found the number is greater that 49 in your if block.
Website: Learn Java by Examples
Similar Threads
-
Casting Enum Type arrays to object type arrays
By nmvictor in forum Advanced JavaReplies: 4Last Post: 02-17-2012, 12:49 PM -
arrays and multidimensional arrays
By belfast09 in forum New To JavaReplies: 5Last Post: 06-14-2011, 01:28 PM -
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Need help with 2D arrays...
By rrsv2 in forum New To JavaReplies: 3Last Post: 11-30-2008, 03:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks