Click here to Skip to main content
15,915,094 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: strtol and errno Pin
bob1697227-Apr-08 8:54
bob1697227-Apr-08 8:54 
QuestionAcclelerated C++ Chapter 8 Exercises - Don't Understand Question Pin
oliversimon26-Apr-08 5:49
oliversimon26-Apr-08 5:49 
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question Pin
Saurabh.Garg26-Apr-08 18:18
Saurabh.Garg26-Apr-08 18:18 
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question Pin
oliversimon27-Apr-08 2:09
oliversimon27-Apr-08 2:09 
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question [modified] Pin
Saurabh.Garg27-Apr-08 2:49
Saurabh.Garg27-Apr-08 2:49 
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question Pin
oliversimon27-Apr-08 4:16
oliversimon27-Apr-08 4:16 
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question Pin
Saurabh.Garg27-Apr-08 6:54
Saurabh.Garg27-Apr-08 6:54 
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question Pin
oliversimon27-Apr-08 7:12
oliversimon27-Apr-08 7:12 
Hi Saurabh, Smile | :)

OK here's the code I've knocked together as a solution:

#include "stdafx.h"
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <vector>
using namespace std;

template<typename inputiterator>
typename InputIterator::value_type median(InputIterator begin, InputIterator end)
{
vector<typename> v;
typedef typename vector<typename>::size_type vec_sz;

//After the while loop, below, begin == end so can't use the while loop again.
//So chuck everything in a vector and work on that
//Does this save processing over simply passing a vector into
// the template?
//Is this the way it should be done??
//Who knows - all I know is it works and I can finally move on
//Some people do this for fun, you know...

while (begin != end)
{
v.push_back(*begin);
begin++;
}

vec_sz size = v.size();
if (size == 0)
throw domain_error("median of an empty vector");

sort(v.begin(),v.end());
vec_sz mid = size/2;
return size % 2 == 0 ? (v[mid] + v[mid-1]) / 2 : v[mid];
}

int main()
{
vector <int> v2;
v2.push_back(3);v2.push_back(13);v2.push_back(7);v2.push_back(5);v2.push_back(21);v2.push_back(23);
v2.push_back(23);v2.push_back(40);v2.push_back(23);v2.push_back(14);v2.push_back(12);
v2.push_back(56);v2.push_back(23);v2.push_back(29);

//median of v2 shuld be 22. See http://www.mathsisfun.com/median.html
cout << "The median of the int vector v2 is " << median(v2.begin(),v2.end())<< endl;
system ("pause");
return 0;
}

The only niggle is that after using the while loop to get the size, 'begin' equalled 'end' and so I couldn't iterate through again to find the mid point. In the end I created a vector and put everything in there. This kind of made sense but I can't help feeling I'm missing something by using another vector... perhaps there is a way to reset the iterators after they have been used once (i.e. in the while loop)

Cheers

Si
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question Pin
Saurabh.Garg27-Apr-08 7:57
Saurabh.Garg27-Apr-08 7:57 
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question Pin
oliversimon27-Apr-08 8:15
oliversimon27-Apr-08 8:15 
GeneralRe: Acclelerated C++ Chapter 8 Exercises - Don't Understand Question Pin
Saurabh.Garg27-Apr-08 8:25
Saurabh.Garg27-Apr-08 8:25 
Questionchange the program's entry point function Pin
LiYS26-Apr-08 3:56
LiYS26-Apr-08 3:56 
GeneralRe: change the program's entry point function Pin
Saurabh.Garg26-Apr-08 18:30
Saurabh.Garg26-Apr-08 18:30 
Questionfont problem Pin
trioum26-Apr-08 1:19
trioum26-Apr-08 1:19 
GeneralRe: font problem Pin
Hamid_RT26-Apr-08 4:16
Hamid_RT26-Apr-08 4:16 
GeneralRe: font problem Pin
trioum27-Apr-08 22:01
trioum27-Apr-08 22:01 
GeneralRe: font problem Pin
phanindra varma28-Apr-08 0:33
phanindra varma28-Apr-08 0:33 
GeneralRe: font problem Pin
Hamid_RT28-Apr-08 5:04
Hamid_RT28-Apr-08 5:04 
QuestionIs MSDN wrong? Pin
zengkun10026-Apr-08 1:09
zengkun10026-Apr-08 1:09 
GeneralRe: Is MSDN wrong? Pin
Mukesh Kumar26-Apr-08 2:26
Mukesh Kumar26-Apr-08 2:26 
GeneralRe: Is MSDN wrong? Pin
zengkun10026-Apr-08 2:42
zengkun10026-Apr-08 2:42 
GeneralRe: Is MSDN wrong? Pin
ThatsAlok1-Jul-09 0:02
ThatsAlok1-Jul-09 0:02 
GeneralRe: Is MSDN wrong? Pin
CPallini26-Apr-08 2:48
mveCPallini26-Apr-08 2:48 
GeneralRe: Is MSDN wrong? Pin
zengkun10026-Apr-08 2:54
zengkun10026-Apr-08 2:54 
GeneralRe: Is MSDN wrong? Pin
CPallini26-Apr-08 4:12
mveCPallini26-Apr-08 4:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.