Connecting Tech Pros Worldwide Help | Site Map

keep splitting the atom question

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 25th, 2007, 10:35 AM
Angus
Guest
 
Posts: n/a
Default keep splitting the atom question

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?


  #2  
Old September 25th, 2007, 10:35 AM
Jim Langston
Guest
 
Posts: n/a
Default Re: keep splitting the atom question

"Angus" <anguscomber@gmail.comwrote in message
news:1190716119.694118.138910@o80g2000hse.googlegr oups.com...
Quote:
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.


  #3  
Old September 25th, 2007, 12:45 PM
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
Default Re: keep splitting the atom question

On 2007-09-25 12:28, Angus wrote:
Quote:
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
  #4  
Old September 25th, 2007, 12:55 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: keep splitting the atom question

Angus wrote:
Quote:
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"?
Quote:
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?
Quote:
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


  #5  
Old September 28th, 2007, 02:55 AM
Howard
Guest
 
Posts: n/a
Default Re: keep splitting the atom question

>
Quote:
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

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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.