Results 1 to 8 of 8
  1. #1
    keki is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default 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 05:12 PM. Reason: added [code] ... [/code] tags (with a purpose)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default 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 - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Random Array with average

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

  4. #4
    keki is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default 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.

  5. #5
    keki is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default 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

  6. #6
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default 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...

  7. #7
    keki is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default 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.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Average of an array. Please help
    By vika in forum New To Java
    Replies: 1
    Last Post: 03-28-2011, 09:09 PM
  2. 2 dimensional array, average student
    By aborgeld in forum Advanced Java
    Replies: 5
    Last Post: 03-05-2011, 03:12 PM
  3. Can't get correct average using array
    By nevets93 in forum New To Java
    Replies: 2
    Last Post: 02-11-2011, 10:33 AM
  4. Array of random numbers ...
    By shane1987 in forum New To Java
    Replies: 31
    Last Post: 11-14-2010, 09:33 PM
  5. How to get a random value in an array
    By Franneldort in forum New To Java
    Replies: 21
    Last Post: 11-01-2008, 02:42 PM

Posting Permissions

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