0

I am so close to finishing this program. It will find the median of an array of 5 values. I have one last error that I cannot seem to get to go away. Since I am new to C++, I have no idea of what the problem could be. I researched the error over and over on here and Google; no luck.

Here's the code:

#include <algorithm>
#include <functional>
#include <array>
#include <iostream>
using namespace std; 

int main()
{ 
    int integer1, integer2, integer3, integer4, integer5;

//Input of integers
std::cout << "Enter the first integer: "; 
std::cin >> integer1; 
std::cout << "Enter the second integer: "; 
std::cin >> integer2; 
std::cout << "Enter the third integer: "; 
std::cin >> integer3; 
std::cout << "Enter the fourth integer:";
std::cin >> integer4;
std::cout << "Enter the fifth integer:";
std::cin >> integer5;

std::array <int,5> a = {integer1, integer2, integer3, integer4, integer5}; 

//Sort array
std::sort(a.begin(), a.end());
for (int a : a) {
        std::cout << a << " ";
}

std::nth_element(a.begin(), a.begin()+1, a.size()/2, a.end());
std::cout <<"The median of the integers "<<integer1<<", "<<integer2<<", "<<integer3<<", "<<integer4<<", and "<<integer5<< " is " <<a[a.size()/2]<< '\n';
std::endl (std::cout);



return 0; 
}

The error states: "IntelliSense: no instance of overloaded function "std::nth_element" matches the argument list, argument types are: (std::_Array_iterator, std::_Array_iterator, unsigned int, std::_Array_iterator)

Help me finish this thing! Thanks in advance.

  • 1
    The error message says exactly what is wrong. The parameters you are passing do not match [any of the overloads](http://www.cplusplus.com/reference/algorithm/nth_element/). – Raymond Chen Mar 13 '14 at 04:10
  • Please elaborate. Break it down. – user3413585 Mar 13 '14 at 04:14
  • You are passing four parameters. The only four-parameter overload takes a comparator as the last parameter. Your fourth parameter is not a comparator. – Raymond Chen Mar 13 '14 at 04:17

2 Answers2

1

You misunderstand what nth_element does, and are trying to use it incorrectly.

This function takes a range which is not necessarily sorted, and partially sorts it such that nth element is in its correct place. If you ise this function to find a median, you don't need to sorf first.

If you already have a sorted range [first, last) then nth element of this range is pointed to by first + n.

n. 'pronouns' m.
  • 95,181
  • 13
  • 111
  • 206
  • So I could, for good programming practice, delete the entire sort function written above? And it would still work correctly? – user3413585 Mar 13 '14 at 04:33
  • Yes, you can delete it (and fix the call to `nth_element` obviously). – n. 'pronouns' m. Mar 13 '14 at 04:35
  • Thank you all for you help. The program is finished and operational. I started learning C++ about 1 month ago so I'll really appreciate these brain picking sessions. – user3413585 Mar 13 '14 at 04:40
0

I think you meant:

std::nth_element(a.begin(), a.begin()+a.size()/2, a.end()); 

Please refer to the c++ reference.

feihu
  • 1,767
  • 14
  • 24
  • That fixed the error. However, the cmd prompt closes immediately after the program finishes running. Any suggestions? – user3413585 Mar 13 '14 at 04:23
  • @user3413585, if you're using `visual studio`, please use `ctrl+F5` instead of `f5`. or you could add a `breakpoint` or `cin` statement at the end of the `main` function. – feihu Mar 13 '14 at 04:25
  • @user3413585, @n.m. is right, you don't need to call `sort` because `nth_element` would do it for you. – feihu Mar 13 '14 at 04:28
  • Thank you all for you help. The program is finished and operational. I started learning C++ about 1 month ago so I'll really appreciate these brain picking sessions. – user3413585 Mar 13 '14 at 04:40