473,396 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Three C/C++ Programming Questions

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
3 1437
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: William | last post by:
I would appreciate your help on the following programming questions: 1. Given an array of length N containing integers between 1 and N, determine if it contains any duplicates. HINT: The...
5
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose...
1
by: Edward | last post by:
hi, I've some requirements about the data, Nations->States->Cities->Counties, four level. I want to give a friendly UI to customer, but I wasn't satisfied with all that I thought out, can...
31
by: anon856525 | last post by:
Hello I am seeking the best way (speed and portability) to program mode 13h (320 x 200 256 colors), and mode X. I am using Borland's Turbo C ver 3.0 for MS DOS. With Borland product, I can...
8
by: AAaron123 | last post by:
Dim FILENAME As String = Page.MapPath("something.txt") HomeDir = Directory.GetCurrentDirectory TextBox1.Text = File.ReadAllText(FILENAME, Encoding.UTF8) If I want to display text I use a...
14
by: Cowboy \(Gregory A. Beamer\) | last post by:
Top posting is also considered bad netiquette. -- Gregory A. Beamer MVP, MCP: +I, SE, SD, DBA Subscribe to my blog http://gregorybeamer.spaces.live.com/lists/feed.rss or just read it:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.