Results 1 to 5 of 5
  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Working on the following problem !

    Hello,

    I have written a code to find a middle element from a sorted array. Here is some part of my code:

    Java Code:
    int[] a = {1,2,3,4,5};
    
    int x = a.length;
    		if (x % 2 != 0){
    			
    			int mid = 0 + x/2;
    			
    			System.out.println("The Middle Element is : " + a[mid]);
    		}
    I'm getting the output properly but wondering what should I write in the else condition of the code above? In other words, what should I do to find a middle element if a.length is even? Is this possible?

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Working on the following problem !

    you could either:

    Display the elements either side of the middle point, so use the same code you have but add this:
    Java Code:
    int mid = 0 + x/2;
    int beforMid = mid - 1;
    int afterMid = mid +1;
                 
                System.out.println("The Middle Elements are : " + a[beforeMid] + a[afterMid]);
    Or you can add the 2 "middle" values together and divide by 2:

    Java Code:
    int mid = 0 + x/2;
    int beforMid = mid - 1;
    int afterMid = mid +1;
    
    double m = (a[beforeMid]+a[afetrMid]) / 2
                System.out.println("The average of middle elements is : " + m)[B][/B]
    up to you how you want to do it,

    You could even just print an error message saying there is no exact middle value This isnt very user friendly though :D

  3. #3
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Re: Working on the following problem !

    I see. Thanks for your valuable input.
    Quote Originally Posted by monkeyjr97 View Post
    you could either:

    Display the elements either side of the middle point, so use the same code you have but add this:
    Java Code:
    int mid = 0 + x/2;
    int beforMid = mid - 1;
    int afterMid = mid +1;
                 
                System.out.println("The Middle Elements are : " + a[beforeMid] + a[afterMid]);
    Or you can add the 2 "middle" values together and divide by 2:

    Java Code:
    int mid = 0 + x/2;
    int beforMid = mid - 1;
    int afterMid = mid +1;
    
    double m = (a[beforeMid]+a[afetrMid]) / 2
                System.out.println("The average of middle elements is : " + m)[B][/B]
    up to you how you want to do it,

    You could even just print an error message saying there is no exact middle value This isnt very user friendly though :D

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,512
    Blog Entries
    7
    Rep Power
    20

    Default Re: Working on the following problem !

    That code is not correct; example: there are 6 (even) elements in your array; you should consider the elements 2 and 3 (not elements 2 and 4).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Working on the following problem !

    Ah yes, i do apologize, rookie mistake to make. Sorry :(

Similar Threads

  1. Replies: 10
    Last Post: 04-13-2012, 09:48 PM
  2. Replies: 3
    Last Post: 12-22-2011, 12:22 PM
  3. Problem working out Assignment
    By maknib in forum New To Java
    Replies: 12
    Last Post: 03-14-2011, 09:50 PM
  4. Problem with .jar working with netbeans
    By chipimdp in forum NetBeans
    Replies: 1
    Last Post: 05-14-2010, 09:25 AM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 AM

Posting Permissions

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