lecture10slides

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

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

View Full Document Right Arrow Icon
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; }
Background image of page 1

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

View Full DocumentRight Arrow Icon
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"); }
Background image of page 2
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); }
Background image of page 3

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

View Full DocumentRight Arrow Icon
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; }
Background image of page 4
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; } }
Background image of page 5

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

View Full DocumentRight Arrow Icon
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; }
Background image of page 6
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];
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
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 Right Arrow Icon
Ask a homework question - tutors are online