Lab 1 - due Jan 18

1. Arrays. Write a program which creates an array x containing 100 randomly chosen integers in the range 0 .. 99. The main() function asks the user to enter a target number to search for, then tells the user how many times it was found in the array.

You may use this code for generating random numbers:

srand( (unsigned)time( NULL ) ); /* to seed the random number stream for fresh random numbers */

rand()%n; /* to generate a random number in the range 0 .. n-1 */

2. You are to create a personal telephone directory with these capabilities:

  1. Add new entry
  2. Lookup phone number
  3. Change phone number
  4. Remove entry
  5. Print all entries
  6. Exit

A phone entry is a struct with these fields:

firstname: 10 chars

lastname: 15 chars

phone: 15 chars

Use either a struct or a class to create the ENTRY type.

The main() function sets up an infinite loop which presents a menu of choices to the user and carries out the request.

Phone entries are to be kept in an array of size MAXSIZE, which is defined. The main() function declares the array, and keeps track of the next available slot for adding a new entry. When an entry is removed, the entries following it are moved down to fill the gap.

Provide a find function, which asks the user for the first and last names of the entry to find. It then returns the array index of the entry, or -1 if not found. The lookup, change and remove functions start off by calling find.

3. Modify problem 2 in the following ways:

Change the array to be an array of pointers to ENTRY, rather than an array of ENTRY.

The function, which adds a new entry, must allocate space for it using malloc and return a pointer. Removing an entry should free the memory.

4. (Extra credit - 1 point) The find function is passed a match function, which is used to determine if an entry matches the one being looked for. The formal match function takes 2 ENTRY* parameters and returns true or false. The actual match functions passed to it are:

·  matchByFname

·  matchByLname

·  matchByBothNames

Add menu choices to see if the user would like to search by first name, last name, or both.

5. (Extra credit - 1 point) Add menu choices and functions to save your telephone directory into a file and load it from the file. Use fread and fwrite.

Lab 2 due Jan 25 (subject to minor change)

You are to rewrite the phone directory problem from Lab 1 in an object-oriented way. Use classes with public operations and private data members. For an example to follow, see the Time class in Section 3.2 of the textbook. Use at least the following classes:

Entry - used to store a single phone entry. Provide public constructors and an operations addEntry, which interacts with the user to fill in the fields of an entry. Also, an operation modifyEntry to change existing entry information. Provide operations bool matchByFirstName(Entry e) and bool matchByLastName(Entry e). Also, an operation to print an entry to an output stream. Finally, overload the < operator so that an entry can be printed directly to a stream. See the textbook p.107 to see how to do this.

Directory - used to store a collection of Entry. The collection should initially be an array, as in Lab 1, but should be declared private, to allow for change to another kind of collection later. Public operations allow adding a new entry, looking up an entry by either first or last name, printing the whole phone book, and removing an entry. Operations are also provided for storing the directory to a file and loading it from a file.

Interface - used to present a menu to the user, to get the menu choice and call on one of the directory operations.

A stand-alone main(), which is where the console application starts and ends. The main() constructs the directory (either a new one or by loading form a file). main() has variables for the directory and the interface. After starting up it calls the interface, which takes control until the user chooses to exit.

Lab 3 due Feb 1

p. 164 #1

Lab 4 due Feb 8

p. 164 #5

p. 207 #2

Lab 5 due Feb 22

p. 208 #12, #14ac

Lab 6 due Mar 1

1. p. 226 #5,6,7,9

2. Write a function named average, which is passed a queue of doubles, and returns their average. Write a driver to test your average function.

3. Extra Credit (2 points) p. 227 #18

Lab 7 due Mar 15

1. p. 267 Exercises 4,5,6 (For 6 use a simple sort like bubble sort)

2. p. 315 #4,5,6 (drivers to test template functions in part 1)

Lab 8 due Mar 22

1. p. 289 Exercise 17 (Convert the Queue class from Exercise 5 of Sec 5.2 into a class template.)

2. p. 315 #11 (test your queue class)

3. p. 290 Exercise 15 (A template function to see if the values stored in a vector are in ascending order)

4. p. 315 #9 (Test the above function)

Lab 9 due Mar 29

1. p. 317 #28 prime factorization

2. p. 317 #30 Memory Recall queue

3. (extra credit - 2 points) p.315 #18 Pascal's Triangle

Lab 10 - due Apr 5

p. 389 5a

p. 464 1

In this lab you will create a polynomial class. The book suggests using a linked list to hold the terms. You may find it easier to use a vector. That's how I did it. If v is the vector, v[i] holds the coefficient of the term for xi.

Your program should allow the user to enter the polynomial by typing it in using "^" for powers. You should get the entire line containing the user input, and construct a polynomial from it. To do this, I built a constructor for Polynomial, which takes a string as parameter. It eliminates spaces from the string, and then parses it into tokens representing terms, using the "+", "-" and end-of-string as delimiters. It then parses the tokens, picking out the coefficient and power.

Lab 11 - due Apr 12

p. 465 20

Extra Credit (2 points) Modify the Polynomial class from Lab 10, so that the terms are stored in descending order (by exponent) in linked lists of nodes.

Lab 12 – due Apr 19

p. 450 20 (Write a complete LinkedList class template)

p. 464 9 (Write a driver to test your LinkedList class)