lecture10slides

# lecture10slides - Wednesday, November 7th Todays Topics 1....

This preview shows pages 1–8. Sign up to view the full content.

Wednesday, November 7 th Today’s Topics 1. A little recursion review 2. More pointers 3. How to use files Another Recursion Example string reverse(string s) { string temp; if (s.length() == 1) return(s); temp = reverse(s.substr(1)) + s[0]; return(temp); } void main(void) { string a = reverse("wxyz"); cout << a; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Mergesort: One More Time! This sort takes a string and sorts its characters and returns the new string. string sort(string x) { int n = x.length(); if (n == 1) return(x); n /= 2; string first = x.substr(0,n); string second = x.substr(n); first = sort(first); second = sort(second); string done = merge(first,second); return(done); } main() { cout << sort(“BA”); cout << sort("CBDA"); }
Review: More pointers Question: What is printed out? Assume fv is at address 1000 and pfv at 1008. main() { float fv = 3.14159; float *pfv; pfv = & fv; cout << pfv; cout << &pfv; cout << sizeof(pfv); }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Review of The * Operator! What is printed? Assume i1 is at address 1000, i2 at 1004, and pi at 1008 main() { int i1 = 100, i2; int *pi; piv = & i1; cout << i1; cout << *pi; *pi = 10; cout << i1; i2 = *pi; cout << i2; }
More pointer examples: What is printed? Assume a is at address 2000, and the other variables follow it in order. main() { int a=1, b=2; int *p1, *p2; // how to define 2 ptrs p1 = &a; p2 = &b; // whats the diff between *p1 = *p2; // and p1 = p2; // how about: if (*p1 == *p2) { do something; } // and if (p1 == p2) { do something; } }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Using Pointers Instead of References void set(int *pa) // like a reference! { *pa = 5; } main() { int x = 1; set(&x); cout << x; } Quiz: main() { char ch = 'A'; char *pch; char *pch2; pch = &ch; pch2 = pch; (*pch2)++; cout << ch; }
Pointers and Arrays main() { int arr[4] = {-1,-2,-3,-4}; int *parray; cout << arr[0] << endl; cout << &arr[0] cout << arr << endl; parray = &arr[0];

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/08/2011 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

### Page1 / 23

lecture10slides - Wednesday, November 7th Todays Topics 1....

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online