Project 4, due July 17 (Wednesday)
- Now... modify your program to keep a SORTED list of employees, 10 employees long.
- You will accept input from a file consisting of four lines of input per employee
- a first name/last name
- an id number
- a salary
- a position
- there will also be a fifth line, which is whether to continue or not.
- yes or no, alone on a line.
CS151 -- Administration
Project 5, due July 24 (Teusday).Don't sweat it, there will be lots of in class examples... Just pay attention.
- Make a deck of cards. It must be in a file called Deck.C and Deck.h
- Each Deck will read in a set of cards, from a file. The file will be the same type used in project 4 as input to the employees.
- The deck must read in the cards, with the first thirteen entries being the first suit, the second being the second suit and so on. The name of the employee read in should be the value returned as the card number, and the suit should be the title the employee carries. (Program Manager, Programmer, Project Leader, Executive).
- The deck must return a Top card, and mark that card as read, so that it is not selected again, until the deck is re-shuffled. The card returned should be a random card from the deck. This must be:
Card getTopCard();- You must also be able to get the last card drawn. This must be:
Card lastCardDrawn();- You should be able to output all cards not yet drawn as well:
void output();- More information will be given as time allows.
Arrays
Arrays in functions...void search(int array[], int size, int find)
Just use the []
- empty this time
- though you can put something there...
Arrays
Arrays in functions...void search(int array[], int size, int find)
- Always passed by reference
- no reference operator
- always pass with the size of the array!
Searching
Sequential Search (naive)Binary Search (really smart...but has constraints)
Searching
Sequential Search (naive)
- Always works... on anything
- Simply search each element of the array.
Searching
Sequential Search (naive)element seqSearch(element array[],
int size,
element find)
{
for (int i = 0; i < size; i++) {
if (array[i] == find) {
return find;
}
}
}
Searching
Binary Search
- Smart
- Only works on sorted unique arrays.
Searching
Binary Search
- Only works on sorted unique arrays.
- Non-unique
- only returns the first matching element... actually same as sequential search for this.
Searching
Binary Search
- Only works on sorted unique arrays.
- Non-sorted
- Does not work at all.
Searching
Binary Search
- The algorithm (on a smallest to largest sorted array)
- Look at the middle element of the array
- if it is less, then
- cut off the bottom half of the array (all elements less than middle)
- if it is greater, then
- cut off the top half of the array (all elements greater than the middle).
Searching
Binary Searchelement search(element array[], int size,
- The code
element find)
{
int high = size, low = 0;
int middle = (high+low) / 2;while (high > low) {
if (array[middle] < find) {
low = middle;
} else if (array[middle] > find) {
high = middle;
} else {
return element;
}
middle = (high+low)/2;
}
throw notFound;
}
Sorting
Sorting -- make a list ordered...
Sorting
Sorting -- make a list ordered...
- Bubble Sort
- Selection Sort
- Insertion Sort
Sorting
Sorting -- make a list ordered...
- Bubble Sort
- While the array is not sorted,
swap the first neighboring pair of elements that you find that are out of order.
Sorting
Sorting -- make a list ordered...
- Bubble Sort
- void sort(int array[], int size) {
boolean sorted = false;
int top = 0, bottom = size-1;
while (sorted == false) {
sorted = true;
for (j = top; j < bottom; j++) {
if (array[j] < array[j+1]) {
swap(x[j], x[j+1]);
sorted = false;
}
}
top--;
}
}
Sorting
Sorting -- make a list ordered...
- Selection Sort
- Repeat:
- find the smallest element
- swap it with the beginning element
- move the beginning of the array one right
- Until the beginning of the array is the end of the array.
Sorting
Sorting -- make a list ordered...
- Selection Sort
- void sort(int array[], int size) {
for (int i = 0; i < size-1; i++) {
int smallest, smallestYet = 0;
for (smallest = i; smallest < size; smallest++) {
if (array[i] < array[smallestYet]) {
smallestYet = i;
}
}
swap(array[i], array[smallestYet]);
}
}