    <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  :: Code Critique Competition</title>
        <link>https://members.accu.org/index.php/articles/883</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">Student Code Critiques from CVu journal. + CVu Journal Vol 11, #4 - Jun 1999</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/c184/">Journal Columns</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c183/">Code Critique</a>
<br />

                                            <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/c131/">114</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c183-131/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c183+131/">All of these categories</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;Code Critique Competition</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 June 1999 13:15:31 +01:00 or Thu, 03 June 1999 13:15:31 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e20" id="d0e20"></a></h2>
</div>
<p>We can often learn from the mistakes and struggles of novices. I
am often surprised when I discover things I always knew are untrue,
and novice code is a rich source for such insights. Rather than
leave all the work (and consequential benefits) to myself and The
Harpist I have decided to provide regular problem code from genuine
novices for your inspection.</p>
<p>Your task is a very simple one. Read the following, study the
code and then provide as helpful a response as you can. The result
will be judged for clarity of explanation as well as accuracy in
identifying problems. There will be a prize for the entry that I
consider would best meet the needs of the original questioner.
Rewriting the code to make it clean and intelligible is certainly
desirable.</p>
<p>By the way, the code is exactly as it was originally presented
except that I corrected a couple of spelling mistakes in the
comments.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e28" id="d0e28"></a>The
Problem</h2>
</div>
<p>I want my program to do this:</p>
<p>You should be able to add your forename, surname and finally
your personal number. If you input 0 as your personal number you
will stop adding more people to the register.</p>
<p>My problem is that my sorting routine does not work.</p>
<p>I want to sort by the personal numbers and display the results
on the screen.</p>
<p>so....</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>What is wrong with my sorting routine?</p>
</li>
<li>
<p>How should I construct a clean stop when I don't want to add
more people to the register (I want to use 0 as last personal
number)</p>
</li>
</ol>
</div>
<p>I know there are a lot of errors in the code besides this
one&hellip;but I'm a beginner and need time to develop a program
that work at all , but right now I just want this to work .</p>
<pre class="programlisting">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;conio.h&gt;    
            //Not standard in C i know...:-)
#include &lt;stdlib.h&gt;
#include &lt;io.h&gt;

//Prototyper
void lasin();
void meny();
void sortera();
void avsluta();
void utskrift();

typedef struct{
char pnumber[15];
char forname[10];
char lastname[15];
} perspost;

int main(void){
meny();
getch();
return 0;
}

void meny(){
int val;
clrscr();
printf(&quot;  MENY &quot;);
puts(&quot;&quot;);
puts(&quot;&quot;);
printf(&quot;1......Add people to the register\n&quot;);
printf(&quot;2......print\n&quot;);
printf(&quot;Val:&quot;);
scanf(&quot;%d&quot;,&amp;val);
switch(val){
case 1: lasin();
break;
case 2: sortera();
break;
 }
}

void lasin(){
perspost preg;
FILE *my_fil;
char namn[10];
int i=0,ptr=1;
clrscr();
printf(&quot;filename(must be named 'fil.txt' :-)? :&quot;);
scanf(&quot;%s&quot;,namn);
my_fil=fopen(namn,&quot;wb&quot;);
fseek(my_fil,0,SEEK_SET);
clrscr();

while(ptr!=0){
printf(&quot;personnummer(personalnumber): &quot;);
 scanf(&quot;%s&quot;,preg.pnumber);
 ptr=atoi(preg.pnumber);
 if(ptr==0)
 meny();
 printf(&quot;F&ouml;rnamn(forname): &quot;);
 scanf(&quot;%s&quot;,preg.forname);
 printf(&quot;Efternamn(lastname: &quot;);
 scanf(&quot;%s&quot;,preg.lastname);

 fseek(my_fil,i*sizeof(perspost),SEEK_SET);
 fwrite(&amp;preg ,sizeof(perspost),1,my_fil);
 i++;
}
fclose(my_fil);

}

/*Mabe i wont have to open  the file agein??
Something is wrong here!!??   */
void sortera(){
FILE *my_fil;
int sf,ft,storlek,k,m;
perspost pos1,pos2,tmp;
my_fil=fopen(&quot;fil.txt&quot;,&quot;rb&quot;);

fseek(my_fil,0,SEEK_END);
ft=ftell(my_fil);
sf=sizeof(perspost);
storlek=ft/sf;

 for(k=1;k&lt;storlek;k++) {
   fseek(my_fil,k*sf,SEEK_SET);
   fread(&amp;pos1,sf,1,my_fil);
   for(m=k+1;m&lt;=storlek;m++) {
      fseek(my_fil,m*sf,SEEK_SET);
      fread(&amp;pos2,sf,1,my_fil);
      if(strcmp(pos1.pnumber,pos2.pnumber)&gt;0) {
      tmp=pos1; pos1=pos2; pos2=tmp;
      fseek(my_fil,k*sf,SEEK_SET);
      fwrite(&amp;pos1,sf,1,my_fil);
      fseek(my_fil,m*sf,SEEK_SET);
      fwrite(&amp;pos2,sf,1,my_fil);
  }
 }
}

//Something may be wrong here too....
utskrift();
}

void utskrift(){
FILE *my_fil;
int n=0,sf,ft,storlek;
perspost preg;
clrscr();
my_fil=fopen(&quot;fil.txt&quot;,&quot;rb&quot;);

fseek(my_fil,0,SEEK_END);
ft=ftell(my_fil);
sf=sizeof(perspost);
storlek=ft/sf;

while(n&lt;=storlek){
fseek(my_fil,n*sizeof(preg),SEEK_SET);
fread(&amp;preg,sizeof(preg),1,my_fil);
printf(&quot;%s\n&quot;,preg.pnumber);
printf(&quot;%s\n&quot;,preg.forname);
printf(&quot;%s\n&quot;,preg.lastname);
n++;
}
printf(&quot;END&quot;);
getch();
meny();
}
</pre></div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
