Results 1 to 4 of 4
  1. #1
    Drask is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default 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[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.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default 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

  3. #3
    Drask is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: Help with arrays

    Quote Originally Posted by wsaryada View Post
    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.

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Help with arrays

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

Similar Threads

  1. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 01:49 PM
  2. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 02:28 PM
  3. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  5. Need help with 2D arrays...
    By rrsv2 in forum New To Java
    Replies: 3
    Last Post: 11-30-2008, 04:15 AM

Posting Permissions

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