Lecture 4/13: Vectors and Grids


April 13, 2020

đź“‚Associated files

The Vector and Grid Classes

CS 106B: Programming Abstractions

Spring 2020, Stanford University Computer Science Department

Lecturers: Chris Gregg and Julie Zelenski

The Stanford Campus


Slide 2

Announcements


Slide 3

Code Mystery

The words 'Code Mystery' with a fingerprint

void mystery(int& b, int c, int& a) { 
    a++;
    b--;
    c += a; 
}

int main() { 
    int a = 5; 
    int b = 2;
    int c = 8;
    mystery(c, a, b);
    cout << a << " " << b << " " << c << endl;
    return 0;
}

Poll:

A. 5 2 8
B. 5 3 7
C. 6 1 8 
D. 61 13 
E. other

Slide 4

Poll answer:

output
A. 5 2 8
B. 5 3 7
C. 6 1 8 
D. 61 13 
E. other

Note: please don't obfuscate your code like this! :(

See the International Obfuscated C Contest for much, much worse examples


Slide 5

A function for Solving the Quadratic Equation

An image of the quadratic formula

ax2 + bx + c = 0

for some numbers a, b, and c.


Slide 6

A Function for Solving the Quadratic Equation

An image of the quadratic formula

/*
 * Solves a quadratic equation ax^2 + bx + c = 0,
 * storing the results in output parameters root1 and root2. 
 * Assumes that the given equation has two real roots.
 */
void quadratic(double a, double b, double c,
               double& root1, double& root2) {
    double d = sqrt(b * b - 4 * a * c);
    root1 = (-b + d) / (2 * a);
    root2 = (-b - d) / (2 * a);
}

Slide 7

Collections

An image of six collections, 'Vector', 'Grid', 'Map', 'Stack', 'Queue', and 'Set'


Slide 8

The Vector Collection


Slide 9

Creating a Vector


Slide 10

Adding elements to a vector

magic:

index:  0   1   2   3 
value: 4 8 15 16

Slide 11

Vectors have useful functions, like size()

magic:

index:  0   1   2   3 
value: 4 8 15 16

Slide 12

A new type of for loop: the for each loop:

magic:

index:  0   1   2   3 
value: 4 8 15 16

Slide 13

Vector Functions


Slide 14

The Grid Container

An image similar to a part of the Matrix movie, with text cascading down the screen making the likeness of Neo, the main character

a0 b0 c0
a1 b1 c1
a2 b2 c2

Slide 15

Grid


Slide 16

Grid Example Code

Grid<int> matrix(2,2); 
matrix[0][0] = 42;
matrix[0][1] = 6;
matrix[1][0] = matrix[0][1];
cout << matrix.numRows() << endl;
cout << matrix[0][1] << endl;
cout << matrix[1][1] << endl;
cout << matrix[2][3] << endl;

Let's draw what happens on each line.

Grid<int> matrix(2,2); // Create a 2x2 grid, with 0 as the default entries

0 1
0 0 0
1 0 0

matrix[0][0] = 42; // put 42 at row 0, column 0

0 1
0 42 0
1 0 0

matrix[0][1] = 6; // put 6 at row 0, column 1

0 1
0 42 6
1 0 0

matrix[1][0] = matrix[0][1]; // put the value from r0,c1 at r1,c0

0 1
0 42 6
1 6 0
cout << matrix.numRows() << endl;
cout << matrix[0][1] << endl;
cout << matrix[1][1] << endl;

Prints the number of rows, then the value at r0,c1, then the value at r1,c1:

2
6
0

cout << matrix[2][3] << endl; // attempts to print out the value at r2,c3

***
*** STANFORD C++ LIBRARY 
*** An ErrorException occurred during program execution: 
*** Grid::operator [][]: (2, 3) is outside of valid range [(0, 0)..(1, 1)]
***
libc++abi.dylib: terminate_handler unexpectedly threw an exception
15:25:55: The program has unexpectedly finished.
15:25:55: The process was ended forcefully.

Grids do bounds checking! If you want to bounds check without crashing, you should call the grid.inBounds(row, col) function, which returns true if the row and column are in bounds for the grid.


Slide 17

Grid Functions


Slide 18

Grid Example: Traversing a Grid

void printGrid(Grid<char> &grid) {
    for(int r = 0; r < grid.numRows(); r++) {
        for(int c = 0; c < grid.numCols(); c++) {
            cout << grid[r][c];
        }
        cout << endl;
    }
}

If we pass in the following grid, what will print?

0 1
0 a b
1 c d
2 e f

Output:

ab
cd
ef

Slide 19

Common pitfalls when working with collections in C++

void printOutGrid(Grid<bool> & grid) {
    for(int i = 0; i < grid.numRows(); i++) {
        for(int j = 0; j < grid.numCols(); j++) {
            cout << grid[j][i];
        }
    }

Slide 20

Let's Code Instagram!

Images of Mike Krieger, Stanford class of 2008, Founder of Instagram


Slide 21

A color is an int, and and Image is just a Grid<int>!

Image of Martin Luther King, Jr. and a blown up image of his eye, so you can see the individual pixels


Slide 22

Let's change the palette of the image

Two images, one with an original palette of colors, and the other with the colors being replaced with their nearest match from a palette, filtering the image.


Slide 23

Let's Code!

Logo for the Qt Creator IDE