Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to get Median/Middle value in an array?

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to get Median/Middle value in an array?

    So first of all we use textpad in our java course (idk why) and the Easy.In method for our input.


    anyway I have this assignment and we're ask to make a program that input 1) how many numbers were input 2) the largest value 3)smallest value 4) average value and 5) middle/median value. I manage to figure out the code for the other except the median value. So i'm here asking for help!

    here's my code

    class project2
    {
     public static void main(String[] args)
     {
    	 System.out.println("How many numbers you want to input");
    	 int maxItems = EasyIn.getInt();
     
    	 int[] numbers = new int[maxItems];
     
    	 for(int i = 0; i < maxItems; i++)
    	 {
    		 System.out.println("Enter the value of number " + (i+1));
    		 numbers[i] = EasyIn.getInt();
    	 }
     
    	 System.out.println("The total count of numbers you inputed is " +maxItems);
     
    	 findlargest(numbers);
    	 findsmallest(numbers);
    	 findaverage(numbers);
    	 findmiddle(numbers);
     
     }
     
     public static void findlargest(int[]nos)
     {
    	 int largest = 0;
     
    	 for(int i = 0; i < nos.length; i++)
    	 {
    		 if(nos[i] > largest)
     
    		 largest = nos[i];
    	 }
    	 System.out.println(" ");
    	 System.out.println("The largest value is " +largest);
     }
     
     public static void findsmallest(int[]nos)
     {
    	 int smallest = nos[0];
     
    	 for(int i = 0; i < nos.length; i++)
    	 {
    		 if(nos[i] < smallest)
     
    		 smallest = nos[i];
    	 }
    	 System.out.println(" ");
    	 System.out.println("The smallest value is " +smallest);
     }
     
     public static void findaverage(int[]nos)
     {
    	 double total = 0;
    	 double average;
     
    	 for(int i = 0; i < nos.length; i++)
    	 {
    		 total += nos[i];
    	 }
    	 average = total / nos.length;
    	 System.out.println(" ");
    	 System.out.println("The average value is " +average);
     }
     
     public static void findmiddle(int[]nos)
     
     
     
     
     
     
     
     
     
    {
     }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to get Median/Middle value in an array?

    Please post your code correctly.

    When I greeted you in January, I didn't ask you to read this topic to learn how to post code in code or highlight tags and other useful info for new members.

Similar Threads

  1. MEDIAN,(BY SORT ARRAY)
    By manahil in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 12th, 2013, 03:07 PM
  2. Quicksort with median-of-3 partitioning
    By ueg1990 in forum Algorithms & Recursion
    Replies: 0
    Last Post: October 27th, 2012, 03:19 PM
  3. Iterative quick sort using median of three values
    By omfgz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 30th, 2012, 09:59 PM
  4. Replies: 2
    Last Post: August 30th, 2012, 03:26 AM
  5. recursive program to find median
    By TeamRival in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 26th, 2011, 10:40 PM