Program 1
cs550 Operating Systems
Due date: Monday Sept. 8, 2014 at 11:59:59 p.m.
10 points
Objective: To learn about loops, structures, and functions in C.
Specification:
Outline: For Program 1 you are to implement a program simulate a library. Your library will contain an unknown number of books. To store these books you will use a dynamic array of structures. Your structure must store the title of a book, the first name of an author, the last name of an author, and the Dewey Decimal Number for each book. You are to perform several operations on your array; i.e., you must sort the books in the library by title, by author, and by call number, and then print them out. To implement this program, you are to create four files. The first file is a makefile that will allow you to compile the other three files. It must be named makefile. The second file will contain constants, global variables, and function prototypes. You are to name this file Library.h. The third file will contain your function definitions. This file will be called Library.c. The last file will be named program1.c. This file will contain your main function and the code to run your program.
Main
In your main function (i.e. in program1.c ) you will do very little work. All that you will need to do is to call the function to make your library; next, print your books; then sort your books, printing them after each sort. You must sort your books in the following order: by title, by author, then by call number. At the end of your main call the destroyLibrary function to clear the memory used by your books.
Global Variables and Types
You may use the following global variables in your program (you may change the names if you wish, however you may not use any other global variables):
numBooks - the number of books in your library. This must be of type int.
books - an array that contains all of the books in the library. This must be of type Book *.
struct Book - a structure that contains all of the information pertinent to one book in the library. This structure must contain the following variables:
authorLastName - a string containing the last name of the author. This must be of type char *.
authorFirstName - a string that holds the first name of the author. This must be of typechar *.
title - a string that holds the title of the book. This must be of type char *.
callNumber - the array that holds the call number of the book. This must be of type double.
All global variables and types must be declared in Library.h.
Functions
You are to use seven functions in your program. All of these functions must be given prototypes in Library.h and defined in Library.c.
The first of these functions will be called makeLibrary. This function will take no parameters, and it will not return any values. The purpose of this function will be to read in the number of books, create the array to store the books, and read in the data for each book.
To implement this function you will need to allocate dynamic memory for the array of books. To do this, read in the number of books that are in the library from the keyboard, then allocate the necessary memory using malloc with the appropriate size and type. You may assume that the size of each string will be no greater than 100.
After you have allocated the necessary amount of memory, you must read in your data. You MUST read in your data in the following format:
title, author's last name, author's first name, call number
You may assume that commas separate each field; however, the title and author's names may contain more than one string separated by whitespace.
Hints:
The next function you will need is called printLibrary. This function will take no parameters, and it will not return any values. The purpose of this function is to print all of the books in the library. This function must produce the following output for each book in the library:
Title: Book
Title
Author: Last Name,
First Name
Dewey Decimal Number:
Number
Now, you will need three more functions named sortByTitle, sortByAuthor, and sortByCallNumber respectively. These functions will take no parameters, and they will not return any values. Their purpose is quite obvious: to sort the books. Each of these functions must sort the books in ascending order by the appropriate parameter using a sorting algorithm of your choice.
Hint: use strcmp to aid in sorting strings.
Since you will be using a sorting algorithm, a swap function will be essential. Your swap function should be called swapBooks. This function must take two parameters (i.e. the array indices to be swapped) and it will return no parameters. Note that the array indices will be integer values, not pointers. Given the indices, the function must swap a pair of books in the array. Note that since books are referenced by pointers it will be sufficient to swap the reference to each book in rather than swapping all the data of each book.
Your last function will be named destroyLibrary . This function will take no parameters and it will not return any values. The purpose of this function is to free all memory allocated by the library. To do this you will need to use the free function provided by C.
Persons failing to complete the destroyLibrary function or implementing the destroyLibrary function incorrectly will be penalized on this programming assignment. Be sure to come to class and listen about how to implement this function correctly.
Example
An example of input and output for the library follows:
Input:
4
Numerical Linear Algebra, Bau, David, 32.601
Fundamentals of Digital Image Processing, Jain, Anil, 621.21
Probability and Statistical Inference, Hogg, Robert, 321.009
Artificial Intelligence, Jain, Alan, 825.221
Output:
Title: Numerical Linear Algebra
Author: Bau, David
Dewey Decimal Number: 32.601
Title: Fundamentals of Digital Image Processing
Author: Jain, Anil
Dewey Decimal Number: 621.210
Title: Probability and Statistical Inference
Author: Hogg, Robert
Dewey Decimal Number: 321.009
Title: Artificial Intelligence
Author: Jain, Alan
Dewey Decimal Number: 825.221
Books sorted by title:
Title: Artificial Intelligence
Author: Jain, Alan
Dewey Decimal Number: 825.221
Title: Fundamentals of Digital Image Processing
Author: Jain, Anil
Dewey Decimal Number: 621.210
Title: Numerical Linear Algebra
Author: Bau, David
Dewey Decimal Number: 32.601
Title: Probability and Statistical Inference
Author: Hogg, Robert
Dewey Decimal Number: 321.009
Books sorted by author:
Title: Numerical Linear Algebra
Author: Bau, David
Dewey Decimal Number: 32.601
Title: Probability and Statistical Inference
Author: Hogg, Robert
Dewey Decimal Number: 321.009
Title: Artificial Intelligence
Author: Jain, Alan
Dewey Decimal Number: 825.221
Title: Fundamentals of Digital Image Processing
Author: Jain, Anil
Dewey Decimal Number: 621.210
Books sorted by number:
Title: Numerical Linear Algebra
Author: Bau, David
Dewey Decimal Number: 32.601
Title: Probability and Statistical Inference
Author: Hogg, Robert
Dewey Decimal Number: 321.009
Title: Fundamentals of Digital Image Processing
Author: Jain, Anil
Dewey Decimal Number: 621.210
Title: Artificial Intelligence
Author: Jain, Alan
Dewey Decimal Number: 825.221
To run this program, it is suggested that you use the UNIX redirection commands to provide input and obtain output from your program. Run your program using the following command:
prog1 < p1.in > p1.out
To obtain input from the file p1.in, and to send the output of your program to the file named p1.out. You may give these input and output files any name that you wish since you will not be submitting them. The names provided are only examples
Submitting Your Program:
Note that you must provide informative comments for your code and follow the program guidelines.
To turn in your programs use the eCompanion dropbox for Program 1.
Your source code for program 1 MUST be in files named Library.h, Library.c, and program1.c. You must also submit your makefile in a file named makefile. Your files must compile and run on CSA using the gcc compiler and the make command. An example of a makefile can be found on the examples page of the class website.