Link to home
Start Free TrialLog in
Avatar of marcoullisp
marcoullisp

asked on

Having problems with a specific kind of java problem...

Most of java seems pretty straight forward to me. I am in the learning process right now, and am currently stumped on one specific java question, namely:

1. how do i generate 10 random numbers.
2. then place the random numbers in an array.
3. then print out only the highest value, the lowest value and the average values.

I have been working on this off and on for a few days now, and could use some help.

Thanks,

PKM
ASKER CERTIFIED SOLUTION
Avatar of arun_kuttz
arun_kuttz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of marcoullisp
marcoullisp

ASKER

I'm getting errors... hold on let me see if I can clean it up... on first glance there is a problem with:

Arrays.sort(rndArray);

Did you try compiling this, b/'c that line is causing problems...

-Panos
yes.. ive compiled and run it..
u might have forgotton to add

import java.util.Arrays;

what errors are u getting ?

-KuTtZ
never mind i figured it out it was the import you just mentioned... it runs fine, I just don't get what you did... and you know what they say... teach a man to fish...

but seriously, if you could just walk me through this with some comments, thats all I still need.

-PKM
i awarded the points, because your code works if i only understood it, I would be set...

:-)

PKM
Please find my comments inline..


public static void main(String[] args) {
        int[] rndArray = generateNrndNumbersbetweenXandY(100, 30, 80);
// sort() is a very usefull API provided by the Arrays utility class
// it sorts the given array in place..
        Arrays.sort(rndArray);
// The first element of the sorted array will be the smallest int
        System.out.println("Smallest num : " + rndArray[0]);
// The last element of the sorted array will be the largest int
        System.out.println("Largest num : " + rndArray[rndArray.length - 1]);
        float sum = 0;
// Here i find the sum of all the integers in the Array returned by my function
        for (int i = 0; i < rndArray.length; i++) {
            sum += rndArray[i];
        }
// i print out the Average (sun/rndArray.length)
        System.out.println("Average : " + (sum/ rndArray.length));
    }


    public static int[] generateNrndNumbersbetweenXandY(int n, int x, int y) {
        int[] retVal = new int[n];
// create a Random object
        Random rnd = new Random();
        for (int i = 0; i < n; i++) {
// The rnd Object has an API nextInt() which takes an int (lets say i) parameter...
// It will return a random integer between 0(inclusive) and i(exclusive)
// so what i did was get a random integer between 0 and (y-x) and add x to it
// i then keep populating the integer array that ive created with the rand integer
// returned by this API.
            retVal[i] = rnd.nextInt(y - x) + x;
        }
        return retVal;
    }



regards
-KuTtZ
thanks man... you are the best... I will look this over, and my best wishes to you.

-PKM