recursion

O

oliver

I'd like to code something recursively - it's not going to be in C
(VBA actually) but I hope this is the best place to ask.
I have a Bill of Materials structured like this:

row description
1 main assembly
2 .subassembly (of 1)
3 ..subassembly (of 2)
4 .subassembly (of 1)
5 ..subassembly (of 4)
6 ...subassembly (of 5)
7 .subassembly (of 1)

and I need to turn this into a table that looks like this
row parent
1 -
2 1
3 2
4 1
5 4
6 5
7 1

The (of 1, of 2) in the descritpions don't actually exist in the data,
the dots preceeding the description tell you that.

Ok so please help - I've been chipping away at this for hours and it's
so long since I did my degree I've gone a bit dopey and can't see the
wood from the trees. I "KNOW" the most elegant way to handle this is
recursively I just can't figure out how today.

Thanks very much - really appreciate your input
Olly
 
R

Robert Gamble

I'd like to code something recursively - it's not going to be in C
(VBA actually) but I hope this is the best place to ask.

[snip]

I don't know what makes you think that a C group is the best place to
ask questions about how to program a VBA app, I would suggest starting
with a group that has "vba" in its name. When you do it will also
probably be helpful to include the code that you have come up with so
far.

Robert Gamble
 
F

Flash Gordon

oliver wrote, On 16/08/07 17:00:
I'd like to code something recursively - it's not going to be in C
(VBA actually) but I hope this is the best place to ask.

<snip>

I seriously cannot think why you thought this would be a good place to
ask when you are explicitly not using C. I'm sure you will find groups
in the microsoft hierarchy for VBA, or for general algorithms work there
is comp.programming.
 
J

Jens Thoms Toerring

oliver said:
I'd like to code something recursively - it's not going to be in C
(VBA actually) but I hope this is the best place to ask.
I have a Bill of Materials structured like this:
row description
1 main assembly
2 .subassembly (of 1)
3 ..subassembly (of 2)
4 .subassembly (of 1)
5 ..subassembly (of 4)
6 ...subassembly (of 5)
7 .subassembly (of 1)
and I need to turn this into a table that looks like this
row parent
1 -
2 1
3 2
4 1
5 4
6 5
7 1
The (of 1, of 2) in the descritpions don't actually exist in the data,
the dots preceeding the description tell you that.
Ok so please help - I've been chipping away at this for hours and it's
so long since I did my degree I've gone a bit dopey and can't see the
wood from the trees. I "KNOW" the most elegant way to handle this is
recursively I just can't figure out how today.

Since this is more a question about an algorithm and not about C
(which you don't even want to use) I would recommend that you ask
in e.g. comp.programming. But anyway, this looks like a nice ex-
ample where one could employ a stack:

a) read in a line
b) If the number of dots is smaller or equal to the depth
of the stack pop elements from the stack until the depth
of the stack is one less than the number of dots.
c) Print out the number of the line and the top-most number
on the stack (or '-' if the stack is empty). Push the line
number onto the stack.
d) goto a)
Regards, Jens
 
O

oliver

oliver wrote, On 16/08/07 17:00:


<snip>

I seriously cannot think why you thought this would be a good place to
ask when you are explicitly not using C. I'm sure you will find groups
in the microsoft hierarchy for VBA, or for general algorithms work there
is comp.programming.

Thanks for your unhelpful responses - what a waste of your time and
mine.
I posted on a C group because I used this board many years ago when I
was heavily involved in C programming and found the people helpful and
knowledgable.
I have no code because I can't get my head round the recursive side of
it, a solution I imagine will only be 10 or so lines long.
The language I implement this in, bar possibly the need for it to be
procedural, is absoloutely irrelevant!!!
I can simply port any C code to VBA. C is my language of choice and
an answer in C will be easiest for me to read.

I do take your point on board that a more generic .programming group
my help and will post there too.
 
F

Flash Gordon

oliver wrote, On 16/08/07 18:18:
Thanks for your unhelpful responses - what a waste of your time and
mine.

You chose to waste every bodies time by posting to what was obviously
the wrong group.
I posted on a C group because I used this board many years ago when I

Then how come you don't know that this is not a board but a news group
and don't know to snip peoples signatures?
was heavily involved in C programming and found the people helpful and

I do take your point on board that a more generic .programming group
my help and will post there too.

So my post did not waste your time since it pointed you to a more
appropriate group.

Now we are don't to just you wasting peoples time.
 
O

oliver

oliver wrote, On 16/08/07 18:18:





You chose to waste every bodies time by posting to what was obviously
the wrong group.


Then how come you don't know that this is not a board but a news group
and don't know to snip peoples signatures?




So my post did not waste your time since it pointed you to a more
appropriate group.

Now we are don't to just you wasting peoples time.

Hehe, that's more like it - a bit of bite - although it's not helping
me with my code prob. Thanks for the stack pointer - anyone got other
ideas?

Soooooo Mr Flash... I won't comment on your use of English on the
assumption it's not your first language. I have no idea what you mean
by 'snip signatures'...if you had any tradition and age behind you
then you'd appreciate calling this a board is hardly a serious faux
pas.

Here is appropriate in terms of I am certain there are people that
read this that knowledgeable enough to cut a little code to get me
started....maybe you are ----- who knows :)
 
U

user923005

I'd like to code something recursively - it's not going to be in C
(VBA actually) but I hope this is the best place to ask.
I have a Bill of Materials structured like this:

row description
1 main assembly
2 .subassembly (of 1)
3 ..subassembly (of 2)
4 .subassembly (of 1)
5 ..subassembly (of 4)
6 ...subassembly (of 5)
7 .subassembly (of 1)

and I need to turn this into a table that looks like this
row parent
1 -
2 1
3 2
4 1
5 4
6 5
7 1

The (of 1, of 2) in the descritpions don't actually exist in the data,
the dots preceeding the description tell you that.

Ok so please help - I've been chipping away at this for hours and it's
so long since I did my degree I've gone a bit dopey and can't see the
wood from the trees. I "KNOW" the most elegant way to handle this is
recursively I just can't figure out how today.

Thanks very much - really appreciate your input

In C, structs that contain structs is the simple, obvious answer.
In SQL, it is tables with child tables.
Your question is better posed in I am guessing that a web search will turn up a BOM program in any
language that you like.
 
A

Al Balmer

Thanks for your unhelpful responses - what a waste of your time and
mine.

Yep, that's what we're telling you. Your question has to do with
algorithms, not the C language. Two of the three replies advised you
to go to comp.programming. Why were they not helpful responses?

"I'm sorry, but this is Rochester, NY. The Mayo Clinic is in
Rochester, MN." Happens several times a year. Is that an unhelpful
response?
 
A

Al Balmer

Here is appropriate in terms of I am certain there are people that
read this that knowledgeable enough to cut a little code to get me
started....maybe you are ----- who knows :)

Of course there are, but no one here is likely to do it for you.
That's not why we're here. If you want to try to write a program, in
C, and have problems, come on back. You'll get help from the best.

Perhaps you really need alt.sources.wanted.

Some other information: This isn't a bulletin board, and it's not
Google. Using the Google interface, you've found your way to a Usenet
newsgroup. "Signatures", or "sig blocks" are the text that you see
following the signature separator, which is two dashes and a space, on
a line by itself. You'll see one below. Traditionally, newsgroups are
accessed using a newsreader program, and proper newsreader programs
strip signature blocks on replies.
 
R

Richard

Default User said:
oliver wrote:



This is an easy *plonk*.

Impressive. Do keep us informed on who you plonk and it would also be
beneficial to us all if you reply about 30 times to OT messages in order
to reduce the noise in this NG.

Sheesh....
 
M

Mark McIntyre

Thanks for your unhelpful responses - what a waste of your time and
mine.

Frankly - you started it.
I posted on a C group because I used this board many years ago when I
was heavily involved in C programming and found the people helpful and
knowledgable.

I find the people over in rec.motorcycles helpful and knowledgeable
too, but I'm not going to ask them for recipes for fairy cakes. :)
I do take your point on board that a more generic .programming group
my help and will post there too.

Its your best bet I think.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
J

John Bode

I'd like to code something recursively - it's not going to be in C
(VBA actually) but I hope this is the best place to ask.
I have a Bill of Materials structured like this:

row description
1 main assembly
2 .subassembly (of 1)
3 ..subassembly (of 2)
4 .subassembly (of 1)
5 ..subassembly (of 4)
6 ...subassembly (of 5)
7 .subassembly (of 1)

and I need to turn this into a table that looks like this
row parent
1 -
2 1
3 2
4 1
5 4
6 5
7 1

The (of 1, of 2) in the descritpions don't actually exist in the data,
the dots preceeding the description tell you that.

Ok so please help - I've been chipping away at this for hours and it's
so long since I did my degree I've gone a bit dopey and can't see the
wood from the trees. I "KNOW" the most elegant way to handle this is
recursively I just can't figure out how today.

Why do you KNOW a recursive solution is the most elegant? Because I
just came up with a non-recursive solution that works pretty well and
is reasonably straightforward:

int rownum = 1;
int stack[STACK_SIZE];
char record[RECORD_SIZE];

stack[0] = rownum;

printf("%8s%8s\n", "row", "parent");
printf("%8s%8s\n", "---", "------");
while (fgets(record, sizeof record, data))
{
int parent = 0;
int level = getLevel(record); //getLevel counts leading '.'
if (level > 0)
{
parent = stack[level - 1];
stack[level] = rownum;
}
printf("%%8d%8d\n", rownum, parent);
rownum++;
}

Should be enough to get you started, anyway.
 
O

oliver

I'd like to code something recursively - it's not going to be in C
(VBA actually) but I hope this is the best place to ask.
I have a Bill of Materials structured like this:
row description
1 main assembly
2 .subassembly (of 1)
3 ..subassembly (of 2)
4 .subassembly (of 1)
5 ..subassembly (of 4)
6 ...subassembly (of 5)
7 .subassembly (of 1)
and I need to turn this into a table that looks like this
row parent
1 -
2 1
3 2
4 1
5 4
6 5
7 1
The (of 1, of 2) in the descritpions don't actually exist in the data,
the dots preceeding the description tell you that.
Ok so please help - I've been chipping away at this for hours and it's
so long since I did my degree I've gone a bit dopey and can't see the
wood from the trees. I "KNOW" the most elegant way to handle this is
recursively I just can't figure out how today.

Why do you KNOW a recursive solution is the most elegant? Because I
just came up with a non-recursive solution that works pretty well and
is reasonably straightforward:

int rownum = 1;
int stack[STACK_SIZE];
char record[RECORD_SIZE];

stack[0] = rownum;

printf("%8s%8s\n", "row", "parent");
printf("%8s%8s\n", "---", "------");
while (fgets(record, sizeof record, data))
{
int parent = 0;
int level = getLevel(record); //getLevel counts leading '.'
if (level > 0)
{
parent = stack[level - 1];
stack[level] = rownum;
}
printf("%%8d%8d\n", rownum, parent);
rownum++;
}

Should be enough to get you started, anyway.- Hide quoted text -

- Show quoted text -

I did it like this in the end
k = START_ROW
While Cells(k, INDENTURE_COL) = 0
For i = k To lngMaxRow
If Cells(i, INDENTURE_COL) > 0 Then
intThisIndent = Cells(i, INDENTURE_COL)
If intThisIndent > j Then
j = j + 1
aryParent(j) = Cells(i - 1, INDEX_COL)
Else
j = intThisIndent
End If
l = l + 1
Sheets("Hierarchy").Cells(l, 1) = Cells(i, INDEX_COL)
Sheets("Hierarchy").Cells(l, 2) = aryParent(j)
End If
Next i
k = k + 1
Wend
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top