Three C/C++ Programming Questions

A

az

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);
 
J

Jack Klein

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

Only if you include your instructors email address, so we can send in
the answers for you.

[obvious homework snipped]
 
A

Artie Gold

az said:
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
 
K

Kanenas

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
 

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,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top