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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
yes.. ive compiled and run it..
u might have forgotton to add
import java.util.Arrays;
what errors are u getting ?
-KuTtZ
u might have forgotton to add
import java.util.Arrays;
what errors are u getting ?
-KuTtZ
ASKER
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
but seriously, if you could just walk me through this with some comments, thats all I still need.
-PKM
ASKER
i awarded the points, because your code works if i only understood it, I would be set...
:-)
PKM
:-)
PKM
Please find my comments inline..
public static void main(String[] args) {
int[] rndArray = generateNrndNumbersbetween XandY(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("Smalle st num : " + rndArray[0]);
// The last element of the sorted array will be the largest int
System.out.println("Larges t 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("Averag e : " + (sum/ rndArray.length));
}
public static int[] generateNrndNumbersbetween XandY(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
public static void main(String[] args) {
int[] rndArray = generateNrndNumbersbetween
// 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("Smalle
// The last element of the sorted array will be the largest int
System.out.println("Larges
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("Averag
}
public static int[] generateNrndNumbersbetween
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
ASKER
thanks man... you are the best... I will look this over, and my best wishes to you.
-PKM
-PKM
ASKER
Arrays.sort(rndArray);
Did you try compiling this, b/'c that line is causing problems...
-Panos