Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7615501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:39:31+00:00 2026-05-31T02:39:31+00:00

I need help with a piece of code I’m writing. I want to compute

  • 0

I need help with a piece of code I’m writing. I want to compute the median of an array. Initially the types were all ints and it compouted the median with integer division which gave me a wrong answer. I changed everything to doubles but I’m getting a type mismatch error saying I can’t convert doubles to ints which doesn’t really make sense. Can someone help. This is my code:

public class MedianTest {

   public static void main(String[] args) {

      double median = 0;
      double myValues[] = { 1, 2, 3, 4 };

      double arrayHalfOdd = ((myValues.length - 1) / 2) + 1;
      double arrayHalfEven = ((myValues.length - 1) / 2);

      if ((myValues.length - 1) % 2 == 0) {
         median = (myValues[arrayHalfEven] + myValues[arrayHalfEven + 1]) / 2;
      } else {
         median = myValues[arrayHalfOdd];
      }
      System.out.print(median);
   }

}

the errors are in the myValues[arrayHalfEven] and myValues[arrayHalfOdd]

By the way this IS NOT homework

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:39:32+00:00Added an answer on May 31, 2026 at 2:39 am

    final answer:

        if (myValues.length == 0) {
            throw new IllegalStateException();
        }
        else if (myValues.length == 1) {
            return myValues[0];
        }
        else {
            int halfRoundedUp = myValues.length / 2;
            if (myValues.length % 2 == 0) { // even count, average the middle values
                return (myValues[halfRoundedUp - 1] + myValues[halfRoundedUp]) / 2;
            }
            else { // odd, take the middle value
                return myValues[halfRoundedUp];
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I still need your help. I have this piece of code that doesn't want
I need help with this piece of code. public class ParkingLot { static int
I need help with debugging this piece of code. I know the problem is
Thanks @dtb for help, he advised really need piece of code for generic service
Iam a perl newbie and need help in understanding the below piece of code.
I am writing a piece of code in c++ where in i need a
I need help with the following piece of code, I would like to see
I need some help about PHP GD. Here is my piece of code. header(Content-type:
Need help writing a script downloads data from google insight using c# this is
This is my piece of code.Im getting all the tables names and col names

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.