C Activity
Grade List  - Dynamic Name Length

Note

This program is identical to the previous GradeList activity with the exception that now the name field will not be a fixed character array. The name array wll be dynamically sized to be large enough to just fit the actual name length.

Overview

For this activity you will write a program that

  1. Reads set of names and grades from standard input,
  2. Prints the list in its initial ordering
  3. Prints the list sorted by name,
  4. Prints the list sorted by grade, and finally
  5. Prints the mean and median grade.

The input file is organized as a set of lines with the following format:

name grade

where name is a sequence of up to 20 alphabetic characters starting in the first column of the, grade is an integer in the range 0 .. 100 terminated by a newline, and the name and grade are separated by a single space. An example grade file is:

Jones 70
Ziegler 85
Adams 75
Morris 90
Samuels 80


You may assume that

  1. The standard input holds a legally formatted grade file, and
  2. There are at most 100 grades in the file.

The grade information itself will be held in an array of grade_entry structures (grade_list[MAXGRADES]), with an associated int variable numgrades, declared as follows:

#define MAXNAME (20)

typedef struct{
        char *name;     // variable length string
        int grade;
}grade_entry;



#define MAXGRADES (100)

int numgrades = 0 ;
grade_entry grade_list[ MAXGRADES ] ;    // note change since we are using a grade_entry typedef


To compute the average, sum up the grades, convert the sum and the number of grades to double precision, and compute the ratio. For example, if the integer grade summation is in an int variable sumgrades, then the mean is a double precision variable, then the computation would look like:

mean = (double)sumgrades / (double)numgrades ;

Computing the median, a double precision variable, assumes the grade_list is sorted by grade, and depends on whether numgrades is odd or even:

Constraints

Your program must comprise one file, main.c, which will start with the declarations above. I strongly suggest that you declare and define two functions to do the sorting:

static void sort_by_name() ;
static void sort_by_grade() ;


to encapsulate the two sorting algorithms (remember: the strcmp function imported via #include <string.h> can be used to compare two NULL-terminated strings). Feel free to add other helper functions you find useful, (e.g., to print the grade list, to compute the mean and median, etc.).

You must also include a Makefile which has a single target, grades, which will compile your main.c and produce an executable named grades. Test your program using the sample file above, as well as others you create.

Submission

The directory in your repository should be called GradeList-Variable

Grading (10 points)

2  Submission: Correct files submitted according to specification.
1  Compilation: main.c compiles without error
1  Make: make compiles main.c without error
1  Program: grades reads data prints it in the original order.

1  Program: grades reads data and sorts properly by name
1  Program: grades sorts properly by grade
1  Program: grades computes correct mean and median
2  Quality: main.c conforms to standards of quality in structure,style, naming and documentation.