## CIS22A - Notes for Week 11, 12/2-12/5

### Sorting Arrays

#### Selection Sort

Algorithm

Find the minimum value in the list, swap it with the value in the first position.  Repeat the steps above for the remainder of the list (starting at the second position and advancing each time).

Reference Page

Example Code

How would you change the code to sort a double array, or sort a string array?

### Searching Arrays

#### Sequential Search

Implement STL vectors

Define and initialize vector

Process contents of vector using vector member functions

### Review

One page of notes will be permitted on the final, just like the midterm. No computers, laptops, netbooks, tablets, other electronic devices, or phones are allowed. The final will be 2 hours exactly, even if you show up late. Tests will not be returned.

Types of questions on the final

Write a statement
Write a function
Write main() to demonstrate your function
Write a complete program
What's the output?
Modify a program, and add a capability or change a function

#### Sample Questions

1. Write statements for each of the following. Assume ABC is a 10 element array of float.
a. declare ABC.
b. assign 8.5 for the fourth element of ABC.
c. multiply the third and fourth elements of ABC together and assign the result to the 7th element of the array.
d. print the 8th element of ABC with 2 decimal place accuracy.
e. scan in the first element of ABC.
f. print 2 times ABC[10] with 3 decimal place accuracy.

2. Write functions to perform the following. Assume XYZ is an array of int that is declared in main() . You will have to pass it and/or any appropriate values to the function. Also determine the appropriate return type for the function.
a. print all values in the array in reverse order.
b. prompt the user for a number and read in the value for each element of the array.
c. find the average of the first and last element of the array.
d. find the average of the largest and smallest value in the array.
e, print the highest number in the array to a file, called "biggie".
f. double the value of every element in the array.
g. triple the value of every element in the array that is divisible by 7.

3. Write a function that opens a file, called "charfile", and verifies that it is open. Then it should count the number of characters in the file. Close the file and return the number of characters.

4. Write a function that opens a file, called "intfile". Assume the file contains only ints that are separated by whitespace.  Count the number of ints that have a one's digit of 2 (like 62 82 192 8882, etc), count those that have a one's digit of 3, a one's digit of 5, and a one's digit of 7. Return the sum of the four counts. Use a switch in your solution.

5. Write a function that opens a file, called "outfile". Write the numbers from 1 to 100 into the file, in 5 columns, each having a width of 10.

6. Write a program to read a file of ints. Save every other int into an array, starting with the first one. Assume that there are no more than 10000 ints in the file (but you don't know how many). Find the average of the ints that you scanned and return that number.

Solution

7. Write a program to read a file containing 50,000 records of three columns of numbers. The first two columns are type int and the third is type double. Store all the doubles in an array. Sort the array from low to high and print into a new file. Makeup your own file names.

Solution
Selection sort

Sequential search

STL vector