home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 210_01 / sumgradr.c < prev    next >
Text File  |  1985-11-14  |  10KB  |  390 lines

  1. /* SUMGRADR.C   VERS:- 01.00  DATE:- 09/26/86  TIME:- 09:39:39 PM */
  2. /*
  3. cp b:$1 -e -s0
  4. z80asm $1.bb
  5. slrnk b:$1, b:$1/n/e
  6. */
  7. /* 
  8. Description: 
  9.  
  10. Sum each student's grades, with weighting according to point value of report, 
  11.     exam, etc.  
  12. Drop any grade entered as negative.  
  13. Calculate average = sum grades / sum possible points.
  14. Output sorted by name, and by average.
  15.  
  16. For structure of the data file for input to the program, see the usage message.
  17.  
  18. By J.A. Rupley, Tucson, Arizona
  19. Coded for ECO C compiler, version 3.40
  20. */
  21.  
  22. /*
  23. For example:
  24.  
  25. There are 7 expts, 2 exams, and a notebook grade.
  26. The final is optional, and is optionally included in the input file below.
  27. The low grade for the experiments is dropped and is given as a negative number.
  28. Negative "0" is given as "-999".
  29. Excused missing grades are given as "-999".
  30. Point values of labs, exams, and notebook  =  (maxium credit) * (weight)
  31.                    0     1     2     3     4     5     6     7     8     9
  32.                    lab   lab   lab   lab   lab   exm   lab   lab   nbk   exm
  33.                    100   200   100   100   200   250   100   200   300   250
  34.  
  35. weight             1     2     1     1     2     2.5   1     2     3     2.5
  36.  
  37. maxium credit      100   100   100   100   100   100   100   100   100   100
  38.  
  39. data given for each student are:
  40. name      9 (or 10) grades, one for each lab, etc, on a 100 point scale
  41.  
  42. ******************************************************************************
  43. */
  44.  
  45. #include <stdio.h>
  46.  
  47. #include <ctrlcnst.h>
  48.  
  49. #define STUDENTS    200        /* maximum class size        */
  50. #define GRADES        20        /* maximum number of 
  51.                         grades per student    */
  52.  
  53.  
  54.  
  55.                     /* data structure "students",
  56.                         to contain grade info
  57.                         and calculated averages    */
  58. struct students {
  59. char *name;
  60. int grades[GRADES];
  61. float total;
  62. float possible;
  63. float average;
  64. } ;
  65.  
  66.                     /* data for each student in class */
  67. struct students stu[STUDENTS];
  68.  
  69.                          /* an array of pointers to
  70.                         struct students, later set
  71.                         to stu, for use in sorting */
  72. struct students *jarsort[STUDENTS];
  73.  
  74.  
  75.                     /* title, giving class, year, etc  */
  76. char title[80];
  77.  
  78.                     /* number of students in class and
  79.                         number of grades per student */
  80. int num_student, num_grade;
  81.  
  82.  
  83.                     /* relative weight of each grade */
  84. float weight[GRADES];
  85.  
  86.                     /* full credit, ie maximum possible
  87.                         points for each grade    */
  88. int credit[GRADES];
  89.  
  90.  
  91.                     /* array to store student names,
  92.                         pointed to by name element
  93.                         of data structure stu    */
  94. char name[STUDENTS][24];
  95.  
  96.  
  97.  
  98.                 /* main program            */
  99. main(argc, argv)
  100. int argc; char *argv[];
  101. {
  102.     register int i, j;
  103.     int putchar();
  104.     int strcomp(), fltcomp(), swap();
  105.     void read_data();
  106.     void printf(), bsort(), exit();
  107.  
  108.  
  109.                     /* read data from file        */
  110.     read_data(argc, argv);
  111.  
  112.  
  113.                     /* calculate adjusted average etc */
  114.                         /* omit grades flagged "-" */
  115.                         /* sum points received
  116.                             and points possible */
  117.                         /* calculate average grade */
  118.     for (i=0; i < num_student; i++) {
  119.         stu[i].possible = stu[i].total = stu[i].average = 0;
  120.         for (j=0; j < num_grade; j++) {
  121.             if (stu[i].grades[j] < 0 )
  122.                 continue;
  123.             else {
  124.                  stu[i].total = stu[i].total 
  125.                         + stu[i].grades[j] * weight[j];
  126.                  stu[i].possible = stu[i].possible 
  127.                         + credit[j] * weight[j];
  128.             }
  129.         }
  130.         stu[i].average = 100 * stu[i].total / stu[i].possible;
  131.     }
  132.  
  133.  
  134.                     /* initialize array of pointers to 
  135.                         structure elements, to be
  136.                         used in sorting subroutines,
  137.                         swap and comp        */
  138.     for (i = 0; i < num_student; i++)
  139.         jarsort[i] = &stu[i];
  140.  
  141.  
  142.  
  143.                     /* sort students by name
  144.                         and print        */
  145.     bsort(num_student, strcomp, swap);
  146.  
  147.     putchar(FF);
  148.     printf("\n%-s\n", title);
  149.     printf("Sorted  by student name\n\n");
  150.  
  151.     for (i = 0; i < num_student; i++) {
  152.         printf("%3d %-23.23s ", (i + 1), jarsort[i]->name);
  153.         printf("%5.0f %5.0f %7.2f\n", jarsort[i]->total, 
  154.                 jarsort[i]->possible, jarsort[i]->average);
  155.     }
  156.  
  157.  
  158.                     /* sort students by grade
  159.                         and print        */
  160.     bsort(num_student, fltcomp, swap);
  161.  
  162.     putchar(FF);
  163.     printf("\n%-s\n", title);
  164.     printf("Sorted  by grade\n\n");
  165.  
  166.     for (i = 0; i < num_student; i++) {
  167.         printf("%3d %-23.23s ", (i + 1), jarsort[i]->name);
  168.         printf("%5.0f %5.0f %7.2f\n", jarsort[i]->total, 
  169.                 jarsort[i]->possible, jarsort[i]->average);
  170.  
  171.     }
  172.  
  173.                     /* end with form feed        */
  174.     putchar(FF);
  175. }                /* END OF MAIN                */
  176.  
  177.  
  178.  
  179.                 /* NOTE-- the swap routine exchanges
  180.                     pointers to structure records;
  181.                     the comp routines compare members of
  182.                     two structure records, which may
  183.                     be swapped later; the members checked
  184.                     here are the average grade of a 
  185.                     student (fltcomp), 
  186.                     and his name (strcomp).        */
  187.  
  188.  
  189.                 /* swap contents of array elements x and y
  190.                     which are pointers to structure
  191.                     records tested in comp routines */
  192. int swap(x, y)
  193. int x, y;
  194. {
  195.     int *temp;
  196.  
  197.     temp = jarsort[x];
  198.     jarsort[x] = jarsort[y];
  199.     jarsort[y] = temp;
  200.  
  201.     returε (OK);
  202.  
  203. }                /* END OF SWAP                */
  204.  
  205.  
  206.  
  207.                 /* compare float contents of records x and y
  208.                        for struct member average
  209.                     and for a descending sort    */
  210. int fltcomp(x, y)
  211. int x,y;
  212. {
  213.     if (jarsort[x]->average < jarsort[y]->average)
  214.         return (1);
  215.  
  216.     else if (jarsort[x]->average > jarsort[y]->average)
  217.         return (-1);
  218.  
  219.     else if (jarsort[x]->average == jarsort[y]->average)
  220.         return (0);
  221.  
  222. }                /* END OF FLTCOMP            */
  223.  
  224.  
  225.  
  226.                 /* compare string contents of records x and y
  227.                        for struct member name
  228.                     and for an ascending sort    */
  229. int strcomp(x, y)
  230. int x,y;
  231. {
  232.     int strcmp();
  233.  
  234.     return (strcmp(jarsort[x]->name, jarsort[y]->name));
  235.  
  236. }                /* END OF STRCOMP            */
  237.  
  238.  
  239.  
  240.                 /* usage message            */
  241. void use_mess()
  242. {
  243.     void puts();
  244.  
  245. puts("\nUsage:");
  246. puts("A>sumgrade  <filename.typ>");
  247. puts("   filename.typ has the following structure:");
  248. puts("     line 0.  one-line title, with no tabs or ctrl characters");
  249. puts("     line 1.  two INTEGER values, num_student  &  num_grade");
  250. puts("     line 2.  num_grade FLOAT values of relative weights of each grade");
  251. puts("     line 3.  num_grade INTEGER values of full credit for each grade");
  252. puts("     line 4.  student name, then num_grade grades for that student");
  253. puts("                only 23 characters of the name are read");
  254. puts("                grades must be given as INTEGERS");
  255. puts("                grades to be dropped are to be given as negatives");
  256. puts("                excused missing or zero grades are given as -999");
  257. puts("\n");
  258. puts("      repeat line 4 for num_student students");
  259. puts("      lines 1 to 4 can be more than one line, ie the input file can be");
  260. puts("              freeform, with the restriction that there must be the");
  261. puts("              proper number and sequence of numeric and alphabetic");
  262. puts("              values; a crlf like a space terminates a value");
  263. puts("      comments at the end of the file are not read by the program");
  264. puts("              and any other information to be stored should be there");
  265. puts("\n");
  266. }                /* END OF USE_MESS            */
  267.  
  268.  
  269.  
  270.                 /* read data from file            */
  271. void read_data(argc, argv)
  272. int argc; char *argv[];
  273. {
  274.     register int i, j;
  275.     int t, c;
  276.     int fscanf(), ungetc(), getc();
  277.     int isdigit(), isalpha(), iscntrl();
  278.     void printf(), exit(), puts(), use_mess();
  279.     FILE *fptr, *fopen();
  280.  
  281.  
  282.                     /* open data file specified
  283.                         in the command line    */
  284.                         /* print filename if given */
  285.                         /* else print usage message */
  286.                         /* exit on error    */
  287.     if (argc < 2) {
  288.         use_mess();
  289.         exit (ERROR);
  290.     }
  291.     else if ((fptr = fopen(argv[1],"r")) == NULL) {
  292.         puts("\nSorry, I cannot open the file.\n\n");
  293.         exit (ERROR);
  294.     }
  295.     printf("\nData from file = %s\n", argv[1]);
  296.  
  297.  
  298.                     /* read title and print        */
  299.     for (i = 0; i < 79; i++) {
  300.         if (iscntrl(title[i] = getc(fptr)))
  301.             break;
  302.     }
  303.     title[i] = '\0';
  304.     printf("\n%-s\n", title);
  305.  
  306.  
  307.                     /* read number of students and
  308.                         number of grades per student
  309.                         and print             */
  310.                         /* exit on error    */
  311.     t = fscanf(fptr, "%d %d", &num_student, &num_grade);
  312.     if (t <= 0) {
  313.         printf("\nError in reading file\n");
  314.         exit(ERROR);
  315.     }
  316.     printf("\nnum_student = %d   num_grade = %d\n", 
  317.             num_student, num_grade);
  318.  
  319.  
  320.  
  321.                     /* read relative weights for 
  322.                         each grade and print    */
  323.                         /* exit on error    */
  324.     for (j = 0; j < num_grade; j++) {
  325.         if (j % 10 == NULL)
  326.             printf("\nweights =     ");
  327.         t = fscanf(fptr, "%f", &weight[j]);
  328.         if (t <= 0) {
  329.             printf("\nError in reading file\n");
  330.             exit(ERROR);
  331.         }
  332.         printf("%6.2f",weight[j]);
  333.     }
  334.     printf("\n");
  335.  
  336.  
  337.                     /* read points given as full credit
  338.                         for each grade and print */
  339.                         /* exit on error    */
  340.     for (j = 0; j < num_grade; j++) {
  341.         if (j % 10 == NULL)
  342.             printf("\nfull credit = ");
  343.         t = fscanf(fptr, "%d", &credit[j]);
  344.         if (t <= 0) {
  345.             printf("\nError in reading file\n");
  346.             exit(ERROR);
  347.         }
  348.         printf("%6d",credit[j]);
  349.     }
  350.     printf("\n");
  351.  
  352.  
  353.                     /* read name and set of grades for
  354.                         each student, and print    */
  355.                         /* position pntr at name */
  356.                         /* read name to EOL or digit */
  357.                         /* read numgrade grades    */
  358.                         /* print data as read    */
  359.                         /* exit on error    */
  360.     for (i = 0; i < num_student; i++) {
  361.         while(!isalpha(c = getc(fptr)))
  362.             ;
  363.         ungetc(c, fptr);
  364.  
  365.         for (j = 0; j < 23; j++) {
  366.             if (isdigit(name[i][j] = getc(fptr))
  367.                     || iscntrl(name[i][j])) {
  368.                 ungetc(name[i][j], fptr);
  369.                 break;
  370.             }
  371.         }
  372.         name[i][j] = '\0';
  373.         stu[i].name = name[i];
  374.         printf("\n%3d %-23.23s ", (i + 1), stu[i].name);
  375.  
  376.         for (j = 0; j < num_grade; j++) {
  377.             t = fscanf(fptr, "%d", &stu[i].grades[j]);
  378.             if (t <= 0) {
  379.                 printf("\nError in reading file\n");
  380.                 exit(ERROR);
  381.             }
  382.             if (j != NULL && (j % 10) == NULL)
  383.                 printf("\n                             ");
  384.             printf("%5d", stu[i].grades[j]);
  385.         }
  386.     }
  387.     printf("\n");
  388.  
  389. }                /* END OF READ_DATA            */
  390.