Journal Articles

CVu Journal Vol 11, #6 - Oct 1999 + Student Code Critiques from CVu journal.
Browse in : All > Journals > CVu > 116 (22)
All > Journal Columns > Code Critique (70)
Any of these categories - All of these categories

Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. 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/yourtheme/modules/articles . The templates will get the extension .xt there.

Title: Student Code Critique Competition

Author: Administrator

Date: 05 October 1999 13:15:34 +01:00 or Tue, 05 October 1999 13:15:34 +01:00

Summary: 

Body: 

The problem is to read in a file (first item being a count of the number of remaining items. For example the file "land.dat" looks like this:

10
swe dan dan nor swe swe tur swe pol dan

Having read the file you must create a list of the unique elements (so the list form the above file would be: swe, dan, nor, tur, pol. The order of the elements is irrelevant.

Now the following code is the student's solution. Critique and refine it. Then write your own model answer. Just to exercise your brain cells a little more, answers must be in C (it is too easy in C++☺). I have left out the #includes

#define MAXLAND 10
typedef struct { char namn[4]; }test;
int main(void){
  FILE *fil;
  test Ttest[MAXLAND],Ttemp[MAXLAND];
  int k=0,counter=0,ej_lika,m,stycken,a;
  fil=fopen("land.dat","r");
  if(!fil){
    printf("ERROR open file!");
    exit(1);
  }
  fscanf(fil,"%d",&stycken);
  while(!feof(fil)){
     fscanf(fil,"%s",&Ttest[k].namn);
     k++;
  }
  fclose(fil);
  for(k=0;k<=MAXLAND;k++) Ttemp[k].namn==NULL;
/' Can i do this different??*/
  for(k=0;k<=stycken-1;k++){
    ej_lika=0;
    for(m=0;m<=stycken-1;m++){
      if(strcmp(Ttest[k].namn, Ttemp[m].namn)!=0)  ej_lika++;
      if(ej_lika==stycken){
        strcpy(Ttemp[counter].namn, Ttest[k].namn);
        ej_lika=0;
        counter++;
        break;
       }
    }
  }
/*check the result.*/
  for(k=0; k<counter; k++) printf("%s\n",Ttemp[k].namn);
  getch();   return 0;
}

Credit will begiven for clarity of the review as well as the quality of the final code. As long as the code format is consistent any format is acceptable and no credit will be given for any specific choice. Good use of identifiers and suitable comments are expected. The 'winner' (i.e. my sole choice) will be able to choose a title from my collection of prize books.

Notes: 

More fields may be available via dynamicdata ..