By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,215 Members | 1,922 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,215 IT Pros & Developers. It's quick & easy.

keep splitting the atom question

P: n/a
Hello

I am writing a sampling type program which takes a buffer, and picks
the middle of the buffer. But I need to take a bit out the middle and
then take what is left (on both sides) and take the middle out of
these fragments.

I have a function which takes a range, eg start, end and a samplesize
and calculates the range of the buffer to take for sampling. But of
course each time I call this function it creates two new start-end
ranges to again get the middle bit again. I have a size of sample so
when the middle bits reach the sample size I just stop.

How would I write something to achieve this? Should it be a recursive
function?

Sep 25 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Angus" <an*********@gmail.comwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
Hello

I am writing a sampling type program which takes a buffer, and picks
the middle of the buffer. But I need to take a bit out the middle and
then take what is left (on both sides) and take the middle out of
these fragments.

I have a function which takes a range, eg start, end and a samplesize
and calculates the range of the buffer to take for sampling. But of
course each time I call this function it creates two new start-end
ranges to again get the middle bit again. I have a size of sample so
when the middle bits reach the sample size I just stop.

How would I write something to achieve this? Should it be a recursive
function?
I'd probably do it recursively. This is more a programming question than a
C++ quesiton though.
Sep 25 '07 #2

P: n/a
On 2007-09-25 12:28, Angus wrote:
Hello

I am writing a sampling type program which takes a buffer, and picks
the middle of the buffer. But I need to take a bit out the middle and
then take what is left (on both sides) and take the middle out of
these fragments.

I have a function which takes a range, eg start, end and a samplesize
and calculates the range of the buffer to take for sampling. But of
course each time I call this function it creates two new start-end
ranges to again get the middle bit again. I have a size of sample so
when the middle bits reach the sample size I just stop.

How would I write something to achieve this? Should it be a recursive
function?

Recursive is probably a good idea. However for these kinds of questions
please consult comp.programming instead.

--
Erik Wikström
Sep 25 '07 #3

P: n/a
Angus wrote:
I am writing a sampling type program which takes a buffer, and picks
the middle of the buffer. But I need to take a bit out the middle
What does that mean "to take a bit out the middle"?
and
then take what is left (on both sides) and take the middle out of
these fragments.
And then what? When does it end, and what do you expect as the result
of that processing?
I have a function which takes a range, eg start, end and a samplesize
and calculates the range of the buffer to take for sampling. But of
course each time I call this function it creates two new start-end
ranges to again get the middle bit again. I have a size of sample so
when the middle bits reach the sample size I just stop.

How would I write something to achieve this? Should it be a recursive
function?
Yes, the recursion seems to fit right in.

template<class Iterator, class F>
void doSomethingWithTheMiddle(Iterator start, Iterator end, F f)
{
if (start != end) { // not an empty range
Iterator middle = start + (start - end)/2;
f(*middle);
doSomethingWithTheMiddle(start, middle);
doSomethingWithTheMiddle(middle, end);
}
}

The function above has a flaw -- it will recurse infinitely for a range
of length 1. You need to correct it. Hint: you need to add 1 addition
somewhere.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 25 '07 #4

P: n/a
>
template<class Iterator, class F>
void doSomethingWithTheMiddle(Iterator start, Iterator end, F f)
{
if (start != end) { // not an empty range
Iterator middle = start + (start - end)/2;
Did you mean:

= start + (end-start)/2; // which equals (start+end)/2

?
f(*middle);
doSomethingWithTheMiddle(start, middle);
doSomethingWithTheMiddle(middle, end);
}
}
-Howard

Sep 28 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.