Pseudocode

Pseudocode - Pseudocode Pseudocode is usually a more...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Pseudocode Pseudocode is usually a more effective means of communicating an algorithm than a narrative paragraph. Pseudocode should not be merely a statement-by-statement rephrasing of the code — how would that be any clearer than the code itself? For example, if we had to describe the algorithm for finding the average length of the words in a string as implemented by this code: ... int totLength = 0; int nWords = 0; for (size_t pos = 0; ; ) { // find start of word while (pos != s.size() && ! isalpha(s.at(pos))) pos++; // if no word, break if (pos == s.size()) break; size_t start = pos; // find end of word do { pos++; } while (pos != s.size() && isalpha(s.at(pos))); totLength += pos - start; nWords++; } if (nWords == 0) cout << "There are no words in the string" << endl; else cout << "The average word length is " << static_cast(totLength) / nWords << endl; ... a suitable pseudocode rendition would be:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
... repeatedly:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/07/2009 for the course CS 31 taught by Professor Melkanoff during the Spring '00 term at UCLA.

Page1 / 3

Pseudocode - Pseudocode Pseudocode is usually a more...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online