Three C/C++ Programming Questions

Discussion in 'C++' started by az, Apr 11, 2005.

  1. az

    az Guest

    Can anyone help me to answer the following questions? Thanks a lot!

    1) Given the following code snippet, what does the function DoesWhat()
    do? And what, if any, assumptions are made about the input to the
    function.

    struct _tagElement
    {
    int m_cRef;
    unsigned char m_bData[20];
    struct _tagElement * m_next;

    } NODE, * PNODE;

    PNODE DoesWhat (PNODE pn1, PNODE pn2)
    {
    PNODE * ppnV = &pn1;
    PNODE * ppn1 = &pn1;
    PNODE * ppn2 = &pn2;

    for ( ; *ppn1 || *ppn2; ppn1 = &((*ppn1)->m_next))
    {
    if (!(*ppn1) || (0 < memcmp((*ppn1)->m_bData,
    (*ppn2)->m_bData, sizeof((*ppn2)->m_bData))))
    {
    PNODE pn = *ppn1;
    *ppn1 = *ppn2;
    pn2 = (*ppn2)->m_next;
    (*ppn1)->m_next = pn;
    }
    }
    return *ppnV;
    }

    2) What are the problems with the quality of the previous code and how
    would you make it better? Please try to find as many problems as you
    can.

    3) Write the code for the function below. Parameters data and filter
    are null-terminated strings. Scan removes all occurrences of the
    string filter from the string data.

    int scan(char *data, char *filter);
     
    az, Apr 11, 2005
    #1
    1. Advertising

  2. az

    Jack Klein Guest

    Jack Klein, Apr 11, 2005
    #2
    1. Advertising

  3. az

    Artie Gold Guest

    az wrote:
    > Can anyone help me to answer the following questions? Thanks a lot!


    If it sounds like homework....
    >
    > 1) Given the following code snippet, what does the function DoesWhat()
    > do? And what, if any, assumptions are made about the input to the
    > function.
    >
    > struct _tagElement
    > {
    > int m_cRef;
    > unsigned char m_bData[20];
    > struct _tagElement * m_next;
    >
    > } NODE, * PNODE;
    >
    > PNODE DoesWhat (PNODE pn1, PNODE pn2)
    > {
    > PNODE * ppnV = &pn1;
    > PNODE * ppn1 = &pn1;
    > PNODE * ppn2 = &pn2;
    >
    > for ( ; *ppn1 || *ppn2; ppn1 = &((*ppn1)->m_next))
    > {
    > if (!(*ppn1) || (0 < memcmp((*ppn1)->m_bData,
    > (*ppn2)->m_bData, sizeof((*ppn2)->m_bData))))
    > {
    > PNODE pn = *ppn1;
    > *ppn1 = *ppn2;
    > pn2 = (*ppn2)->m_next;
    > (*ppn1)->m_next = pn;
    > }
    > }
    > return *ppnV;
    > }
    >


    ....and it walks like homework...

    > 2) What are the problems with the quality of the previous code and how
    > would you make it better? Please try to find as many problems as you
    > can.


    ....and it smells like homework...

    >
    > 3) Write the code for the function below. Parameters data and filter
    > are null-terminated strings. Scan removes all occurrences of the
    > string filter from the string data.
    >
    > int scan(char *data, char *filter);


    ....it's homework.

    Please send us you instructor's address -- and appropriate payment (many
    small unmarked bills, of course) -- and we'll take care of it for you.

    Seriously though, tell us what *you* think, show us *your* work and you
    *will* get valuable assistance.

    HTH,
    --ag
    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)
    http://www.cafepress.com/goldsays
     
    Artie Gold, Apr 11, 2005
    #3
  4. az

    Kanenas Guest

    On 10 Apr 2005 19:10:17 -0700, (az) wrote:

    >Can anyone help me to answer the following questions?


    Your TA.

    Seriously, though, one approach to question 1 is to write a test
    framework. Create functions to create NODEs, print NODEs, then apply
    DoesWhat and print the result. The output should give you a clue what
    DoesWhat does. Also try stepping through DoesWhat with a debugger.
    As your programming skills improve, you'll eventually be able to skip
    the framework and model the behavior of a function in your head.

    Once you know what DoesWhat does (and what it's supposed to do),
    you'll be able to tackle question 2.

    For question 3, take a look at bcopy and the family of C string
    functions, which includes strcpy (and the safer strncpy & strlcpy),
    strcat, and strcmp. An implementation of "scan" using these functions
    is potentially not the most efficient, but will get the job done.

    There are some potential "gotchas" when using the C string functions,
    so read up on them. On Unix systems, you can read about these
    functions in their manual pages; try "man strcpy" at a command prompt
    or use a manual browser (like xman). You can also find the man pages
    on the web using Google; just search for (e.g.) "man strcmp". The man
    pages you find online will probably be for systems other than your
    own, but the semantics of the str* functions they describe should
    follow the standard. Lcc-win32 includes a Windows HTML help file
    which covers the C string functions, as well as the Lcc-win32
    implementation of the standard C library.
    http://www.cs.virginia.edu/~lcc-win32/
    Your system may include its own documentation on the C string
    functions (and the rest of the standard C library).

    Seriously, though, if you have questions then ask the TA.

    Kanenas
    --
    Kanenas
     
    Kanenas, Apr 12, 2005
    #4
    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. Adam White
    Replies:
    10
    Views:
    1,904
    Joerg Jooss
    Nov 3, 2004
  2. Marcin Misiewicz
    Replies:
    1
    Views:
    1,941
    zoopy
    Aug 19, 2004
  3. Rocket Scientist

    three questions

    Rocket Scientist, Jan 2, 2005, in forum: HTML
    Replies:
    13
    Views:
    655
    Edwin van der Vaart
    Jan 11, 2005
  4. richardv2
    Replies:
    2
    Views:
    463
    David Graham
    Jan 21, 2006
  5. Richard Jones
    Replies:
    0
    Views:
    178
    Richard Jones
    Mar 5, 2010
Loading...

Share This Page