CMPSC 311, Spring 2013, Project 2, Grading Info



General comments.
Specific comments and the grade will be posted to the ANGEL gradebook.

If you have questions, please contact the TA or the instructor.



Some common mistakes.
For testing purposes, we ignored differences in the output that were only due to the treatment of spaces and tabs, or simple mistyping.  Some of the later projects will not be so generous - you will need to produce exactly the same output then.

Various problems, caused by not reading or following the instructions carefully.



The test cases

% ls -l (edited to show only the byte counts)
    0   Test1
    1   Test2
    1   Test3
    2   Test4
    2   Test5
    3   Test6
    4   Test7
    4   Test8
    5   Test9
    5   Test10
    6   Test11
    9   Test12
    7   Test13
    8   Test14
    7   Test15
    7   Test16
   80   Test17
   37   Test18

% od -c Test1
0000000

% od -c Test2
0000000  \n
0000001

% od -c Test3
0000000  \r
0000001

% od -c Test4
0000000  \r  \n
0000002

% od -c Test5
0000000  \n  \r
0000002

% od -c Test6
0000000   1   2   3
0000003

% od -c Test7
0000000   1   2   3  \n
0000004

% od -c Test8
0000000   1   2   3  \r
0000004

% od -c Test9
0000000   1   2   3  \r  \n
0000005

% od -c Test10
0000000   1   2   3  \n  \r
0000005

% od -c Test11
0000000   1  \n   2  \n   3  \n
0000006

% od -c Test12
0000000   1  \r  \n   2  \r  \n   3  \r  \n
0000011

% od -c Test13
0000000   1  \r  \n   2  \r  \n   3
0000007

% od -c Test14
0000000   1  \r  \n   2  \n   3  \r  \n
0000010

% od -c Test15
0000000   1  \r   2  \r  \n   3  \r
0000007

% od -c Test16
0000000   1  \r   2  \n   3  \r  \n
0000007

% od -c Test17
0000000  \0 003 006  \t  \f 017 022 025 030 033 036   !   $   '   *   -
0000020   0   3   6   9   <   ?   B   E   H   K   N   Q   T   W   Z   ]
0000040   `   c   f   i   l   o   r   u   x   {   ~ 201 204 207 212 215
0000060 220 223 226 231 234 237 242 245 250 253 256 261 264 267 272 275
0000100 300 303 306 311 314 317 322 325 330 333 336 341 344 347 352 355
0000120

% od -c Test18
0000000   1   2   3   4   5   6   7   8   9   0   !   @   #   $   %   ^
0000020   &   *   (   )   ~   `   _   -   +   =   q   w   e   r   t   y
0000040   u   i   o   p  \n
0000045



All testing for the grade was done on Linux.  We also ran the programs through Sun's compiler, the GNU compiler, and lint.

cc -v -o pr2-sun pr2.3.c
gcc -Wall -Wextra -o pr2-gcc pr2.3.c
lint pr2.3.c

Some typical error messages and explanations:

"pr2.3.c", line 69: syntax error before or at: else
pr2.3.c:69: error: syntax error before "else"
(69) error: syntax error before or at: else

pr2.3.c:91: error: syntax error before "if"
(91) error: syntax error before or at: if
(91) error: syntax error before or at: (
(91) warning: old-style declaration or incorrect type for: ferror

pr2.3.c:100: error: too few arguments to function `fprintf'
(100) error: prototype mismatch: 1 arg passed, 2 expected
        Misuse of library function.

pr2.3.c:55: error: break statement not within loop or switch
(55) error: "break" outside loop or switch

pr2.3.c:33: error: `rn1' undeclared (first use in this function)
(33) error: undefined symbol: rn1
        Obvious.

(94) warning: syntax error:  empty declaration

"pr2.3.c", line 18: syntax error before or at: char
pr2.3.c:18: error: syntax error before "char"
(18) error: syntax error before or at: char

pr2.3.c:22: error: `end' undeclared (first use in this function)
pr2.3.c:22: error: invalid lvalue in assignment
(22) error: undefined symbol: end
(22) error: left operand must be modifiable lvalue: op "="

"pr2.3.c", line 51: syntax error before or at: =
pr2.3.c:51: error: syntax error before '=' token
(51) error: syntax error before or at: =

"pr2.3.c", line 62: syntax error before or at: char
pr2.3.c:62: error: syntax error before "char"
(62) error: syntax error before or at: char

pr2.3.c:184: error: conflicting types for 'printallcounts'
pr2.3.c:98: error: previous definition of 'printallcounts' was here
(184) error: identifier redeclared: printallcounts
        Duplicate definition, but not the same.

Some typical warning messages and explanations.

"pr2.3.c", line 23: warning: implicit function declaration: isprint
pr2.3.c:23: warning: implicit declaration of function `isprint'
(23) warning: implicit function declaration: isprint
        Forgot to #include <ctype.h>

"pr2.3.c", line 22: warning: implicit function declaration: exit
pr2.3.c:22: warning: implicit declaration of function `exit'
(22) warning: implicit function declaration: exit
        Forgot to #include <stdlib.h>

"pr2.3.c", line 106: warning: implicit function declaration: determinetypeoffile
pr2.3.c:106: warning: implicit declaration of function `determinetypeoffile'
(106) warning: implicit function declaration: determinetypeoffile
        Forgot to write a prototype for determinetypeoffile()

"pr2.3.c", line 75: warning: Function has no return statement : main
        An obvious mistake.

"pr2.3.c", line 139: warning: newline not last character in file
pr2.3.c:139:12: warning: no newline at end of file
        Where did you write this file?

(212) warning: statement has no consequent: else
        Sloppy workmanship.

pr2.3.c:58: warning: suggest parentheses around assignment used as truth value
pr2.3.c:20: warning: unused variable `one'
pr2.3.c:39: warning: unused parameter 'argc'
(42) warning: variable may be used before set: last_c
(23) warning: constant in conditional context
(94) warning: syntax error:  empty declaration
pr2.3.c:107: warning: control reaches end of non-void function
(54) warning: equality operator "==" found where assignment "=" expected
(58) warning: assignment operator "=" found where "==" was expected
        Might be obvious?

pr2.3.c:39:12: warning: multi-character character constant
The code is     if (c == '\r \n')
which should merit an error, not just a warning.  As an attempt to check two consecutive characters, it won't work because there are three characters in this multi-character character constant, and the concept is wrong anyway.
pr2.3.c:22: warning: left-hand operand of comma expression has no effect
pr2.3.c:22: warning: statement with no effect
(91) warning: old-style declaration or incorrect type for: ferror
        Might not be obvious?
       
"pr2.3.c", line 100: warning: argument #1 is incompatible with prototype:
pr2.3.c:100: warning: passing arg 1 of `fprintf' from incompatible pointer type
pr2.3.c:63: warning: passing arg 2 of `fprintf' makes pointer from integer without a cast
(63) warning: improper pointer/integer combination: arg #2
        Misuse of a library function.

"pr2.3.c", line 31: warning: improper pointer/integer combination: op "="
pr2.3.c:31: warning: initialization makes integer from pointer without a cast
(31) warning: improper pointer/integer combination: op "="
        Need to look at the code.

pr2.3.c:13: warning: too many arguments for format
        printf(), fprintf() problem.

pr2.3.c:26: warning: suggest parentheses around comparison in operand of |
pr2.3.c:28: warning: suggest parentheses around comparison in operand of &
        probably should have used || and && instead of | and &




Correct programs should be obviously correct.  This isn't.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

typedef int (*isptr)(int);

void report_failure(char *msg) {
  fprintf(stderr, "failure: %s\n", msg);
  exit(1);
}

const char *odd_msg(int c){
    const char *s[] = { "none detected", "file does not end with a newline character" };
    return s[c != '\n'];
}

const char *diag_msg(int *stats){
   const char *s[] = { "strange text file, no CR or LF at all", "DOS-style text file, only CRLF line terminators.", "UNIX-style text file, only LF line terminators",
      "mixed UNIX/DOS-style text file", "MacOS-style text file, only CR line terminators", "mixed MacOS/DOS-style text file, both CRLF and LF line terminators.",
      "mixed MacOS/UNIX-style text file, both CR and LF line terminators", "mixed MacOS/UNIX/DOS-style text file, CRLF, CR and LF line terminators", "not a text file" };
   if (stats[6] > stats[1])
      return s[8];
   else
      return s[(stats[13] > 0) * 4 + (stats[14] > 0) * 2 + (stats[15] > 0)];
}

void print_stats(int c, int *stats, int len){
   int i; const char *s[] = { "characters",  "isalnum ", "isalpha ", "isdigit ", "isgraph ", "isprint ", "iscntrl ", "islower ",
      "isupper ", "ispunct ", "isblank ", "isspace ", "isxdigit", "return        ", "newline       ", "return-newline"};
   printf("\n\n");
   for (i=0; i < len - 3; i++)
      printf("%10s%10d\n", s[i], stats[i]);
   printf("end of line?\n");
   for (; i < len; i++)
      printf("%16s%9d\n", s[i], stats[i]);
   printf("oddities?\n  %s\nfile type?\n  %s\n", odd_msg(c), diag_msg(stats));
}

int main(void) {
   int c, p = 0, sl = 16, fl = 12, *s = (int*)calloc(sl, sizeof(int)), *e[] = { s + 13, s + 14, s + 15 }, i;
   isptr funcs[] = { &isalnum, &isalpha, &isdigit, &isgraph, &isprint,
       &iscntrl, &islower, &isupper, &ispunct, &isblank, &isspace, &isxdigit };
   while ((c = getc(stdin)) != EOF) {
      if (putc(c, stdout) == EOF)
         report_failure("output terminated on error");
      for (s[i=0]++; i < fl; i++)
          s[i+1] += funcs[i](c) > 0;
      *e[0] += c == '\r';
      if (p == '\r' && c == '\n') {
         (*e[0])--; (*e[2])++;
      } else
         *e[1] += c == '\n';
      p = c;
   }
   if (s[0] > 0)
      if (s[1] == s[2] + s[3])
         print_stats(p, s, sl);
      else
         printf("\nalphanumerics don't add up\n");
   else
      printf("file is empty\n");
   if (ferror(stdin))
      report_failure("input terminated on error");
   free(s);
   return 0;
}