# Thread: Random Array with average

1. Member Join Date
Apr 2012
Posts
6
Rep Power
0

## 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.
Java 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;

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");

}
}```
Last edited by JosAH; 04-24-2012 at 05:12 PM. Reason: added [code] ... [/code] tags (with a purpose)  Reply With Quote

2. ## 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?  Reply With Quote

3. AN21XX Join Date
Mar 2012
Location
Munich
Posts
298
Rep Power
9

## Re: Random Array with average

Please use code tags for posting code on the forum. ;)
I do not see a problem...   Reply With Quote

4. Member Join Date
Apr 2012
Posts
6
Rep Power
0

## 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.  Reply With Quote

5. Member Join Date
Apr 2012
Posts
6
Rep Power
0

## 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  Reply With Quote

6. Senior Member Join Date
Feb 2012
Posts
117
Rep Power
0

## 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...  Reply With Quote

7. Member Join Date
Apr 2012
Posts
6
Rep Power
0

## 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.  Reply With Quote

8. ## Re: Random Array with average

Two remarks:

2) why are you calculating the average over and over again? Only the last time in the loop the average will be correct. Move that line just below the loop.

kind regards,

Jos  Reply With Quote

#### Posting Permissions

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