| | |
Psuedocode
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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.
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
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?
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
C++ Syntax (Toggle Plain Text)
for( t= 0; s1[ t]; t++)
Are you talking about Herbert Schildt's C - The Complete Reference?
The key to eliminating bugs from your code is learning from your mistakes.
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)
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)
C++ Syntax (Toggle Plain Text)
int find_ substr( char *s1, char *s2) { register int t; char *p, *p2; for( t= 0; s1[t]; t++) // Move character by character thru the source string // start at the first character t=0 and s1[t] // until the current character s1[t] is 0 (FALSE) { p = &s1[t]; // p becomes the address of the current character p2 = s2; // p2 becomes the search string // Following tests to see if the current position in the source string and the seach string // matches... while (*p2 && // Continue while *p2 is not 0 (TRUE) -- end of string not found *p2 == *p) // and source and search characters are identical { p++; // skip to the next character p2++; // in both strings } if (!*p2) return t; // out of loop -- if the current character in the seach string // is 0, we have a match. Return the substript of the start // of the sub-string } return -1; // substring not found }
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.
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.
•
•
•
•
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.
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.
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.
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { cout << "Hello, World!"; }
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? 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.
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.
![]() |
Similar Threads
- Writing pseudo-codes/developing flowcharts (Computer Science)
- Need help with CPU scheduling algorithms (C++)
- I need a Pseudocode or advice here,Please help!! (C++)
- I need advices for the Pseudocode (JavaScript / DHTML / AJAX)
- Round Robin Scheduler (C)
- How can I write this program. (C++)
- please help me i am a programming newbie (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Help me copy this array.
- Next Thread: Multi-character support
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






