CS 24102 : DATA STRUCTURE AND ALGORITHMS LABORATORY
Lab Assignment No : 2 Day & Date : Monday, August 11, 2025
Sections : IIIC Premises : SH3
Faculty : Akriti Nigam
Read all the handouts shared with you in Lab 1.
Objectives: File i/o of C++; familiarity with use of class – string class of C++; design a class to solve a problem.
Resources Required : Linux server; g++ software; class fstream, class string
Skills to be learnt : use of file i/o; use the class strings of C++; data and function members of a class; separation
of class definition from its use in problem solving
Reinforcement of Concepts from Theory : definition and use of 1D and 2D arrays
Set-up : After logging in, create a new directory, Lab2 and create 4 subdirectories under Lab2, suitably named
as, “assignment”, “handout”, “programs” and “submission”. Copy all files from /home/sb/Lab2/*.* to your
Lab2 directory and place the files in the appropriate directories.
Problem 1. [Get familiar with string library of C++; estimated time : 1 hr]
• Read the program given to you as “string-functions.C” and observe how i) a string object is defined and
initialized,
ii) the basic operators are extended to strings {=, ==, !=, relational operators}, iii) i/o of strings with cin,
cout and cerr, iv) access string elements as a one-dimensional array, and v) member functions of this
class are used on a string object.
• Compile and run the executable and observe its output. Change the input and again observe the output.
Save the results in a “work-done-lab2.txt” file
• Execute the following command : $ echo $PATH > pathnames.txt
Echo is the shell command for output and $PATH is a shell variable that lists all directories that are used
by shell to search the commands we submit to shell. You can browse the contents of “pathlist.txt” and it
will be of the form :
/home/sb/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/
games:/snap/bin
Each directory is separated from the following one by “:”
• Read the program “parsefinal.C”, whose purpose is to count and list all directories in the PATH variable.
Compile and run the program and save its output to the “work-done-lab2.txt” file.
• You have to change the program to parse the input given in the file, “namedata.csv” and generate similar
output. Save your results in the above file.
Problem 2. [Design a class for simplifying the attendance management problem for a faculty; estimated time 1
hr]
The attendance problem is defined as follows. The inputs are : class strength, maximum number of lectures, list
of roll numbers (integers) of all students, for all the dates on which a lecture has been conducted - date of the lec-
ture and list of students present. The system is to be designed in such a way that it can a large variety of queries,
such as
• Read the class definition given in the file, “attendance-defs1.h” and its use in the file, “attendance-
use1.C”. Note that it uses file input feature of C++ to read the list of roll numbers from a file, “rol-
list.txt”.
• Compile and run the program as follows. Note how the input file is given in the command line; should
not use input redirection operator “<” of the shell; because here the file input feature of C++ is used.
$ g++ attendance-use1.C -o attend1
$ ./attend1 rollist.txt
• Observe the output and save in the designated file. Change the input and test the working of the class de-
sign and save the output.
• You have to change the class design given above by introducing the following enhancements. The new
application of Attendance class is given in the file, “attendance-use2.C”. Identify the changes in this
program as compared to “attendance-use1.C”. Now make changes to the definitions file, “attendance-
defs1.h” and name it “attendance-defs2.h”. You have to ensure that the program in “attendance-use2.C”
compiles without error and when used in the form :
$ g++ attendance-use2.C -o attend2
$ ./attend2 rollist.txt roll-list2.csv attend-data2.csv
Then your output should match with the contents of the last two files in the above command.
Problem 3. [Enhance the Attendance class of Problem2 to perform useful computations; estimated time 1 hr]
The design so far can perform file i/o to initialize an object and test some of the member functions. Now you
will have to add features so that it can accept attendance data from the faculty and save it for further processing.
Consider the data of students present given in the file, “date-present-list.txt”. Create new versions, “attendance-
defs3.h” and “attendance-use3.C” so that when compiled and executed as follows :
$ g++ attendance-use3.C -o attend3
$ ./attend3 rollist.txt roll-list3.csv attend-data3.csv
The desired output files , “roll-list3.csv” and “attend-data3.csv” are included for your reference.
Finally, Before you logout make sure that you have saved the file “work-done-lab2-file.txt” in the submis-
sion directory.
Take Home Assignment
You are now in a position to add more queries as function members in the new class design, such as
• given date, roll no : get the attendance entry
• given date, roll no, new entry : edit attendance of a student
• given two dates as range, list attendance record for all students for all dates in the range
• Given a certain attendance % as cut-off , list all students below the cutoff
• List all students whose attendance is over a given % (say 90)
• List all students with low attendance number of lectures they must be present in the lectures to follow
• Add more functions that appear to be interesting to you.
In order to implement the new functions, you may add appropriate data members for efficiency.
Attachments
1. Handouts – class-c++.pdf
2. Programs : string-functions.C parsefinal.C attendance-def1.h attendance-use1.C
attendance-use2.C
3. Files : attend-data2.csv attend-data3.csv date-present-list.txt namedata.csv
path-list.txt rollist.txt roll-list2.csv rolllist2.csv
roll-list3.csv rolllist3.csv rolllist.csv
End of Lab Assignment No. 2