1

I am kind of new in Java, and I got assigned some exercises. Any idea how to this small problem?

  1. create an array consisting of 100 random integers in the range 100 to 500, including the end points.
  2. make a method to print the array, 5 numbers per line, with a space between each.
  3. make a method to print the smallest number in the array.

So far this is what i got for the first 2 parts but i doesn't seem work, help please, sorry for asking such dumb questions...

package randomhundred;

import java.text.DecimalFormat;

public class RandomHundred {
    public static void main(String[] args) {
        //setting the 100 array
        int rand [] = new int [100];
        double numb;
        DecimalFormat dec = new DecimalFormat("0");
        for(int i=0; i<rand.length; i++){
            numb = Math.random() * (  500 - 100 );
        }  
    }

    public static int arai (){
        return System.out.print(" " + dec.format(numb) + " ");
    }    
}
home
  • 12,169
  • 5
  • 42
  • 54
abonini
  • 33
  • 10
  • 3
    Start by defining precisely what doesn't work, and how it doesn't work. Read any error message you get, and try to make sense of it. Google for the error message. And start with even more simple exercises, like printing a value to the console. Then filling an array and print its content to the console. Then generating one random number and printing it to the console. Then filling an array with random numbers, etc. – JB Nizet Nov 17 '13 at 17:52

3 Answers3

1

Let's get step 1 right.... generating 100 random values.

Your process is nearly there.... but:

  1. it does not generate an int value so you can't store it in an int[] array.
  2. it will never generate the value 500.

To convert the random number to an integer, try the following:

int numb;
....
    numb = (int)(Math.random() * (  500 - 100 ));

but, this will not generate the value 500 (because 500 - 100 is 400, but, there's actually 401 numbers you need to generate....), so change it to (see How do I generate random integers within a specific range in Java?):

   numb = 100 + (int)(Math.random() * (  (500 - 100)  + 1))

Now we have random numbers between 100 and 500 (inclusive), you need to store them in your array now:

rand[i] = numb;

Once you have that working, come back and we can tackle the other problems.

until then, you can print out your array with:

System.out.println(Arrays.toString(rand));  // Arrays is java.util.Arrays
Community
  • 1
  • 1
rolfl
  • 16,851
  • 6
  • 38
  • 72
  • It will also NOT generate numbers between 100 and 500, as specified by OP. Let's get step 1 **right**: int numb = **100** + (int)(Math.random() * 401); :D – Melquiades Nov 17 '13 at 18:24
  • Thanks... duh, of course. Even I forget sometimes in haste (I wouldn't forget in real life.... **promise** (scout's honor) – rolfl Nov 17 '13 at 18:25
  • :) No worries. Btw, you forgot to close the bracket in your comment (opened after "haste") ;p Ok ok, no need to edit the comment as well ;) – Melquiades Nov 17 '13 at 18:31
  • Thanx guys for the help, please look to my reply below! as in these comments you cant put any code that i want u guys to check! thanx a million! – abonini Nov 19 '13 at 19:29
0

I'll try to supply some pseudo code for your problem:

// Task 1
Declare array of 100 ints (I will relate to 100 later on with n). 
Fill array with random integers between 100 and 500 including borders

Hint code for this: 100 + new Random().nextInt(401), 401 as the upper bound is exclusive which makes the max result: 100 + 400 -> 500 and the minimum: 100 + 0 = 100. Even though this will generate the 100 numbers it will be faster if you store the random instance somewhere and re-use it, but that's an optimization step.

// Task 2
for i = 0; i < n; i++
    print integer from array
    add space
    if i is divisible by 5 then
        add new line
    end-if
end-for

//Task 3
declare a min of the maximum value which is possible: in this case your maximum random number of 500
loop through the list checking for smaller numbers
after loop has finished print the last number

I hope this is clear enough for you get an idea on how this could be done.

Johnnei
  • 568
  • 4
  • 9
0

Ad. 1 For the sake of completeness (it's been explained by others):

for(int i=0; i<rand.length; i++){
    rand[i] = 100 + (int)(Math.random() * (  401 ));
} 

Ad. 2 This is wrong:

public static int arai (){
    return System.out.print(" " + dec.format(numb) + " ");
} 

Return type is int, but System.out.print() doesn't return anything, so change int to void and remove return keyword. Next, you'll need to iterate through the whole array:

public static void arai (){
    for (int i=0; i< 100; i++) {
        //this is modulo, which means that if i divided by 5 has no remainder, print next line
        //eg. 5%2 = 1, because 2*2 + 1 = 5
        //5%3 = 2 and so on, read here http://en.wikipedia.org/wiki/Modulo_operation
        if (i%5 == 0) {
            System.out.println();
        }

        //finally, print each value from rand array
        System.out.print(rand[i] + " ");
    }
}

Ad. 3 Try yourself, and comeback if you have issues, but follow Johnnei's advice of finding the smallest number.

NOTE: you also need to make the rand array declaration global, so that it's available to arai() method.

Melquiades
  • 8,316
  • 1
  • 27
  • 40