August 1999
CA208 : 'C' PROGRAMMING

QUESTION 2

Total Marks: 20 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 2

You are to implement a student record database system for an Information Technology course. The system stores one record per student per course. There are four different subjects on the course :   C programming (denoted by CA), Pascal Programming (denoted by AP), C++ programming (denoted by CPP) and Java programming (denoted by JPP).

 

(a) Define a suitable enumerated type type, subject_type, which takes on the value of any of the four subject types denoted. [1]
enum subject_type {CA, AP, CPP, JPP};

 

(b) Declare a suitable data structure StudentRecord, for each student, which is capable of holding the following information:
  • The students name, called student_name, which has a maximum length of 50 characters;
  • The subject_type to which this record refers, called subject;
  • The mark the student achieved in the examination for this subject, called mark.
  • Pointers left and right, which will point to structures of this type to allow creation of a double linked list.
[5]
A sample definition of student_record follows:

typedef struct student_record{
     char student_name[50];
     subject_type subject;
     int mark;
     struct student_record *left, *right;
}    StudentRecord;

 

(c) Given the definition of the double linked list which you are to use for storing your records :

typedef struct {
   StudentRecord* leftmost;
   StudentRecord* rightmost;
   Double_linked_list;
}

Write a function
void add_candidate(double_linked_list* mylist, StudentRecord* candidate);
that takes in a StudentRecord, and adds it to the right of the list. You should assume the list being passed in is non-empty, and you should assume that since the candidate parameter being passed in is the same size as the list element being created, a memcpy will allow the structure to be copied once suitable space has been created.

[4]
A sample of definition of add_candidate follows:

void add_candidate(double_linked_list* mylist, StudentRecord* candidate)
{
my_list->rightmost->right= (StudentRecord*)malloc (sizeof(StudentRecord));
memcpy(my_list->rightmost->right, &candidate, sizeof(StudentRecord));
my_list->rightmost->right->left = my_list->rightmost;
my_list->rightmost->right->right = NULL;
my_list->rightmost = my_list-<rightmost ->right;
}


(d) Write a function StudentRecord* remove_candidate(int mark); that will take in the value of a mark, and remove and return the first occurrence in the list that it finds of a candidate scoring that mark. If no candidate is found scoring that mark, the function will return NULL. [6]
A sample of definition of remove_candidate follows:

StudentRecord* remove_candidate(int mark)
{
     StudentRecord *iterator, *candidate;

     while(iterator->right!=NULL) {
          if(iterator->mark ==mark) {
             candidate= StudentRecord*)malloc(sizeof(StudentRecord));
             memcpy(candidate, iterator, sizeof(StudentRecord) );
             iterator->left->right = iterator->right;
             iterator->right->left = iterator->left;
             free(iterator);
             return candidate;
        }
   }
  return NULL;
}

 

(e) Write a function sort_list, that takes a pointer to the list x, and a pointer to an empty list y, and, starting at the maximum mark of 60 and decreasing to the minimum mark of 0, will perform a sort. Each element matching the current mark should be removed from the original list, and added to the new list. The result will be that the list y will contain all of the records which were in x, but in decreasing order of marks. [4]
A sample of definition of sort_list follows:

void sort_list(double_linked_list *x, double_linked_list *y)
{
     StudentRecord* a;
     int count;

     for(count = 60; count >=0;   count--) {
      while( (a=remove_candidate(count) ) !=NULL)
          add_candidate(y,a);
   }
}