recursion

Discussion in 'C Programming' started by oliver, Aug 16, 2007.

  1. oliver

    oliver Guest

    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
     
    oliver, Aug 16, 2007
    #1
    1. Advertising

  2. On Aug 16, 12:00 pm, oliver <> wrote:
    > 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
     
    Robert Gamble, Aug 16, 2007
    #2
    1. Advertising

  3. oliver

    Flash Gordon Guest

    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.
    --
    Flash Gordon
     
    Flash Gordon, Aug 16, 2007
    #3
  4. oliver <> wrote:
    > 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
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Aug 16, 2007
    #4
  5. oliver

    oliver Guest

    On 16 Aug, 17:56, Flash Gordon <> wrote:
    > 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.
    > --
    > Flash Gordon


    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.
     
    oliver, Aug 16, 2007
    #5
  6. oliver

    Flash Gordon Guest

    oliver wrote, On 16/08/07 18:18:
    > On 16 Aug, 17:56, Flash Gordon <> wrote:
    >> 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.
    >> --
    >> Flash Gordon

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


    <snip>

    > 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.
    --
    Flash Gordon
     
    Flash Gordon, Aug 16, 2007
    #6
  7. oliver

    oliver Guest

    On 16 Aug, 19:19, Flash Gordon <> wrote:
    > oliver wrote, On 16/08/07 18:18:
    >
    >
    >
    > > On 16 Aug, 17:56, Flash Gordon <> wrote:
    > >> 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.
    > >> --
    > >> Flash Gordon

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

    >
    > <snip>
    >
    > > 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.
    > --
    > Flash Gordon


    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 :)
     
    oliver, Aug 16, 2007
    #7
  8. oliver

    user923005 Guest

    On Aug 16, 9:00 am, oliver <> wrote:
    > 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 news:comp.programming
    I am guessing that a web search will turn up a BOM program in any
    language that you like.
     
    user923005, Aug 16, 2007
    #8
  9. oliver

    Al Balmer Guest

    On Thu, 16 Aug 2007 10:18:18 -0700, oliver <>
    wrote:

    >On 16 Aug, 17:56, Flash Gordon <> wrote:
    >> 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.
    >> --
    >> Flash Gordon

    >
    >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?

    --
    Al Balmer
    Sun City, AZ
     
    Al Balmer, Aug 16, 2007
    #9
  10. oliver

    Al Balmer Guest

    On Thu, 16 Aug 2007 11:36:50 -0700, oliver <>
    wrote:

    >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.

    --
    Al Balmer
    Sun City, AZ
     
    Al Balmer, Aug 16, 2007
    #10
  11. oliver

    Default User Guest

    oliver wrote:


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


    This is an easy *plonk*.



    Brian
     
    Default User, Aug 16, 2007
    #11
  12. oliver

    Richard Guest

    "Default User" <> writes:

    > oliver wrote:
    >
    >
    >> Thanks for your unhelpful responses - what a waste of your time and
    >> mine.

    >
    > 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....
     
    Richard, Aug 16, 2007
    #12
  13. On Thu, 16 Aug 2007 10:18:18 -0700, in comp.lang.c , oliver
    <> wrote:

    >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
     
    Mark McIntyre, Aug 16, 2007
    #13
  14. oliver

    John Bode Guest

    On Aug 16, 11:00 am, oliver <> wrote:
    > 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.
     
    John Bode, Aug 16, 2007
    #14
  15. oliver

    oliver Guest

    On 16 Aug, 22:57, John Bode <> wrote:
    > On Aug 16, 11:00 am, oliver <> wrote:
    >
    >
    >
    >
    >
    > > 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
     
    oliver, Aug 17, 2007
    #15
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Mohler
    Replies:
    1
    Views:
    450
    Steve Grazzini
    Sep 16, 2003
  2. B McInnes

    recursion with perl

    B McInnes, Nov 1, 2003, in forum: Perl
    Replies:
    4
    Views:
    4,139
    B McInnes
    Nov 4, 2003
  3. Chris

    Recursion Query

    Chris, May 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    447
    avnrao
    May 17, 2004
  4. Iain
    Replies:
    3
    Views:
    1,829
    Robbe Morris [C# MVP]
    Mar 9, 2006
  5. Replies:
    8
    Views:
    761
    John Reye
    Apr 26, 2012
Loading...

Share This Page