Grid

advertisement
Vectors and Grids
Eric Roberts
CS 106B
April 8, 2009
A Heart-to-Heart Talk about Exams
• I’ve read enough of the warm-up messages coming in for
Assignment #1 to know that there is a lot of fear and loathing
about exams, and a wish that things could be different.
• One comment that seems fairly representative is
>
>
>
>
I loved the class but really disliked the way we were
graded... having to write code for the midterm and
final did not successfully test my knowledge of the
material at all.
• My immediate response is: “Be careful what you wish for.”
We experimented with laboratory exams for a while in the
mid-1990s. We abandoned the experiment mostly because
the failure rate was way too high.
• But why not have a take-home exam or just grade CS106 on
the homework assignments? Well, there’s a problem . . .
A Modest Proposal
• For many years, I’ve been thinking about the possibility of
reducing the weight of the final if we can do something
collectively about the Honor Code problem.
• Here is my proposal: The weight of the final will be
15% + 5% for each Honor Code case filed this quarter
The weight assigned to the homework will be whatever is left
after the announced weights are assigned to the various other
components, subject to a minimum of 15%.
• Thus, if no Honor Code cases come up this quarter, the final
will count for 15%, which is exactly the same as the midterm.
The homework would count for 60%.
• If we file three Honor Code cases (as we did last quarter), the
final will count for 30% and the homework for 45%. And so
on . . .
The Collection Classes
• For the next three days, we will be learning about the classes
in Chapter 4. These classes, for the most part, contain other
objects and are called container or collection classes.
• Here are some general guidelines for using these classes:
–
–
–
–
–
–
These classes represent abstract data types whose details are hidden.
Each class requires a type specification as described on the next slide.
Declaring variables of these types always invokes a constructor.
Any memory for these objects is freed when its declaration scope ends.
Assigning one value to another copies the entire structure.
To avoid copying, these structures are usually passed by reference.
Template Classes
• The collection classes are implemented using the template
facility in C++, which makes it possible for an entire family
of classes to share the same code.
• Instead of using the class name alone, the Vector, Grid,
Stack, Queue, and Map classes require a type parameter that
specifies the element type. Thus, Vector<int> represents a
vector of integers and Grid<char> represents a twodimensional array of characters.
• It is possible to nest classes, so that, for example, you could
use the following definition to represent a list of chess
positions:
Vector< Grid<char> > chessPositions;
Simple Call-by-Reference Example
/*
* Function: SolveQuadratic
* Usage: SolveQuadratic(a, b, c, x1, x2);
* --------------------------------------* This function solves a quadratic equation. The coefficients
* are supplied as the arguments a, b, and c, and the roots are
* returned in x1 and x2, which are reference parameters.
*/
void SolveQuadratic(double a, double b, double c,
double & x1, double & x2) {
if (a == 0) Error("The coefficient a must be nonzero");
double disc = b * b - 4 * a * c;
if (disc < 0) Error("The solutions are complex numbers");
double sqrtDisc = sqrt(disc);
x1 = (-b + sqrtDisc) / (2 * a);
x2 = (-b - sqrtDisc) / (2 * a);
}
Methods in the Vector<x> Classes
vec.size()
Returns the number of elements in the vector.
vec.isEmpty()
Returns true if the vector is empty.
vec[i]
Selects the ith element of the vector.
vec.add(value)
Adds a new element to the end of the vector.
vec.insertAt(index, value)
Inserts the value before the specified index position.
vec.removeAt(index)
Removes the element at the specified index.
vec.clear()
Removes all elements from the vector.
The ReadTextFile Function
/*
* Reads an entire file into the Vector<string> supplied
* by the user.
*/
void ReadTextFile(ifstream & infile, Vector<string> & lines) {
while (true) {
string line;
getline(infile, line);
if (infile.fail()) break;
lines.add(line);
}
}
AskUserForInputFile
/*
* Opens a text file whose name is entered by the user. If
* the file does not exist, the user is given additional
* chances to enter a valid file. The prompt string is used
* to tell the user what kind of file is required.
*/
void AskUserForInputFile(string prompt, ifstream & infile) {
while (true) {
cout << prompt;
string filename = GetLine();
infile.open(filename.c_str());
if (!infile.fail()) break;
cout << "Unable to open " << filename << endl;
infile.clear();
}
}
Methods in the Grid<x> Classes
grid.numRows()
Returns the number of rows in the grid.
grid.numCols()
Returns the number of columns in the grid.
grid[i][j]
Selects the element in the ith row and jth column.
resize(rows, cols)
Changes the dimensions of the grid and clears any previous contents.
Exercise: Crossword Numbering
The main problem for today is
to write the code to number a
crossword grid. The grid itself
comes from a text file that has
spaces for blank squares and #
signs for dark squares:
15x15
#
#
#
#
#
#
#
#
##
#
#########
#
##
#
#
#
#
#
#
#
#
#
#
The End
Download