Midterm -- Well... handed back today.
CS151 -- Shawn Stoffer
CS151 -- Administration
Project 3 due, Yesterday... July 8.Project 1 - handed back.
CS151 -- Shawn Stoffer
CS151 -- Administration
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.
CS151 -- Shawn Stoffer
Computers
What computers do for youAlgorithms
CS151 -- Shawn Stoffer
Input and Output
Thinking about what it ischar cin.get(void); -- a primitive!
>> uses cin.get()
istream& operator>>(istream& in,
string out);int getline(istream& in, string out);
CS151 -- Shawn Stoffer
Input and Output
Thinking about what it ischar cin.get(void);
-- a primitive!
-- REMOVE one character from the input stream, and return it.
CS151 -- Shawn Stoffer
Input and Output
Thinking about what it is>> uses cin.get()
istream& operator>>(istream& in,
string out)
{
while (in.available() == true) {
char ch = in.get();
if (ch != '\n' && ch != ' ' &&
ch != '\t')
{
out += ch;
} else {
in.putback(ch);
return in;
}
}
return in;
}
CS151 -- Shawn Stoffer
Input and Output
Thinking about what it isint getline(istream& in, string out) {
while (in.available() == true) {
char ch = in.get();
if (ch != '\n')
{
out += ch;
} else {
return in;
}
}
return in;
}
CS151 -- Shawn Stoffer
Input and Output
So, what?
- getline does not put the newline back.
- >> does.
- cin.get() gets anything.
Arrays
char ch[10];
- An Array of characters, 10 characters long.
- ch is not a character anymore.. it is now an array.
- You may only access one array element at a time.
Arrays
char ch[10];
ch[3] = 'a';
- You may only access one element at a time.
- [] has a different meaning in declaration and use.
Arrays
char ch[10];
ch[3] = 'a';
- [] has a different meanings
- use (ch[3] = 'a';)
- Access this element of the array.
- declaration (char ch[10];)
- This is the size of the array.
- The array is accessible from 0 to (size-1)
Looping
Natural for arrays.
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;
}