By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

Three C/C++ Programming Questions

P: n/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);
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 10 Apr 2005 19:10:17 -0700, ha*********@hotmail.com (az) wrote in
comp.lang.c++:
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]

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #2

P: n/a
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
Jul 23 '05 #3

P: n/a
On 10 Apr 2005 19:10:17 -0700, ha*********@hotmail.com (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
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.