CS 1401, Exam #4, version a

Date: Wednesday, November 23, 2005
Name (please type legibly, ideally in block letters): ______________________________________________________________________

1. An old-fashioned computer editor asks a user to press S to save a file, E to edit, and Q to quit. Use switch to write down a code that, based on the letter, prints out the corresponding command:

Do not forget to print an error message if the letter is different from S, E, or Q.

2. Write a code that does the same but uses if instead of switch. Explain the comparative advantages and disadvantages of using if, switch, and ?.

3. By using the editor program, you try to process the information about the Thai food places in El Paso. The information about these food places is stored in a file thaifood.dat, one food place per line. Write a code that, given this file, transfers all its information into an array thaiplaces of Thai food places, so that:

4. Write a method that, given an array of strings, returns the number of elements that contain the word "Thai". Start with number = 0, and, inside a for-loop, add 1 if the current element of the array contains the word "Thai". Trace your method on the example of an array consisting of two strings: "Singapore Cafe" and "Thai Delights".

5. Write a method that, given an array of real numbers, returns the product of all the elements from this array. Trace this method on the example of an array of 2 elements: 1.0 and 10.0. Hint: for sum, we start with 0 and add elements one by one. For the product, we start with 1 and multiply elements one by one.