Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf ·...
Transcript of Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf ·...
![Page 1: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/1.jpg)
Introduction to Computer
Science and Programming in
CSession 25: December 4, 2008
Columbia University
![Page 2: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/2.jpg)
Announcements
Final Exam: Tuesday, 12/16, 1:10 pm - 4:00 pmMudd 233 (our normal room)
2
![Page 3: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/3.jpg)
Variables Revisited
What actually happens when we declare variables?char a;
C reserves a byte in memory to store a.
Where is that memory? At an address.
Under the hood, C has been keeping track of variables and their addresses.
3
![Page 4: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/4.jpg)
Pointers
We can work with memory addresses too. We can use variables called pointers.
pointer: an address variable
All pointers are the same size, regardless of what they point to
4
![Page 5: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/5.jpg)
Pointer Operators
Declaring a pointer variable:int * x_ptr; /* declares a pointer to an int */
The & operator means “the address of this thing”
The * operator means “the thing this points to”
5
![Page 6: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/6.jpg)
& and *
int * x_ptr; /* declares a pointer to an int */int x, y;
x_ptr = &x; /* set x_ptr to the address of x */
y = *x_ptr; /* set y to whatever x_ptr points to */
/* is equivalent to */y = x;
6
![Page 7: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/7.jpg)
Some vocabulary
* operator is also known as dereference
a pointer references a variable in memory
7
![Page 8: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/8.jpg)
Pointers and Arrays
C blurs the distinction between pointers and arrays
When we declare an arraychar A[10];
what is A?
A can be treated as a pointer to the first element of A
8
![Page 9: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/9.jpg)
Pointers and Arrays
In other words, the following two lines are equivalent:
char * array_ptr = &A[0];
char * array_ptr = A;
This also means the following:
A[0] == *array_ptr
A[1] == *(array_ptr+1)
9
![Page 10: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/10.jpg)
Pointers and Arrays
When we want a function to be able to modify the value of a variable, we pass it by referencesscanf(price, “$%f”, &dollars);
Because arrays are basically pointers, this happens automatically when we pass arrays to functions.
For example: strcpy(stringA, stringB);
10
![Page 11: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/11.jpg)
Pointer Arithmetic
What if A was an array of ints?A[1] == *(array_ptr+1) ??
Yes. C automatically keeps pointer arithmetic in terms of the size of the variable type being pointed to.
Be careful to keep track of what C does for you and what it does not.
11
![Page 12: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/12.jpg)
Memory Management
We discussed before that C does not like to initialize arrays with variable sizes.
To get around this, you can use stdlib.h’s malloc() command.
malloc() stands for memory allocation.
malloc(N) returns a pointer to an allocated block of memory of N bytes.
12
![Page 13: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/13.jpg)
malloc()
Typical usage:int N = 40000;char *giantString = malloc(N*sizeof(char));
Returns a null pointer if malloc fails.
When we are done with the memory, we can free it with:free(giantString);
13
![Page 14: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/14.jpg)
Memory Leaks
int N = 40000;char *giantString = malloc(N*sizeof(char));strcpy(giantString, argv[1]);giantString = malloc(N*sizeof(char));
Now a huge block of memory is allocated but the program has no way of finding it.
If this code runs a lot, the amount of memory the program is using will keep growing.
14
![Page 15: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/15.jpg)
Measuring Algorithms
In Computer Science, we want to be able to describe the running time and memory requirements of our algorithms
A couple challenges:
Running time and space typically depend on input size
Algorithms are run on different machines
15
![Page 16: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/16.jpg)
Measuring Algorithms
For varying input sizes, we can write our time and space requirements as functions of N.
For varying implementation, we need our description to not care about constant factors.
16
![Page 17: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/17.jpg)
Example
What is the running time of a function that sums an array of size 5 on a machine that takes 2 seconds to add numbers?
What if array is size N?
What if it takes c seconds to add?
17
4 * 2 = 8
2(N-1)
c(N-1)
![Page 18: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/18.jpg)
Big-O
g(n) = O(f(n))means that for some cg(n) ≤ c(f(n))
In other words, big-O means less than some constant scaling.
In big-O notation, what is the running time to sum an array of size N? c(N-1) =
18
O(N)
![Page 19: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/19.jpg)
Sorting
One of the most studied problems in CompSci
We are given N numbers
Put the numbers in order
least to greatest, greatest to least, alphabetical, etc.
compare two numbers at at time
19
![Page 20: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/20.jpg)
Algorithm for Sorting
In English: Given 50 index cards with numbers on them, how do you put them in order?
Lots of different algorithms. We’ll go over three
20
![Page 21: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/21.jpg)
Bubble Sort
Worst algorithm ever
Start at beginning of deck
Compare current and next cards. If next card should be before current, swap. Move to next card.
Keep passing through deck until no more swaps necessary.
21
![Page 22: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/22.jpg)
Selection Sort
Smarter cousin of Bubble Sort
Find the smallest unsorted card
Swap smallest with the first unsorted card
Consider that card sorted, and repeat
22
![Page 23: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/23.jpg)
Merge Sort
If deck is 2 or less cards, just sort and return
Split deck into two halves
Merge Sort each half-deck (recursion!)
Then, merge the two half-decks:
Look at top of each deck. Take the smallest of the two. Repeat until decks are combined.
23
![Page 24: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/24.jpg)
Running time
Bubble Sort: O(N^2)
Selection Sort: O(N^2) But the algorithm seems better organized.
Merge Sort: O(N log(N))
24
![Page 25: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/25.jpg)
Pseudocode
Mix of English and programming language
Use programming constructs to keep thoughts organized: loops, conditionals, variables
But use any syntax that is clear and consistent
And use functions that are obvious to abstract busywork
25
![Page 26: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/26.jpg)
Pseudocode example
print “Enter your friends’ names:”while input is not “quit” input = keyboardInput add input to array Contacts
sort Contactsoutput Contacts
Even though this is a simple piece of code, if it were written in C, it would be much harder to understand
26
![Page 27: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/27.jpg)
Modular Programming
modular - Designed with standardized units or dimensions, as for easy assembly and repair or flexible arrangement and use: modular furniture; modular homes.
Organize programs into interchangeable parts
Keep functions that deal with a certain type together, but separate them from functions that deal with other types.
27
![Page 28: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/28.jpg)
28
calendar.c
struct appointmentsort()
addEvent()cancelEvent()
printDate()printMonth()printWeek()
...main()
![Page 29: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/29.jpg)
29
calendar.c#include “calendar.h”
main()
calendar.hstruct appointment
<function declarations>
print.c#include “calendar.h”
printDate()printMonth()printWeek()
event.c#include “calendar.h”
sort()addEvent()
cancelEvent()
![Page 30: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/30.jpg)
Pointers to pointers
Recall that C arrays and pointers are basically the same:int A[10];int *A_ptr = A;
How does C store 2d arrays?int B[10][10];
B is a pointer to an array of pointers
30
![Page 31: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/31.jpg)
int **
int * int
Pointers to pointers
31
B B[0]
B[1]
B[2]
B[3]
B[4]
B[0][0] B[0][1] B[0][2] B[0][3] B[0][4]
B[1][0] B[1][1] B[1][2] B[1][3] B[1][4]
B[2][0] B[2][1] B[2][2] B[2][3] B[2][4]
B[3][0] B[3][1] B[3][2] B[3][3] B[3][4]
B[4][0] B[4][1] B[4][2] B[4][3] B[4][4]
![Page 32: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/32.jpg)
malloc()
We can dynamically allocate multi-dimensional arrays
int **C;C = (int**) malloc(N*sizeof(int*));
for (i=0; i<N; i++) { C[i] = (int*)malloc(N*sizeof(int));}
32
![Page 33: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/33.jpg)
Pointers to functions
It is occasionally useful to use pointers to functions
Since functions are stored in memory, we can reason about their addresses too
This allows us to say, “run the function at address _____ on these arguments”
Useful for being truly general, e.g. stdlib qsort
33
![Page 34: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/34.jpg)
Function Pointer Syntax
int (*f_ptr)(); /* pointer to function that returns an int */
Parentheses are important. Without parentheses, f_ptr looks like it returns a pointer to an int.
int (*f_ptr)(int, int); /* function takes 2 ints as arguments */
int greater_than(int a, int b);f_ptr = greater_than;
34
![Page 35: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/35.jpg)
qsort example
Stdlib’s qsort function is a general sorting function.
Sort an array of any type, using any comparison criterion
Define that comparison as a function pointer
void qsort(void *base, size_t n, size_t size, int (*cmp)(const void *, const void *));
35
![Page 36: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/36.jpg)
qsort example
Compare function should take two entries A and B,
return +1 if A>B
return -1 if A<B
return 0 if A==B
36
![Page 37: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/37.jpg)
qsort exampleint greater_than(const void *x, const void *y){ float *a = (float*)x, *b = (float*)y;
if (*a>*b) return 1; if (*a<*b) return -1; return 0;}
void mySort(float A[], int N){ int (*f_ptr)(const void *, const void *) = greater_than; qsort((void*)A, N, sizeof(float), f_ptr);}
37
![Page 38: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/38.jpg)
Linked Lists
Store each element in a struct that contains the data and a pointer to the next struct: a node
Keep a pointer to the first node
Following a linked list is like a scavenger hunt
38
![Page 39: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/39.jpg)
Linked Lists
struct node { int data; struct node * next;};
struct node *start;
How do we add a node at beginning of list?
Allocate new node, set next pointer to start, set start to new node.
39
![Page 40: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/40.jpg)
Linked ListsHow do we add a node to the end of the list?
Follow pointers to last node, allocate new node, set last node’s next to new node.
How do we add in the middle of the list?
Set previous node’s next to new node, set new node’s next to next node.
How do we delete a node?40
![Page 41: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/41.jpg)
Doubly Linked Lists
Keep a next pointer and a previous pointer.
A little extra work for adding and removing, but allows for faster backtracking.
41
![Page 42: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/42.jpg)
Binary Trees
Finding an item in a list or array is usually an O(N) operation.
We can create a structure that makes it faster (at a cost; a tradeoff)
We use a tree structure, which is like a linked list, except each node has more than one pointer.
42
![Page 43: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/43.jpg)
Binary Trees
Binary tree: Each node has left and right child.
Left child is less than, right child is greater than
struct node { int data; struct node *left; struct node *right;}
struct node *root;
43
![Page 44: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/44.jpg)
Binary Trees
Inserting number x into a Binary Tree:
0. Start at root
1. If current node is NULL, create new node and set node to x
2. Otherwise, if x >= current node, follow right pointer, else follow left pointer. Goto 1.
44
![Page 45: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/45.jpg)
Binary TreesFinding an item x in a binary tree:
0. Start at root
1. If current node is x, return
2. If x >= current node, follow right pointerelse, follow left pointer
3. If node is NULL, return “not found”, otherwise goto 1.
45
![Page 46: Introduction to Computer Science and Programming in Cbert/courses/1003/lecture25.pdf · Introduction to Computer Science and Programming in C Session 25: December 4, 2008 Columbia](https://reader030.fdocuments.us/reader030/viewer/2022041103/5f02855f7e708231d404ac99/html5/thumbnails/46.jpg)
Binary Trees
On average, lookup and insertion take O(log N) time
But worst case is still O(N)
46