    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Getting Started</title>
        <link>https://members.accu.org/index.php/articles/750</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">CVu Journal Vol 11, #1 - Nov 1998</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c134/">111</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Getting Started</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 November 1998 13:15:28 +00:00 or Tue, 03 November 1998 13:15:28 +00:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e16" id="d0e16"></a></h2>
</div>
<p>One of the problems that newcomers to programming have is in
sorting out their thoughts about a problem. Often the problem is an
exercise from a book or a tutor. These problems have a habit of
being very poorly specified. Initial specifications are often
(usually) deficient even in professional environments. Programmers
need to learn to refine initial specifications. A way of doing this
is by having a dialogue (in a professional environment, with your
client).</p>
<p>The problem that faces the newcomer is that it seems hard to
hold a dialogue with a book. However learning to interrogate the
question is a vital problem solving skill. I offer you the
following as an example.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e22" id="d0e22"></a>Initial
Programming Exercise:</h2>
</div>
<p>Write a program that will count the occurrences of characters in
a string and print out the most frequently used and the least
frequently used (that has been used at least once). Also print out
the frequency for these items.</p>
<p>The following dialogue is between the student and a mentor (that
mentor might in fact be the students alter ego, indeed for long
term progress it needs to be). I have distinguished between student
and mentor by the use of different typefaces.</p>
<div class="variablelist">
<dl>
<dt><span class="term">Student</span></dt>
<dd>
<p>Where do I start?</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>The beginning would seem a good place. Think about what
constitutes a string.</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>Letters? Numbers? Punctuation?</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>Yes. What are all those things called collectively?</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>Symbols?</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>But what represents a symbol in C?</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>A <tt class="type">char</tt>.</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>Good, but there are several kinds of <tt class="type">char</tt>.
Which should we use?</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>I know that there are <tt class="type">signed</tt> and
<tt class="type">unsigned</tt> ones but how do I choose?</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>Do you think the concept of a negative character makes
sense?</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>No. So I guess I should choose the <tt class=
"type">unsigned</tt> one.</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>How many different characters might there be in the string?</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>I have no idea.</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>Yes you have. Think about <tt class="type">unsigned char</tt>.
How many different symbols can it represent?</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>It all depends</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>On what?</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>Which codes make sense.</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>OK. Let us assume they all do. What is the largest number? Think
about limits.</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>You mean check <tt class="filename">limits.h</tt>? &hellip;
<tt class="literal">UCHAR_MAX</tt>.</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>Almost, but that is the value of the largest. Think about the
value of the smallest.</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>I see. We are counting from zero. So at most there may be
<tt class="literal">UCHAR_MAX+1</tt> different <tt class=
"type">unsigned char</tt>s.</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>Good. So we will need to be able to keep counts for up to that
number of characters. Does that give you any ideas?</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>What about an array of counters? Something like <tt class=
"literal">int counters[UCHAR_MAX+1]</tt>.</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>Why an <tt class="type">int</tt> array? Think about the nature
of a counter. What is the least value it can have?</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>You mean it would be better as:</p>
<pre class="programlisting">
unsigned int counters[UCHAR_MAX + 1];
</pre></dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>Yes. But where are we going to put that definition and how
should it be initialised?</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>What is wrong with it as it is?</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>Nothing until someone else uses your code, or until you recycle
it yourself in a few months time. As it is it is a global variable.
Despite all you may have been taught, global variables are very bad
news in modern code where several processes (threads) may be
running at the same time.</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>OK. Then it will have to be in a function. What about:</p>
<pre class="programlisting">
int main(void){
  unsigned int counters[UCHAR_MAX + 1];
}
</pre></dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>Well that is a start. But you need to initialise your counters.
Check out the rules and you will find that you can do better than
write a loop.</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>Yes, I remember that we can brace initialise an array to
zeroes.</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>Good. Now where is that string coming from?</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>I do not know. But I could delay the decision with a function.
Something like:</p>
<pre class="programlisting">
char * getString();
</pre>
<p>and use that for a data item in main()</p>
<pre class="programlisting">
char * data_string = getString();
</pre></dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>Excellent. Make a note that we will have to write <tt class=
"function">getString()</tt> and make sure that it returns a pointer
to a non-local object.</p>
<p>For the next part we will have to tie down the problem a little
further. What sort of characters are we going to count? All of them
or just letters?</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>I think that just letters was intended.</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>OK. But are we to distinguish upper and lower case?</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>Let us do it the hard way and say no.</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>Now we need to do some research. How do we identify letters? Be
careful because we do not want to miss the accented ones. And how
do we collect corresponding upper and lower case ones together. Go
and check out the Standard Library.</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>&hellip;OK, I think I have found what we need. isalpha() will
identify letters and tolower() will provide the code for lowercase
versions of uppercase letters.</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>Time to write a little more code. Write code to count the
letters from your string:</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>Like this:</p>
<pre class="programlisting">
int main(void){
  unsigned int counters[UCHAR_MAX + 1] = {0};
  char * data_string = getString();
  unsigned int iter;
  for(  iter = 0; 
      iter &lt;  strlen(data_string);
       ++iter){
    if (isalpha(data_string[iter]) {
      ++counters[tolower(data_string)];
    }
  }
  return 0;
}
</pre></dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>Good. You will need some header files, you will need to provide
a definition of <tt class="function">getString()</tt>. I like that
simple use of strlen() rather than trying to be clever. I also note
that someone has had the sense to tell you to pre-increment for
preference (that will work better if you write C++ code)</p>
<p>It is time to consider finding the least often used letter.
Think about what the largest value of this could be under the worst
circumstances (always a good idea to think about boundary
conditions).</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>The string might contain a single letter repeated. So the
largest possible value for the count of the least used letter (that
is a mouthful&#9786;) is strlen(data_string);</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>Exactly. Now just iterate through the counters lowering the
limit each time you find a lower but non-zero count.</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>Like:</p>
<pre class="programlisting">
unsigned int least=strlen(data_string);
for(iter=0; iter&lt;UCHAR_MAX+1; ++iter){
  if(counters[iter])
    if(counters[iter]&lt;least) {
      least = counters[iter];
    }
  }
}
</pre>
<p>And I could do the opposite for finding the most frequent.</p>
<pre class="programlisting">
unsigned int most = 0;
for(iter=0; iter&lt;UCHAR_MAX+1; ++iter){
  if(counters[iter])
    if(counters[iter]&lt;most){
       most = counters[iter];
    }
  }
}
</pre></dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>True. What would it mean if least stays as
strlen(data_string)?</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>That the string was all one letter.</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>Really? Any other possibility? What if if(counters[iter]) always
fails?</p>
</dd>
<dt><span class="term">Student</span></dt>
<dd>
<p>I see. If there were no letters in the string then the value of
least would never be changed. But neither would the value of most.
Hey that is useful. The only way that the value of most could be
zero is if there were no letters in the string. Perhaps we should
count those first.</p>
</dd>
<dt><span class="term">Mentor</span></dt>
<dd>
<p>Fine. Now see if you can go away and write your complete
program. It never said in the original spec but I think you should
print out all letters that are least frequent and all that are most
frequent.</p>
</dd>
</dl>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e394" id="d0e394"></a>The Final
Cut</h2>
</div>
<pre class="programlisting">
#include &lt;limits.h&gt;
#include &lt;stdio.h&gt;
#include &lt;ctype.h&gt;
#include &lt;string.h&gt;
char const * getString();
 int main(void){
  unsigned int counters[UCHAR_MAX + 1] = {0};
  char * data_string = getString();
  unsigned int iter;
  for(iter = 0; iter &lt;  strlen(data_string); ++iter){
    if (isalpha(data_string[iter]) {
      ++counters[tolower(data_string)];
    }
  }
  {  /* block to allow timely definitions of locals */
    unsigned int most = 0;
    unsigned int least = strlen(data_string);
    for(iter=0; iter&lt;UCHAR_MAX+1; ++iter){
      if(counters[iter])
        if(counters[iter]&lt;most) most 
                    = counters[iter];
      }
    }
    if(most == 0){
      puts(&quot;There are no letters in the string&quot;);
      return 0;      /* note early return */
    }
    for(iter=0; iter&lt;UCHAR_MAX+1; ++iter){
      if(counters[iter])
        if(counters[iter]&lt;least) least  
                    = counters[iter];
    }
    puts(&quot;The least used letters are:&quot;);
    for(iter=0; iter&lt;UCHAR_MAX+1; ++iter){
      if(counters[iter] == least){
        printf(&quot;%c, &quot;, iter);
      }
    }
    printf(&quot;\nEach used %d times.\n&quot;, least);
    puts(&quot;The most used letters are:&quot;);
    for(iter=0; iter&lt;UCHAR_MAX+1; ++iter){
      if(counters[iter] == most){
        printf(&quot;%c, &quot;, iter);
      }
    }
    printf(&quot;\nEach used %d times.\n&quot;, most);
  }
  return 0;
}
</pre>
<p>Of course there is still room for polishing. Ideally I would
like to see code extracted into functions where it can be reused
and otherwise maintained. But my purpose with this article was to
try to help those struggling to see how writing a program is a
stepwise process. Trying to tackle it all at once is far to
daunting. As you gain experience you work in larger steps. Some of
the steps above are large for the true novice but I have to give
some consideration to space and the boredom factor.</p>
<p>Remember that the biggest difference between any novice and a
Chess Grandmaster is the complexity of each step in their analysis
of a position.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
