CPSC 301

2D Arrays and C-type Strings

Design, write, and test a program for the following.

Function: This program reads in information about books and prints out tables containing that information formatted in different ways. For each book, it reads in the title and author’s name. It gives the user a choice of how the table should be ordered: by title or by author. It will print out the requested table, and then ask again for a choice. This should continue until the user enters the signal to quit.

Input: The input will be from the keyboard (standard input) and consists of two parts. The first part starts out with an integer giving the number of books to be input. This is followed by input of one line for each book: the line consists of the first and last name of the author followed by the title of the book. (See sample run below.) The second part of the input simply consists of the user entering one of the following letters: A, T, or Q. The letter A will mean that the table will be sorted by the author’s last name, the letter T will mean it will be sorted by the title, and the letter Q will mean the program is to halt.

Output: After the information is entered the program should print out well-formatted tables showing information about each book. Depending on the user’s input, the data in the table will be sorted in different ways.

Sample run: (You may change the wording and format, but all information asked for should be present and easily read. The user input is in bold type.)

Welcome to CS library program.

Please enter the number of books: 2

For each book, please enter one line consisting the first and last names of the author follwoed by the title of the book.

Ernest Hemingway For Whom the Bell Tolls

Stephen Crane The Red Badge of Courage

The program will display all books sorted according to your choice.

Please enter your one letter choice as follows:

A: sort the books by the last names of the authors

T: sort the books by the titles

Q: quit this program

A

Crane, Stephen, The Red Badge of Courage

Hemingway, Ernest, For Whom the Bells Tolls

The program will display all books sorted according to your choice.

Please enter your one letter choice as follows:

A: sort the books by the last names of the authors

T: sort the books by the titles

Q: quit this program

T

For Whom the Bells Tolls, Hemingway, Ernest

The Red Badge of Courage, Crane, Stephen

The program will display all books sorted according to your choice.

Please enter your one letter choice as follows:

A: sort the books by the last names of the authors

T: sort the books by the titles

Q: quit this program

Q

Pseudocode. You should develop a detailed pseudocode, and have it checked out by the instructor before starting to code.

Programming requirements: For this and all subsequent programs, use functions! In particular, most, if not all, tasks identified in Level 0 of your pseudocode should be functions. The exception would be if the body of a function would consist of only one or two non-compound statements. For this assignment you are required to use arrays and functions and C-type strings, i.e. character arrays, and their standard functions. (This means you will need to include a header file <cstring>.)

You should have at least the following arrays:

nameTitle - array of strings containing: lastName, firstName, title

titleName- array of strings containing: title, lastName, firstName

You should use at least the following C++(actually C) functions:

strcpy // to copy strings

strcat // to concatenate strings

strcmp // needs to be used in sorting

For a sorting algorithm, use insertion sort, selection sort, or quicksort, not bubble sort.

Assumptions: You may assume again that all entries are correct. You may further make assumptions about the lengths of names and titles if you wish; if you do, you need to document it. You may assume that all books have just one author and only the first and last names of the authors are entered.

Testing: You should perform your own tests to make sure that each component of your program is working. At the end, run your program using the input as outlined in the file books.txt . If you like, it is possible to type in the input into a text file using Notepad, and then have your executable file act on it. If you like, you can also capture the output in a text file. To do this, open up a MS DOS window, and then type in

C:\WINDOWS:> FileName.exe < TextFileName.txt > OutFileName

where you can fill in the correct filenames for "FileName.exe", "TextFileName.txt" and "OutFileName". All file names should include the full path unless you are in the same directory (folder). You may need to rename the files to be 8 characters long.

Optional extra: Write an extension of the program that will sort the titles of the books by ignoring any initial “The”, “A”, or “An”. Therefore, “The Red Badge of Courage” will come before “Tess of the D’Urbervilles”. When the title is printed out, it should contain the article in front.

Whenyou should finish the program and have it checked out by the instructor before turning it in via the Titanium site..

Your program including the following internal documentation:

  • Your name, date, assignment topic
  • For each function including the main:

oa purpose that explain what the function does, making sure to include the role played by each parameter

oany assumptions made or credits due (If you took code from someplace for your sort, it should be documented.

1