You are here:

# C++/recursive splitting

Question
Hello!
I would like to ask you a question if you have a little bit of time. I have the following table:
1,1,4,6,7,8
1,2,3,7,8,9
1,2,5,6,7,8
2,3,4,5,6,7
2,4,6,7,8,9

I need to write a program that determines how many rows I have that start with 1, how many that start with 1,2 how many start with 1,2,3; 1,2,4;.....; 1,3,4;....; 2,2; 2,2,3 .... etc. etc. In other works I need to know all these possible combinations. I think this can be done iteratively with a bunch of for loops but it is a bit non-elegant. I have heard that recursive method is a little better. I have a book that talks about a simple recursive method (so I know what it is in theory) but I cannot get it to work on this table splitting. I guess I don't know whow to start. Do you think you would be able to help me to get going with this approach? Or maybe you'll convince me that recursive method is not the way to go.
I'd be hugely thankful to you! -Eric

Hi Eric,

I don't understand the question - I don't see the pattern in the prefix strings you want to count.  You've listed the pattern, but what is the problem if the data were generic?  I ask this because you can't define the prefixes to count giving an example.

I'm not sure a see an algorithm suited for recursion.  Generally in programming, simple is more elegant than complicated.

The rule I use for recursion is, if I can apply an algorithm to part of the data, and then the same algorithm can be used for the rest, then it's a good choice to use. I've found over the many years of professional programming that recursion is quite rare.

Bill

C++

Volunteer

#### Bill A

##### Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

##### Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering