Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

BIRLA INSTITUTE OF TECHNOLOGY & SCIENCE, PILANI

First Semester 2010-2011


CS / IS C363 Data Structures and Algorithms
[Section 1 Thursday, 8th August] Lab Sheet 1
[Duration: 150 minutes]
General Instructions for Programming
Linux command to download the labsheet and support files to the local machine
$scp

-r <username>@blade3:~/<foldername>

Linux command to upload all the program files back to the server.
$ scp -r <dir containing their files> username@blade3:~
1. All inputs to the program must be either (a) command line arguments (b) or read from a file (other than stdin). DO NOT
READ anything from stdin and DO NOT USE PROMPTS like Please enter a number .
2. You are required to write the output to a file (other than stdout) and errors if any to a different output channel (stderr or
another file).
3. Use standard C coding conventions for multi-file programs. Separate the following: interfaces of functions (use a .h
file), data type definitions (use another .h file), ADT / algorithm implementation (use a .c file), and driver/test code
(use another .c code). In general, each module has to be written in separate c files.
4. All files related to a lab must be put inside a single directory by the name of the lab (lab1, lab2, etc.).
5. Valid makefile must be present in the directory.
6. Ensure that all the code written by you are compiling correctly. Preferably use gcc with the options -W -Wall -O2,
while compiling your code.

Problem 1
Write a stock inventory verification system for a fruit stall. The system must take purchase and sale letters and
construct the current stock. The current status of stock has to be written back to files named boxes.txt and
stock.txt. This program takes in as input parameters the list of files containing the purchase and sale terms. The
input file indicates all the buys and sells carried out since the previous update, typically storing the day-to-day
activities. After all the operations, the current list of boxes has to be maintained in the file named boxes.txt. The
cumulative stock values are to be maintained in the file stock.txt.
All stock arriving in boxes shall be maintained as it is, till the stocks are cleared by means of sale. To keep track
of the boxes, each box will have a unique identification number. The program should first identify the already
available boxes (as given in boxes.txt). Then, it must gather new purchases/sales as provided by the input files.
These files typically list down all the transactions to be reflected in the stock. For the sake of this program, a
transaction constitutes buying/selling of fruits typically on a day-to-day basis. The changes in stock due to these
transactions have to be reflected back into the available set of boxes in the stall (boxes.txt) and available stock
of various fruits (stock.txt). When a new box (buy entry in the input file) is being added as part of a purchase,
the box has to be provided a unique identification number, typically assigned as one more than the highest box
number assigned. When encountering a sales request (sell entry in the input file), the oldest stock is cleared
first.
The data structures to be used, the file format for various files and the general step-by-step process for this labs
task are being stated in the remainder of this document.
Data structures to be used:
1) Stock: This contains the list of all boxes, ordered in chronographic order of their purchase.
Stock is maintained as a dynamic list of boxes.
2) Box: This represents the contents of each box purchased. This is maintained as a dynamic list of fruits.
Each entry in the list will contain the tuple (item_name, num_of_this_item).

3) Inventory: This list contains the current cumulative sum of the fruits in the stall. This dynamic array stores
the tuple (item_name, total_num_of_this_item).

File Formats:
The file format for the input files is as follows:
<buy/sell> <num1><item1> <num2><item2> <numN><itemN>
Note: There is no space between number of items and item name. End Note.
For ex:
buy 3Mango 4Apple 1Kiwi
sell 2Mango
buy 10Apple 5Orange
The file format for boxes.txt is as follows:
<box_number> <num1><item1> <num2><item2> <numN><itemN>
Note: There is no space between number of items and item name. End Note.
For ex:
1 3Mango 4Apple 1Kiwi
2 2Mango
3 10Apple 5Orange
The file format for stock.txt is as follows
<item> <totalcount>
For ex:
Apple 14
Mango 3
Kiwi 2

Step 1:
Create new type definitions: (Box.h)
(a) Create a type definition for Box, containing a list of tuple (item_name, num_of_this_item). Note
that since the number of items in each box cannot be known apriori, this has to be maintained as
a linked list only.
Create new type definitions: (Stock.h)
(a) Create a type definition for Stock, containing the list of Boxes.
(b) Create a type definition for Inventory, containing the array of tuples (item_name,
total_num_of_this_item). Note that we do not know how many types of fruits are present in the
stall.
Apart from these types mentioned, you can, if you need, add more types to help organize the data
structure in a better way.
Step 2:
Design Stock ADT with the following operations (StockOps.h)
(a) Stock * initializeStock(FILE *file) : reads the file boxes.txt and populated the
Stock list. This function should return NULL and gracefully exit, if the structure stock cannot
be created.
(b) int addNewTransaction(FILE *file, Stock stock) : reads the contents of the
input file and includes the transaction into the Stock stock. A line starting with buy is a
new box to be added to the stock. A line starting with sell may lead to deletion of a box. In the
event of a sale, we should be removing the oldest stocks of the fruit(s) first. To maintain
accuracy of stock.txt, this function will read the entries of stock.txt first into Inventory, keep

updating the entries in Inventory as each line is read, and commit the final tally back to
stock.txt.
(c) void destructor (Stock stock, Inventory inv): recovers the memory
allocated to all the data structures dynamically created.
The function (b) listed above uses the following helper fiunctions.
(a) static Box* nextBox(Stock stock, Box* current_box): returns the next
box in stock. If no more boxes are there, this function will return NULL.
(b) static int addBox(Stock stock, Box* new_box): adds the new box into
stock.
(c) static void addToInventory(Box *box, Inventory ** inv): adds the box
into inventory.
(d) static int writeToFile(Inventory* inv): writes the contents of Inventory*
inv into stock.txt.
Step 3: Implement the above operations in the corresponding c files
Note that since nextBox, addBox addToInventory and writeToFile are helper functions, they need to be visible
only inside addNewTransaction.c and hence should be declared static.

Step 4: driver file.


Create a simple driver file (stall.c) that can read multiple input files corresponding to the various
transactions. The names of input files are passed as command line parameters. Note that if no input file is
passed, the program should exit gracefully indicating the same.
The executable stall generates output files stock.txt and boxes.txt.
Tasks to perform:
1) Write the relevant header files Stock.h, Box.h and StockOps.h.
2) Write the makefile.
3) Implement the driver file stall.c.
a. Implement a simple prototype for the driver file with calls to various functions in
StockOps.h as stubs.
4) Implement initializeStock.c.
a. Write the function initializeStock and all relevant helper functions ,if any.
b. Make suitable modifications in the driver file to test whether the function
initializeStock is correctly implemented.
5) Implement addNewTransaction.c.
a. Write the function addNewTransaction and all relevant helper functions ,if any.
b. Make suitable modifications in the driver file to test whether the function
addNewTransaction is correctly implemented.
6) Implement destructor.c
a. Write the function destructor and all relevant helper functions ,if any.
For the purposes of testing, you may implement some functions to print the data structures or other test data.
But all such functions must be commented before submission to server.

Deliverables:

Stock.h,
Box.h,
StockOps.h,
initializeStock.c,
addNewTransaction.c, destructor.c, inventory.c, makefile.

You might also like