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 8268831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:01:34+00:00 2026-06-08T06:01:34+00:00

How to calculate the median of a digital number array has been discussed before.

  • 0

How to calculate the median of a digital number array has been discussed before. For example, you can refer to What is the right approach when using STL container for median calculation?. Now I have a different question, and that is how can you get the index of the median in the original STL container. In order to illustrate my question, I give an example:

vector<int> myarray;
myarray.push_back(3);
myarray.push_back(1);
myarray.push_back(100);
myarray.push_back( 20);
myarray.push_back(200);
int n = myarray.size()/2;
nth_element(myarray.begin(), myarray.begin()+n, myarray.end());
int median = myarray[n];

In the above codes I can get the median value but I can not get its index in the original vector array (4). Any ideas? Thanks!

  • 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-06-08T06:01:35+00:00Added an answer on June 8, 2026 at 6:01 am

    If it is accapteble to search the element

    vector<int>::iterator itOfMedian = std::find(myarray.begin(), myarray.end(), median);
    int index = itOfMedian - myarray.begin();
    

    should do the trick.

    EDIT

    seems you have point here. nth_element sorts its argument vector… Therefore

    vector<int> myArrayCopy = myarray;
    // find median in myArrayCopy
    vector<int>::iterator itOfMedian = std::find(myarray.begin(), myarray.end(), median);
    int index = itOfMedian - myarray.begin();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number array and I'd like to calculate the median. When the
How can I calculate the date having only the week number and day of
We are using the following script to calculate the median: SELECT [Period] = 'amountPeriodA',
I'm trying to calculate the total, mean and median of an array thats populated
I have following (exponential) values and want to calculate their median using R: 1.0584e+00
I want to calculate the median of a frequency distribution for a large number
I am trying to calculate number of users, cumulatively for the dellstore2 database. Looking
Could you help to calculate the number of each variable where each variable =1?
I'm trying to calculate the median of a (small) set of NSNumbers in an
Possible Duplicate: How to calculate or approximate the median of a list without storing

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.