c programming

profileQ87h
ces_assignment-13.doc

1

Objectives

Gain hands on experience with programming C.

To Practice the following:

Dynamic memory allocation, handle file IO, loops, setup and use of data structures, and function decomposition.

Submission details

You must only submit (using the submit command on latcs5) An electronic copy of the code (There is NO hardcopy to be handed-in)

See the end of this document for more details

This is an individual assignment. You are not permitted to work as a part of a group when writing this assignment.

Copying, Plagiarism

Plagiarism is the submission of somebody else’s work in a manner that gives the impression that the work is your own. For individual assignments, plagiarism includes the case where two or more students work collaboratively on the assignment. The Department of Computer Science and Computer Engineering treats plagiarism very seriously. When it is detected, penalties are strictly imposed.

Please Note:

While you are free to develop the code for this assignment on any operating system, your solution (C code) must compile and run on the latcs5 system using the cc (or by using gcc) compiler. Code that does not run on the latcs5 system will receive zero marks for implementation.

Delays caused by computer downtime cannot be accepted as a valid reason for late submission without penalty. Students must plan their work to allow for both scheduled and unscheduled downtime.

Given resources

On lms you will see 3 files:-

1. CES_assignment-13.doc ( This document

2. AssignTemplate.c ( the source code template to start coding in

3. sample_input.txt ( This is the sample input file which contains the details of a number of books currently in the “library”. {For a complete explanation of what each entry is see task 2}

Programming Task

Implement a menu driven library catalogue system that allows users to perform various catalogue maintenance tasks. You are only allowed to use the C programming language. You are not allowed to use C++.

Please do not use global variables. Up to 50% of marks will be lost for assignments that use global variables.

Please do functional decomposition. Up to 50% of marks will be lost for students who try to write the entire program in one main function or something similar. There should be at least 5 functions for the assignment. One function for each task 2 to 4. Two functions for task 5. More functions than 5 is also highly recommended. The aim of this rule is to teach you how to pass pointer arguments into functions.

The assignment has been broken up into 5 tasks.

Task 1 [10 marks code correctness]

The program should be menu driven. The user should be presented with the following menu:

1. Load catalogue from file

2. Save catalogue to file

3. Display catalogue

4. Find book from catalogue

5. Quit

After selecting one of the above options the corresponding task should be performed. After completing the selected task, the menu should be displayed again for the user to choose again unless the 5th option is chosen.

Task 2 [26 marks code correctness]

Implement the load catalogue from file menu option. After selecting this option the user should be asked to enter the name of a file to be loaded. The file should then be loaded into an array of structures. The array of structures should have been declared in the main function before the load catalogue function was called. To get full marks for this task you should use run-time (dynamically) allocated memory for the array of structures. If you use static memory allocation you will lose 5 marks. You can assume the file will have the following format.

[Number of books]

[Title of book]

[Author first name], [Author surname]

[Year of publication]

[Replacement cost]

[Title of book]

[Author first name], [Author surname]

[Year of publication]

[Replacement cost]

{The … above represents intermediate catalogue entries.}

Here is an explanation of the above fields:

[Number of books] - This is the number of book entries stored in the file

[Title of book] – A string specifying the title of the book, the string may contain spaces.

[Author First name], [Author Surname] – Two strings which do not contain spaces but

are separated by a comma followed by a space.

[Year of publication] - A number specifying the year that the book was published

[Replacement cost] - Cost of replacing a book, it may contain a decimal point.

Below is an example of a catalogue file that contains 2 books:

2

C for beginners

Peter, Kernighan

1999

102.50

XML data Management

Amit, Chandhri

2005

122.30

One way to copy the sample file (sample_input.txt) from windows i7 into a putty session is to;_

1. opened vi with an appropriate file name,

2. Immediately issue the command in vi :set paste /*then press return*/

3. Then enter insert mode by pressing i

4. Having opened sample_input.txt in notepad on windows

a. Highlight ALL the data there

b. Move pointer over putty, left click to activate putty, then

c. Right click to put data there.

5. It will now appear as follows:-

{The vi “page” on putty:-}

6

Cpp for beginners

Peter, Kernighan

1999

102.5

CPP FOR Beginners

Peter, Kernighan

1999

102.5

CPP FOR Beginners

PeTer, KerNIghan

2001

101.5

XML data Management

Amit, Chandhri

2005

122.3

PHP for the world wide web

William, Hope

2004

202.4

Computer networks

Andrew, Tanenbaum

2004

291.1

You can now save this in vi as usual and commence programming; treating this file as if you had written it!!

Task 3 [12 marks code correctness]

Implement the save catalogue to file menu option. After selecting this option the user should be asked to specify the name of the save file. All the book catalogue data should be stored onto the specified file in the same format as the file loaded in Task 2.

Task 4 [12 marks code correctness]

Implement the display catalogue menu option.

All the attributes of the books should be displayed as follows:

Number of books in catalogue = [number of books]

---------------------------------------------------------------

Title: [title of book]

Author: [first name of author] [surname of author]

Year of publication: [year of publication]

Replacement cost: $[cost of replacement]

---------------------------------------------------------------

....

....

---------------------------------------------------------------

Title: [title of book]

Author: [first name of author] [surname of author]

Year of publication: [year of publication]

Replacement cost: $[cost of replacement]

---------------------------------------------------------------

Note: [title of book] [first name of author] [surname of author] [year of publication]

[cost of replacement] have the same meaning as those for task 2.

Here is an example output:

Number of books in catalogue = 2

---------------------------------------------------------------

Title: C for beginners

Author: Peter Smith

Year of publication: 1999

Replacement cost: $102.50

---------------------------------------------------------------

Title: Java programmers hand book

Author: John Edwards

Year of publication: 2003

Replacement cost: $105.20

---------------------------------------------------------------

Task 5 [15 marks code correctness]

Implement the search book from catalogue menu option. After selecting this option the user should be asked to specify the book using the following sub-menu:

1. Specify book title

2. Specify author first name

3. Specify author last name

After choosing one of the above sub-menu options, the user should be asked to type in a string to be used for finding the book. Note the string has to completely match the relevant book attribute. Partial match is not considered a match.

In the following example the user strings does not match the corresponding books:

User types in for book title: Cforbeginners

Actual book title: C for beginners

User types in for author first name: peters

Actual book author first name: peter

User types in for author surname: peter

Actual book author surname: peterson

The search should be case insensitive. Therefore the following does match:

User types in for author first name: peter

Actual book author first name: peTer

User types in for book title: c for beginners

Actual book title: C For Beginners

The matching books found should be displayed onto the screen in the same format as task 4. If more than one matching book is found then all matching books should be displayed.

Bonus Task [10 marks code correctness]

If you want bonus 10 marks for your assignment and are ready for a challenge then you can implement an extra option to the menu. This is the “Delete books” menu option. Once the user selects this option then he/she ill be asked to do the same as task 5 except the matching books found are first displayed and then deleted from memory. To get the marks for this task the memory for your array of structures must have been allocated using dynamic memory allocation in Task 2. Here you will need to delete that array of structures and create a new smaller one that exactly fits the remaining (non-deleted) books. Note if the search matches more than one book than all the matched books will need to be deleted.

Marking Scheme

The assignment will be marked as follows:

1. Correct execution 75 marks

2. Coding style 25 marks

3. Bonus task 10 marks

1. Coding style will be marked according to the following criteria:

- Use of comments

- Quality of functional decomposition.

- Algorithm design

2. A student can not get more than 100 marks. Therefore a student who gets everything correct and also does the bonus task will still only get 100 marks not 110. However, it is still a good idea to do the bonus task since you are likely to lose some marks somewhere in your program.

Submission Details

· Your program has to run under Unix on the latcs5 or latcs6 machine. System-specific code should not be used in your program, as we emphasize use of portable C. Code that does not compile or run on latcs5/6 will not be marked.

· Please ensure that your name and student number is on every file that you submit.

· You are to submit your files from your latcs5 account. Make sure you are in the same directory as the one containing these files. Type the following commands at the Unix prompt (ensuring that CES is in uppercase):

> submit CES <filename>

where <filename> is the actual complete name of your file.

DO NOT submit any executable files instead submit your source code files only, that is, your progfilename.c. This is so your code (( source code) can be compiled by an examiner in latcs5/latcs6.

{Where progfilename is the filename you have given to your source code.}

After submitting the files, the following command will list the files submitted from your account:

> verify

You can submit the same filename as many times as you like before the assignment deadline; the previously submitted copy will be replaced by the latest one.

Emailing your code will not be accepted and will result in a mark of 0 being awarded for the assignment. Only those assignments that have been correctly submitted using the above “>submit ….” process will be accepted for assessment. There are no exceptions.