Lab 11 In-Class: Arrays

As usual open an xterm, Emacs, and Firefox and create a lab11 directory for your work.

Array Basics

The file contains a variation on the code in pre-lab that read in a list of salaries. The pre-lab problem asked the user for the number of salaries first then created the array and used a count-controlled for loop to read in the salaries. This version is a sentinel controlled that does not ask how many salaries ahead of time. Instead it lets the user keep entering salaries until a negative number is read in.
  1. Download the program and compile it. What is wrong?

  2. Fix the problem above by creating an array of size 5 (MAX).

  3. Compile and run the program entering the following numbers for salaries: 54000, 48375, 15800, 33000, 98600, 21500. What happens? Why?

  4. Fix the problem above by modifying the loop control condition so that the index will not go out of bounds. After the loop add an if statement with the message "Sorry, the program cannot handle more than ___ salaries" in the case that the index went out of bounds (insert the MAX number of salaries instead of the blank).

  5. Run the program again, trying to enter more than 5 salaries. Be sure that instead of a runtime error you get a message saying the program cannot handle more than 5 salaries.

  6. Change MAX to 50, then add code to the existing loop to find the average of the salaries entered. Print the average after the loop. TEST your code!

  7. Add a for loop AFTER the existing code (see the comment in the program) to print each salary that is above average and count them. Before the loop print a statement to label the list - for example "Above Average Salaries". After the loop print the number of above average salaries, appropriately labeled.

  8. Be sure your program is working correctly and is formatted correctly (CTRL-x CTRL-p followed by CTRL-ALT-\) then print it to hand in.

More Basic Array Processing - Grading Quizzes

In this exercise you will write a program that grades arithmetic quizzes (all the answers are integers). The program will ask the user how many questions are on the quiz, then it will ask for and read in the answer key, storing the values in an array of integers. The program will then read in a student's answers and compute and print the number correct and the percent correct.

  1. The file contains an outline of the program. Download it then use the comments as a guide to complete the program.

  2. Test your program using the following input:
    1. Number of Questions: 10
      Correct Answers:  -3  120  45  76  89  -13  1290  -57  99  0
      Student Answers:  -3  120  39  76  82  -13  1290  -57  25  0
      (The student got 7 right which is 70%)
    2. Number of Questions: 4
      Correct Answers:  538  -300  782  45
      Student Answers:  500   300  782  450
      (The student got 1 right which is 25%)

  3. Be sure the program is correctly formatted then print it to turn in.

Some Cryptography - working with type char

One common way to encrypt a message is to replace each letter in the message with a different letter. This is the way the CryptoQuote puzzles in the newspaper work. Each letter in the alphabet (called the plain alphabet) will have a corresponding letter in the cipher alphabet (see the example below). To encrypt a message (called the plaintext) each letter is replaced with the corresponding letter in the cipher alphabet. Using the cipher alphabet below the message "Java is cool" would become "lxix qy fddg".

Plain Alphabet a b c d e f g h i j k l m n o p q r s t u v w x y z
Cipher Alphabet x v f k m z a b q l c g n s d o t e y h j i p w r u

In this exercise you will use this technique (called a substitution cipher) to encrypt messages. The file contains a skeleton of the program. Notice that it uses an initializer list to set up the cipher alphabet in an array of chars. The alphabet is the same as the example above.

  1. Open and add code to perform the encryption. Notice that there is already code to read in the message and convert it to lowercase. So you only need to worry about lowercase letters. To do the encryption for each letter in the message you need to "look up" letter in the alphabet array to see what letter to substitute. You MUST do this using the ideas from the letter counting program on pages 340 - 341 of the text. The main idea is to use arithmetic on characters to index into the alphabet array. Note that characters that are not lowercase letters should just be printed out.

  2. Test your program thoroughly! When it is correct and formatted print it to hand in.

Arrays of Objects

File contains the code for the CDCollection class (pp. 352 - 353) that we discussed in class. File contains the code for the CD class (p. 354) that CDCollection uses, and contains a slightly modified version of the Tunes class (pp. 350 - 351) that uses CDCollection. Save these files to your directory.
  1. Compile and run You should see a list of 6 CDs with a total value of $97.69 and an average cost of $16.28 followed by a menu that lets you add a CD, search for a CD with a given title, or list all CDs for a given artist. Only the add part has been implemented, so add a few CDs to see what happens. When you add the 11th CD (total), you should notice a message that says "Increasing size of array," which comes from the increaseSize() method. This was added just to help you follow the program's execution.

  2. Add the findCDIndex method from the prelab to the CDCollection class. Note that getter methods have been added to the CD class - you need to use getTitle. Be sure your code doesn't look through the array any longer than you have to. A boolean is a nice way to keep track of whether or not you have found the CD and stop the loop when you do find it.

  3. In add a call to your method (under option 2). Note that code is already there to prompt for and read in the title of the CD to search for. Add code that calls your method and prints a message indicating whether or not the CD is in the collection.

  4. Now run Tunes again and choose the search option. Also try adding some CDs, then searching for ones that you added.

  5. Add a method public CD getCD (int index) to the CDCollection class that returns the CD in the collection at the given index.

  6. Modify so that if the CD with the given title is found the actual CD information (the CD object) is printed. You should use the getCD method along with the index returned by your find method. Run the program again to make sure this works.

  7. Now add a method listByArtist that takes a String parameter for the artist, then returns a String consisting of a list of all CDs by that artist. Use the idea in the toString method of the CDCollection class for composing the string to return. Note that toString adds all CDs to the string. You want to just add the ones with the given artist.

  8. In add a call to the listByArtist method and print the list returned. Also remove the 2 "under construction" messages from the menu.

  9. Test your program. Be sure to add more than one CD by the same artist so you can see if your method is correctly getting all CDs. When all is well print the program.

What to Turn In

Turn in hardcopy of,,,, and Tar your lab11 directory and email it to your instructor with cpsc120 lab11 in the subject line.