Results 1 to 8 of 8
Thread: Random Array with average
- 04-24-2012, 04:40 PM #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[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"); } }
Last edited by JosAH; 04-24-2012 at 06:12 PM. Reason: added [code] ... [/code] tags (with a purpose)
- 04-24-2012, 04:48 PM #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?How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 04-24-2012, 04:50 PM #3
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 301
- Rep Power
- 9
Re: Random Array with average
Please use code tags for posting code on the forum. ;)
I do not see a problem...
- 04-24-2012, 04:55 PM #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.
- 04-24-2012, 04:57 PM #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
- 04-24-2012, 05:13 PM #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...
- 04-24-2012, 06:07 PM #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.
- 04-24-2012, 06:14 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: Random Array with average
Two remarks:
1) your indentation sucks (that's why I added those 'code' tags)
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,
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads
-
Average of an array. Please help
By vika in forum New To JavaReplies: 1Last Post: 03-28-2011, 10:09 PM -
2 dimensional array, average student
By aborgeld in forum Advanced JavaReplies: 5Last Post: 03-05-2011, 04:12 PM -
Can't get correct average using array
By nevets93 in forum New To JavaReplies: 2Last Post: 02-11-2011, 11:33 AM -
Array of random numbers ...
By shane1987 in forum New To JavaReplies: 31Last Post: 11-14-2010, 10:33 PM -
How to get a random value in an array
By Franneldort in forum New To JavaReplies: 21Last Post: 11-01-2008, 03:42 PM
Bookmarks