Introduction to Software Systems
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
COMP-206 Introduction to Software Systems
Mini Assignment C: File IO and Dynamic Memory
This is an individual assignment. You need to solve these questions on your own. If you have ques-
tions, post them on Piazza, but do not post major parts of the assignment code. Though small parts
of code are acceptable, we do not want you sharing your solutions (or large parts of them) on Piazza.
If your question cannot be answered without sharing significant amounts of code, please make a pri-
vate question on Piazza or utilize TA/Instructors office hours. Late penalty is -15% per day. Even if
you are late only by a few minutes it will be rounded up to a day. Maximum of 2 late days are allowed.
You MUST use mimi.cs.mcgill.ca to create the solution to this assignment. You must not use your
Mac command-line, Windows command-line, nor a Linux distro installed locally on your laptop. You can access
mimi.cs.mcgill.ca from your personal computer using ssh or putty and also transfer files to your computer using
filezilla or scp as seen in class and in Lab A and mini assignment 1.
For this assignment, you will have to turn in one C program source code file. Instructors/TAs upon their discretion
may ask you to demonstrate/explain your solution. No points are awarded for commands that do not compile/execute
at all. (Commands that execute, but provide incorrect behavior/output will be given partial marks.) All questions
are graded proportionally. This means that if 40% of the question is correct, you will receive 40% of the grade.
Please read through the entire assignment before you start working on it. You can loose several
points for not following the instructions. There are also some helpful hints given at the end of this document
that can be useful to you.
Week 8 lectures provides some background help for this mini assignment.
Total Points: 20
Ex. 1 — Data Processing from CSV files Using Linked Lists (XX Points)
In this assignment we will implement a C program, topcgpas.c that can read a CSV file containing student infor-
mation and print a list of top 5 students (based on their CGPAs) by using a linked list to process this data.
A sample format of the input CSV file is as shown below
The first line is the header (names of fields) followed by actual information. As can be seen, the student ids are 10
digit numbers, followed by email, last name, first name and the CGPA of the student, X.Y a floating point number.
The data records themselves are not in any particular order (random).
1. Use vim to create a C program source file topcgpas.c . All of your source code logic should be contained
in this file. You may write multiple functions, etc. (You are encouraged to do so). Please read through
the entire assignment before you start working, as the programming constructs that you are
allowed to use are limited in some cases.
2. (1 Point) The source code file should include a comment section at the beginning that describes its purpose
(couple of lines), the author (your name), your department, a small “history” section indicating what changes
you did on what date.
The code should be properly indented for readability as well as contain any additional comments required to
understand the program logic.
3. The source code should be compilable by (exactly) using the command gcc -o topcgpas topcgpas.c. If
not, TAs will not grade it and no points will be awarded for the assignment.
4. (1 Point) Compilation should not produce any warnings!
5. The program accepts 2 arguments, the first is the name of the input CSV file, and the second is the name of
the output CSV file. These file names could be anything, and may have absolute or relative paths.
$ ./topcgpas students.csv output.csv
6. (1 Point) If the program is not passed exactly 2 arguments, it should print a usage message and terminate
with code 1.
$ ./topcgpas
Usage ./topcgpas
$ echo $?
1
$
7. (1 Point) If the program cannot open the input file, it should display an error message and terminate with
code 1.
$ ./topcgpas nosuch.csv output.csv
Error! Unable to open the input file nosuch.csv
$ echo $?
1
$
8. (1 Point) If the input file is empty, the program should display an error message and terminate with code 1.
$ ls -l empty.csv
-rw------- 1 jdsilv2 root 0 Mar 20 15:54 empty.csv
$ ./topcgpas empty.csv output.csv
Error! Input CSV file empty.csv is empty
$ echo $?
1
$
9. (1 Point) The program should behave the same as above, if the input file only has the header (as from the
program’s perspective there is no data for it to work on).
$ cat headeronly.csv
sid,email,lname,fname,cgpa
$ ./topcgpas headeronly.csv output.csv
Error! Input CSV file headeronly.csv is empty
$ echo $?
1
$
2
10. (6 Points) The program should write the sid, email and cgpa of the top 5 (based on their CGPAs) students
into the output file. The output file should have a header and the records should be printed with the students
with the highest CGPAs on the top (i.e., ordered based on decreasing CGPAs). If two students have the same
CGPA, they can be in any order. Existing output files should be overwritten (not appended). On successfull
execution, the program should terminate with code 0.
$ cat students1.csv
sid,email,lname,fname,cgpa
2123123513,
[email protected],Moe,Jack,3.1
... truncated for brevity ...
2161242311,
[email protected],Cruz,Pedro,2.5
2161242311,
[email protected],Lopez,Angela,3.5
$ ./topcgpas students1.csv output.csv
$ echo $?
0
$ cat output.csv
sid,email,cgpa
2161242311,
[email protected],3.5
2169133134,
[email protected],3.4
2173413424,
[email protected],3.3
2178423131,
[email protected],3.1
2123123513,
[email protected],3.1
$
11. (1 Point) If the program was not able to open an output file for writing, it should display an error message
and terminate with code 1.
$ ./topcgpas students1.csv cannot.csv
Error! Unable to open the output file cannot.csv
$ echo $?
1
$
12. (1 Point) The program should create the output file ONLY if it is going to produce valid output.
13. (3 Points) If the input file has less than 5 students, they should still make it to the output, in the proper
format and order as described above.
14. (3 Points) If there are any other students with the same CPGA as the fifth student that was written to the
output, all those additional students must also be written to the output file (i.e., the total number of students
written to the output file can go above five in this particular case).
15. Your program must read through the input CSV file ONLY ONCE, adding a student record in the linked
list as you read through each student data in the input CSV file. After that, your program logic should
then process this linked list to find the top CGPA students. You should not copy over the student / CPGA
information into a new array, etc. -10 points deduction if any of this is not followed.
You can use the below structure as the “node” of the linked list.
struct StudentRecord
{
long sid;
char email[30];
char lname[20];
char fname[20];
float cgpa;
struct StudentRecord *next;
};
See the HINT section for some helpful suggestions.
16. If the program runs out of memory when using malloc or calloc, it should display an error message and
terminate with code 1.
3
$ ./topcgpas students1.csv output.csv
Error! program ran out of memory
$ echo $?
1
$
17. The program must be completely written in C, and should not involve shell scripts, commands, etc. (for
example invoked using the system function call. Any such approaches would result in an automatic 0 for the
entire assignment.
18. You are only allowed to use the library functions from the libraries stdio.h stdlib.h, and string.h. Feel
free to write your own functions.
19. The format of the output file should match what is given above, any variations would result in -2 points
deduction.
20. If your program crashes for one or more test cases, it will result in an additional, -4 point deduction from
the total assignment score in addition to losing appropriate points for those test cases.
21. If you program allocates memory using malloc or calloc, make sure that you free all of them explicitly at
the end of the program (even if it terminates without producing an output). Not doing this would result in
-3 points deduction.