Psuedocode

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2006
Posts: 16
Reputation: VinC is an unknown quantity at this point 
Solved Threads: 0
VinC's Avatar
VinC VinC is offline Offline
Newbie Poster

Psuedocode

 
1
  #1
Sep 2nd, 2006
I'm self-teaching myself C with a complete reference book. Unfortunately that means that the author assumes that the reader is familiar with fundamental concepts and doesn't bother commenting the code. Here is an unnecessarily obfuscated code excerpt demonstrating "the use of return statements" along with my attempt to understand what is going on. Please list any corrections.

int find_ substr( char *s1, char *s2)
{
register int t;
char *p, *p2;
for( t= 0; s1[ t]; t++) {
p = &s1[ t];
p2 = s2;
while(* p2 && *p2==* p) {
p++;
p2++;
}
if(!* p2) return t; /* substring was found */
}
return -1; /* substring not found */
}

So pointers s1 and s2 are passed into "find substring." s2 is the substring we are searching for and s1 is the string we are searching through. As the function cycles through each character of p (equivalent to s1) the letters are compared to p2. When a letter match is found, the while loop checks for a complete match. (ie: you might be searching for digging but you only find dig. If that happens the orange statement is false and we're back at the for loop) If there is a complete match, p2 (the string we're searching through) is finite, the pointer arithmetic goes out of bounds and null is returned, bringing you out of the loop and into the for. The pointer p2 is still out of pounds and the if statement evaluates to false, but the ! changes this to true so t is returned. If the substring is not in the string we are searching through, the while and for loops exits and the -1 is returned.


One statement that confuses me in particular is the loop condition in
  1. for( t= 0; s1[ t]; t++)
Does this cycle through every element of any array? What happens if one of your elements happens to have a value of 0? will you break out of the loop or does it continue on until you are out of bounds and returns null? Since there is no bounds checking in C, the compiler will not stop you from writing more elements than allocated at the risk of overwriting other memory, is it possible that the memory cells wlil be contiguous and maybe you will continue looping past the 'last' element of the array?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: Psuedocode

 
1
  #2
Sep 2nd, 2006
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Psuedocode

 
1
  #3
Sep 2nd, 2006
You've mostly got it.

Each C 'string' ends in a character value of 0. That's what your "out of bounds" is really checking, really it's "end of string.

Commented (and formatted properly)
  1. int find_ substr( char *s1, char *s2)
  2. {
  3. register int t;
  4. char *p, *p2;
  5. for( t= 0; s1[t]; t++) // Move character by character thru the source string
  6. // start at the first character t=0 and s1[t]
  7. // until the current character s1[t] is 0 (FALSE)
  8. {
  9. p = &s1[t]; // p becomes the address of the current character
  10. p2 = s2; // p2 becomes the search string
  11.  
  12. // Following tests to see if the current position in the source string and the seach string
  13. // matches...
  14. while (*p2 && // Continue while *p2 is not 0 (TRUE) -- end of string not found
  15. *p2 == *p) // and source and search characters are identical
  16. {
  17. p++; // skip to the next character
  18. p2++; // in both strings
  19. }
  20. if (!*p2) return t; // out of loop -- if the current character in the seach string
  21. // is 0, we have a match. Return the substript of the start
  22. // of the sub-string
  23. }
  24. return -1; // substring not found
  25. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 16
Reputation: VinC is an unknown quantity at this point 
Solved Threads: 0
VinC's Avatar
VinC VinC is offline Offline
Newbie Poster

Re: Psuedocode

 
1
  #4
Sep 2nd, 2006
Originally Posted by Grunt View Post
Are you talking about Herbert Schildt's C - The Complete Reference?
Yes that is the book I am using. I am going for the very thorough approach (ruling out all the "how to learn to program in 10 days") The first couple hundred pages were manageable but I find the example code to be very difficult to understand simply because Schildt uses code and concepts that aren't covered until much later in the book. I would of also liked to have seen some sort of exercises with solutions. There is a lot of material to digest so I'll probabbly end up rereading the entire text anyways. I will undoubately run into many more examples which I won't fully understand but that's why I come here.:cheesy:

That being said, I find the book to be fairly well written, it was just never intended for newbies such as myself. Experienced programmers in other languages, programmers who are moving on from C to C++, or programmers who just need to review all the features of C and C++ would find Schildt's book useful. If any of you are interested I would definately recommend the electronic version over the text though. At over 1000 pages I can only imagine how unwieldly the text must be.... The search function is invaluable.
Last edited by VinC; Sep 2nd, 2006 at 4:13 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: Psuedocode

 
1
  #5
Sep 2nd, 2006
I guess you didn't read the link which I gave. Get a better book. Don't let me point out the stupid mistakes of the author.
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 16
Reputation: VinC is an unknown quantity at this point 
Solved Threads: 0
VinC's Avatar
VinC VinC is offline Offline
Newbie Poster

Re: Psuedocode

 
1
  #6
Sep 2nd, 2006
Originally Posted by Grunt View Post
I guess you didn't read the link which I gave. Get a better book. Don't let me point out the stupid mistakes of the author.
Damn.

Do you have any recommendations or should I pick a text from one of the stickies?

I'm actually not sure whether I should start with C, jump into C++ or bother learning the language at all. (mainly because of the massive time commitment)

I know I will be learning C++ for the next four years in university. But I also know you shouldn't blindly accept what school teaches you as Gospel. Countless articles on reddit talk are about how Java sucks, schools that teach it put out mediocre hackers, etc. etc.

I don't know how many programs are still being written in C, but for all I know C could become archaic by the time I graduate.... As useful and powerful as C is, (Lisp too is I'm told, but good luck finding a job requiring Lisp coders) I guess I'm lucky this time in that I only wasted two weeks on a bad book, but I don't want to find out two years into my program that I've run in the wrong direction. Any tips or advice?
Last edited by VinC; Sep 2nd, 2006 at 4:49 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: Psuedocode

 
1
  #7
Sep 2nd, 2006
Thinking in C++, Vol-1, Bruce Eckel or Accelerated C++, Andrew keonig and Barbara Moo
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,623
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Psuedocode

 
1
  #8
Sep 2nd, 2006
For absolute beginners, i would recommend "C++ in 21 days" and if you want to move ahead from the beginner stuff i would recommend "C++ programming language".

Hope it helped, bye.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 489
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Psuedocode

 
1
  #9
Sep 3rd, 2006
Originally Posted by VinC View Post
I'm actually not sure whether I should start with C, jump into C++ or bother learning the language at all. (mainly because of the massive time commitment)

I know I will be learning C++ for the next four years in university. But I also know you shouldn't blindly accept what school teaches you as Gospel. Countless articles on reddit talk are about how Java sucks, schools that teach it put out mediocre hackers, etc. etc.
If you're considering learning C purely as a grounding for C++, then please save yourself the wasted effort and skip 'C'. the two languages sometimes look similar at first glance (C++ inherited alot of the C syntax), but they are fundamentally completely different languages - even the "Hello World" programs you learn to write on day 1 for each language are completely different. (You've probably already written a "Hello World" in C, so here's a quick example in C++ to contrast)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. cout << "Hello, World!";
  7. }

I'd like to backup Grunt's reccomendations for Bruce Eckel's Thinking in C++, and Accelerated C++ by Koenig & Moo. Both excellent beginner's books. Also, C++ Primer by Lajoe, Lippman & Moo comes highly reccomended for beginners.

Francis Glassborow also has a book written for people with no prior programming or computer science knowledge, called You Can Do It. that's also an excellent book, but the compiler & libraries included with the book only work on Windows. (So learning from the book on any other OS would be tricky)
Last edited by Bench; Sep 3rd, 2006 at 10:10 am.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 16
Reputation: VinC is an unknown quantity at this point 
Solved Threads: 0
VinC's Avatar
VinC VinC is offline Offline
Newbie Poster

Re: Psuedocode

 
1
  #10
Sep 3rd, 2006
Thank you for all the recommendations. I've started on Bruce Eckel's "Thinking in C" and skimmed through "Thinking in C++"

Both are available on www.mindview.net and can be downloaded free of charge, though a hardcopy of the latter can be purchased.

"Thinking in C" is a visual-audio presentation that explores the basic features of C in preparation of learning Java and/or C++. I'm still working through the chapters and I'll share my impressions but at the moment I have nothing but good things to say.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC