310 likes | 495 Views
CS 24 – Problem Solving II (Data Structures). Diana Franklin. Searching for a number in a phone book that has n numbers in it. Start at the beginning while (we haven’t found it) advance to the next one Best case - 1 Average case – n/2 Worst case - n.
E N D
CS 24 – Problem Solving II (Data Structures) Diana Franklin
Searching for a number in a phone book that has n numbers in it • Start at the beginning • while (we haven’t found it) • advance to the next one • Best case - 1 • Average case – n/2 • Worst case - n
Searching for a number in a sorted phone book • Look at the middle element • if it’s smaller than that one, throw out top half • if it’s larger than that one, throw out bottom half • repeat until we have only one item left • best-case - 1 • worst-case log2(n) • average-case –summation of: time*probability of that time • 1*1/n + 2*2/n + 3*4/n + 4*8/n+…+log2(n)*n/2
n*1/2*1/2*1/2*… = 1 • n*(1/2)^j = 1 • 1/(2^j) = 1/n • n = 2^j • log2(n) = j
http://www.cs.ucsb.edu/~franklin/24/24.html • Syllabus • Assignments (with dates) • Lecture resources • Definition of plagiarism • Detailed grading breakdown • Late policy • Information guides for gdb, compiling, unix • etc, etc, etc.
Logistics • Labs – Worth .5% each. Must attend 4 to not lose credit. Others boost assignment grade. • Mini-Exams – 2 – in between to make sure I don’t lose you • Exams – 2 midterms, 1 final • Programs – First individual, the rest pair programming
Late Policy • Programs – up to 24 hrs late, 10% off. Nothing accepted after 24 hrs. • If you submit late, the entire assignment is late.
Arrays • How is an array different from a pointer? • int *x • int x[10]; • What does this declare? • int *x[10]; • int **x;
Strings • How are strings stored in C? • Array of characters • The end of the string has a ‘\0’ • How do you declare an array of strings? • char **arrayOfStrings = NULL • How do you declare an array of 7 strings? • char *arrayOfStrings[7]
Command-line arguments void main(intargc, char *argv[]) • Explain what type char *argv[] is • How do argc and argv get set? • Draw the memory of argv for the input: • ./a.out cs24 rules
Pointer Review • General knowledge • Drawing the memory • Reading and Writing
What do you know about pointersthat was covered in CS 16? • Pointers store a memory address • You can use pointer arithmetic to access different parts of an array • & is the syntax for “address of” • * is the syntax to dereference (read the contents of the item the pointer points to) • If you have a struct, you use variablename->field rather than variable.field • Pointers can point to anything (including pointers) • You can have a large degree of indirection
When MUST you use pointers? • Calling a function and you want to pass a huge structure • You want a function to modify data that is being used outside the function (not a global variable) • You don’t know how big of an array to create until the program is running
Pointers and Types • If a pointer is not pointing anywhere useful, where should it point? • NULL • If a function is receiving a pointer, but it does not know what type the pointer is, how do you declare it? • void * • To access the data in a void *, what do you need to do? • cast it to the right type • What does void mean? • A function does not return a value • Explicitly states that there are no input arguments
What do you know about pointersthat was covered in CS 16? • They store an address in a variable • You can have a pointer pointing to another pointer • You can change values in arrays more easily • There are void pointers, and you need to cast them if you want to dereference them • Pointers are used for dynamic memory allocation
Draw the memory of each int *x, *y; x = &a; y = &b; x = y; int a, b; a = 5; b = 3; a = b; What is the difference between (a = b) and (x = y) for what the computer does? What is the difference between (a = b) and (x = y) for how we view them? The way to make working with pointers easy is to operate on them mechanically like the computer. They are no different than integers.
Pointer Review – what prints out? int x = 3, y = 4; int *pX = &x, *pY = &y; (pX == pY) ? Print(“Same\n”): print(“Not Same\n”); (*pX == *pY) ? Print(“Equal\n”) : print(“NEqual\n”); x = y (pX == pY) ? Print(“Same\n”): print(“Not Same\n”); (*pX == *pY) ? Print(“Equal\n”) : print(“NEqual\n”); x = 10; pX = pY; (pX == pY) ? Print(“Same\n”): print(“Not Same\n”); (*pX == *pY) ? Print(“Equal\n”) : print(“NEqual\n”);
Draw the memory char *c[]={ "ENTER", "NEW", "POINT", "FIRST" }; printf(“%s”,c[2]); printf(“%s “, &(c[0]3]));
Structs • What do you know about structs? • You can create your own data type • Make many copies of that data type • dynamically allocate structs • Variables with multiple fields – programmer defines what the fields hold
Structs typedefstruct _product { int weight; floatprice; } product; How do I declare three variables, named apple, banana, and melon, of type product? struct _product apple, banana, melon; product apple, banana, melon; apple.weight = 5;
Structs typedefstruct _product { int weight; floatprice; } product; product apple; product banana, melon;
Structs typedefstruct _product { int weight; floatprice; } product; product apple, banana, melon; How do I set the weight field of apple? apple.weight = 5;
header files (.h) • What goes into a header file? • type definitions • function definitions • global variable declarations • A guard to only be read once • What does not go into a header file? • Local variable declarations • Function implementations
dynamicArray.h #ifndefDYNAMICARRAY_H #define DYNAMICARRAY_H typedefstructdynamicarray_h{ int size; char **array; } dynamicArray; dynamicArray*da_make_new_array(); char *da_get(dynamicArray *da,int index); void da_set(dynamicArray *da, int index, char *val); #endif
File I/O void printFile(char *filename) { char buf[256]; // step 1: open the file FILE *file = fopen(filename,”r”); // step 2: make sure it opened properly if (file == NULL) { printf(“Could not open %s\n”,filename);exit(1);} // step 3: read in each line of the file and print it out to the screen fgets(buf,256,file); while(!feof(buf)) { printf(“%s”,buf); fgets(buf,256,file); // step 4: stop when we reach the end of the file } // step 5: close the file fclose(file); }
Linking pointers Typdefstruct _link{ int value; struct _link *next; } link; link l1, l2, l3, l4; link *p1, *p2, *p3, *p4; p1 = &l1; p2 = &l2; p3 = &l3; p4 = &l4; l1.value = 1; l2.value = 2; l3.value = 4; l4.value = 8; p1->next = p2; p2->next = p3; p3->next = p4; p4->next = p1; p1-value = 5; p1->next->value = 3; p1->next->next->value = 7; for(i=0;i<4;i++) { printf(“%d, “,p2->value); p2 = p2->next; } p2 = null; p3 = null; p4 = null;
Function calls / pointersPass int by value void swap(intx, int y) int temp = x; x = y; y = temp; } int main () { int x=100, y = 50; swap(x, y); cout << ”x=" <<x << ", y=" << y; return 0; }
Function calls / pointersPass pointer (reference) by value void swap(int*x, int *y) int temp = *x; *x = *y; *y = temp; } int main () { int x=100, y = 50; swap(&x, &y); cout << ”x=" <<x << ", y=" << y; return 0; }
Function calls / pointersPass int by reference – C++ Syntax of passing an int, Actions of passing a pointer to int void swap(int& x, int& y) int temp = x; x = y; y = temp; } int main () { int x=100, y = 50; swap(x, y); cout << ”x=" <<x << ", y=" << y; return 0; }
Crazy Pointers char *c[]={ "ENTER", "NEW", "POINT", "FIRST" }; char **cp[]={&(c[3]),&(c[2]),&(c[1]),c}; char ***cpp=&(cp[1]); printf(“%s”,**cpp); printf(“%s “, &((*(cpp[2]))[3])); printf(“%c\n”,cpp[0][1][3]);