CSI2131 Assignment#2 Part-1 Solution
Designing the file structure…
Each Record has the following fields:
Field-1: Year (4-digit field)
Field-2: Semester (Fall, Winter or Summer)
Field-3: Course code (3 letters and 4 numbers)
Field-4: Course section (one character or, empty)
Field-5: Course title
Field-6: Instructor's name
Field-7: Course web page (optional)
Field-8: Instructor's home page (optional)
RECORD STRUCTURE
:
We have variable-length Records
Some fields have fixed length (fields 1-4)
some fields have variable length (fields 5-8).
There is NO length indicator.
Each variable-length field ends with a special seperator character('|').
The first four bytes of the record contains the fixed length fields
BYTE-1 : Contains "year" and "term" fields.
The "term" is encoded as 0, 1 or, 2
0 <=> Fall
1 <=> Winter
2 <=> Summer
- The "year" is encoded as a number between 0-84 (for years 1997 to 2081)
1997 -> 0
1998 -> 1
...
2081 -> 84
YEAR |
Fall (0) |
Winter (1) |
Summer (2) |
(0) 1997 |
0 |
1 |
2 |
(1) 1998 |
3 |
4 |
5 |
(2) 1999 |
6 |
7 |
8 |
. . .
|
. . . |
. . . |
. . . |
(83) 2080 |
249 |
250 |
251 |
(84) 2081 |
252 |
253 |
254 |
- Note that we have 3*85=255 possible (year,term) combinations, so we can number them from 0 to 254. One byte is enough to store this number.
- How to get the number corresponding to a (year,term) combination ?
The "year" value is multiplied by 3 and add the "term" value to it, and this number is stored in byte-1.
- How to get back the "year" and "term" from this number?
Divide the number by 3. The remainder (0-2) gives us the "term" and the quotient(0-84) gives the "year" value.
BYTE-2 & 3 : Contains the Course Code field.
For the first part of the code (CSI,CEG etc.) we have 5 possible values
CEG => 0
CSI => 1
ELG => 2
GNG => 3
SEG => 4
- For the last 4 digits in the code, the range is from "0000" to "9999", which can be stored as the corresponding binary number (between 0 and 9999 incl.)
- Total number of combinations = 5 * 10000 = 50,000
To store these numbers, we need at least 16 bits (= 2 bytes).
BYTE-4 : Contains the Course Section.
- We store this field as the ASCII code of the letter representing the section.This takes only one byte (or, one character)
Variable-length fields :
Starting from byte-5, we store the variable length fields (field-5 to field-8) each separated by the '|' character.
We need a separator at the end of the last field but none at the start of the field-5.
TOTAL BYTES USED: 4 (for the fixed length fields) + 4 (for the separators) + number of characters in field-5 to field-8.
EXAMPLE OF A RECORD:
Year: 2005
Term: Winter
Code: CSI2131
Section: B
Course Name: File Mangement
Instructor Name: Lucia Moura
Course Webpage: http://www.site.uottawa.ca/~lucia/courses/2131-05/
Professor's Homepage: http://www.site.uottawa.ca/~lucia/
This Record is stored as follows:
- "year" value = 2005-1997 = 8
"term" value = 1 (for Winter)
So, Byte-1 contains the number 8*3+1 = 25
- "CSI" => 1
"2131" => 2131
Byte(2,3) contains the number 2131*5+1 = 10656
- "section" value = 66 (ASCII code for B)
Byte-4 contains the number 66 (same as the char 'B')
- Byte-5 onwards, we store the following string of characters:
"File Mangement|Lucia Moura|http://www.site.uottawa.ca/~lucia/courses/2131-05/|http://www.site.uottawa.ca/~lucia/|"
So, we can store this record using 4 + 113 =
117 bytes.
NOTE: ASSUMPTIONS ABOUT THE DATA:
1. The value of the "year" field is assumed to range from 1997 to 2081.
2. The course code should start with the letters "CSI", "SEG", "ELG", "CEG", or "GNG", followed with exactly four digits.
3. The course section can be only an upper-case letter (A-Z) or, it may be blank.
4. The fields 5 to 8, could consists of any number of characters. So, there is no restriction on the sizes of these fields.