CS1101C Practical Exam

Session 1 (1000 - 1145 hours)

Student CAP Database

The name of your C program file must be called studb.c, files with any other name will not be marked.

The deadline for this lab is Wednesday 11 April 2007, 11:45:59 hours. Strictly no submissions will be accepted after the deadline.

Background

NUS is looking for a database system to maintain the record of students' CAP (Cumulative Average Points). The database system must be able to update according to the exam results each year. The formula to calculate CAP is shown below:

CAP = [Sum of (grade point * modular credit)]/sum[modular credits]

Text Files

You are provided with 2 data files (which has been copied into your directory by the pesetup command). The first file is called “init.txt” that contains the initial database. Each line of the file record a student's name, total MCs he/she has taken and the current cumulative grade points.

Cumulative Grade Points = Sum of (grade point * modular credit)

A line sample of the file “init.txt” is shown below:

Yang Fei 28 108

It means the student named Yang Fei has a CAP of 3.86 (108/28) after taking 28 modular credits.

The second file “exam_results.txt” stores the exam results of the latest semester, which needs to be input to the database and the CAP updated for each student. The file content is grouped by modules. Each module starts with a separate line indicating the module code (starting with a “#”) and its MC, followed by student grades. For your convenience, the grades has already been translated into numeric scores (0 to 5). Students that have no records in the database will be added in. For example:

#EG1108 3
Soo Yuen Jien 3.5
Yang Fei 4.5

means for the module EG1108 which worth 3 MCs, Soo Yuen Jien got a "B" (he will be added into the database since he is a new student), and Yang Fei finished with "A-" (his record will be updated).

You Need To Do

Write a C program to
  1. Read in the student records from the file “init.txt”.

  2. Update the database according to the file “exam_results.txt”. calculate the CAP for existing students and add the records for new students.

  3. Print out the updated database. Each line includes student's Name, MC, CAP. Display the names in the same order as in the initial database. New students are positioned at the end in the order of their appearance. The CAP value's decimal portion should have a precision of 2 digits.

For example, let’s say “init.txt” contains the following lines:

Raymond Tan 110 537
Beatrice Luca 30 116
Yang Fei 28 108

“exam_results.txt” contains:

#CS1101C 4
Beatrice Luca 4.5
Yang Fei 4.5
#EG1108 3
Soo Yuen Jien 3.5
Yang Fei 4.5
#MA1505 4
Yang Fei 5.0
Raymond Tan 4.0

Your program should print the following result on to the screen:

Raymond Tan 114 4.85
Beatrice Luca 34 3.94
Yang Fei 39 4.09
Soo Yuen Jien 3 3.50

You may assume that there are at the most 1000 different students, each student's name is at the most 30-characters long and contains only alphabets, and each module code is at the most 10-characters long and contains only alphabets and digits. You are not required to handle file I/O errors.

Hints

Since we know neither the length of each student's name, nor the the number of words contained in each name, we must find a way to determine whether we have reached the end of a name. Use the fact that the names are purely made by alphabets, in other words, there should be no digits in any names.

Following are some built-in String functions that may be helpful (it is not compulsory for you to use them).

int strcmp( const char *str1, const char *str2 );

The function strcmp() compares two strings, and returns 0 if they are the same, positive integer for str1 is greater than str2 and negative if str1 is less than str2

char *strcat( char *str1, const char *str2 );

The strcat() function concatenates str2 onto the end of str1, and returns str1.

Remember to add the line #include <string.h> since we are using the above functions related to strings.

#include <stdlib.h>
double atof( const char *str );
int atoi( const char *str );

The function atof() converts str into a double, then returns that value. str must start with a valid number, but can be terminated with any non-numerical character, other than "E" or "e". Similarly, atoi() converts str into an integer, then returns that value.e.g.

x = atof( "42.0is_the_answer" );
i = atoi( "512" );

would result in x being set to 42.0 and i being set to 512. Note that you need to #include <stdlib.h> for these 2 functions.

Notes

We will test your programs with other (more complicated) text input files.

Do not use any structures or any form of dynamic memory allocation (using malloc or calloc) in your program, else no credit will be given.

Remember to submit your program frequently using the submit studb.c command, and check your submission using the check command.

All the best!

UNIX commands

Some useful UNIX commands (in case you forgot what you did in Lab 0):

  1. dir”: lists all the files in the directory.
  2. cp a.txt b.txt”: copies a.txt to b.txt.
  3. mv a.txt b.txt”: moves / renames a.txt to b.txt.
  4. cat a.txt”: shows the contents of a.txt.