Random Array with average

• 04-24-2012, 03:40 PM
keki
Random Array with average
Hi,
I am a relative newbie on JAVA programming but willing to improve.
I have a few queries on a small program that I need to build part of an assignment.

The problem is:
1. Program to populate array of 10,000 random numbers (range 0 to 500).
2. Then find average of these array nos and print it.
3. Then print all numbers together with their positions higher than the average.

I have managed to arrive to part 2 but am finding it difficult to make part 3.
Below is the sourcecode.

Appreciate for any help or guidance. Willing to learn more than copy/paste.
Code:

```import java.io.*; import java.util.*; import java.lang.Math.*; class Arrayrnd {         public static void main (String[] args) throws IOException                 {                 int sum = 0;                 int average = 0;                 Random r = new Random();                                 int arr[] = new int[10000];                                 for(int i = 0; i < 10000; i++){                                   arr[i] = r.nextInt(500) + 1;                // Random numbers within range 0 to 500.                                 }                                 for(int i = 0; i < 10000; i++){                                   System.out.print(arr[i] + " ");                                 sum += arr[i];                                // Calculate and print the average.                                 average = sum / 10000;                 }                 System.out.println("\n\n\nAverage is " + average + ".\n");                 int count = 0;                                                // Count the number of values that were above average.                 for (int i = 0; i < 10000; i++){                     if (arr[i] > average) {                 count++;                                 }             }         System.out.println(count + " instances were above average. \n");                         }                 }```
• 04-24-2012, 03:48 PM
KevinWorkman
Re: Random Array with average
When posting code, please use the code tags to preserve formatting.

What about this is giving you trouble? You say it's difficult to make part 3, but what about it confuses you? What's the very next thing you know you need to do?
• 04-24-2012, 03:50 PM
Sierra
Re: Random Array with average
Please use code tags for posting code on the forum. ;)
I do not see a problem... :(grin):
• 04-24-2012, 03:55 PM
keki
Re: Random Array with average
I am finding it difficult how to call the numbers that are higher than average and print them.
Should I use loops?

I know that I need to find all the instances where the random number is higher than the average number.
The problem is how to make a statement that keeps on going and prints the numbers with their positions.
• 04-24-2012, 03:57 PM
keki
Re: Random Array with average
Sierra, sorry about the code tags.. new to this forum :)
I am aware that the program works well with the above source code, its just that I cannot figure out how to continue.
thanks
• 04-24-2012, 04:13 PM
Diargg
Re: Random Array with average
You're already counting the number that are above average - how are you determining that they're above average? That might give you both the specific number and location...
• 04-24-2012, 05:07 PM
keki
Re: Random Array with average
thanks for the help, I have managed to print out the list of numbers higher than average. Now only need to print them with their positions.
• 04-24-2012, 05:14 PM
JosAH
Re: Random Array with average
Two remarks: