1 / 22

Thursday, January 18, 2007

Thursday, January 18, 2007. The question of whether computers can think is just like the question of whether submarines can swim.  -Edsger W. Dijkstra (1930 – 2002). Self Test : String tokenizing: Skip multiple spaces between words. Function to swap two numbers.

rupert
Download Presentation

Thursday, January 18, 2007

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Thursday, January 18, 2007 The question of whether computers can think is just like the question of whether submarines can swim.  -Edsger W. Dijkstra (1930 – 2002)

  2. Self Test : String tokenizing: • Skip multiple spaces between words.

  3. Function to swap two numbers.

  4. void swap(int* sa, int *sb);//prototype int main() { int a=3; int b=5; cout<<a<<" "<<b<<endl; swap(&a, &b); cout<<a<<" "<<b<<endl; return 0; } void swap(int* sa, int *sb){ int temp=*sa; *sa=*sb; *sb=temp; }

  5. Calling Functions There are three ways in C++ to pass arguments to a function: • Call by value • Call by reference with pointer arguments • Call by reference with reference arguments

  6. Call Functions with pointers void f(int *j); int main() { int i=10; int *p; p = &i; // p now points to i f(p); cout << i; // i is now ? return 0; } void f(int *j) { *j = 100; //pointee of j is assigned 100 }

  7. Call Functions with pointers void f(int *j); int main() { int i=10; f(&i); cout << i; return 0; } void f(int *j) { *j = 100; //pointee of j is assigned 100 }

  8. Functions int cubeByValue(int n); // prototype main() { int number = 5; cout <<"The original value of number is " << number << endl; number = cubeByValue(number); cout << "The new value of number is " << number << endl; return 0; } int cubeByValue(int n) { return n * n * n; //cube local variable n }

  9. Calling Functions with Pointers void cubeByReference(int *nPtr); // prototype main() { int number = 5; cout << "The original value of number is " << number << endl; cubeByReference(&number); cout << "The new value of number is " << number << endl; return 0; } void cubeByReference(int *nPtr) { // cube number in main *nPtr = *nPtr * *nPtr * *nPtr; }

  10. WHAT IS WRONG WITH THE FOLLOWING? void swap(int *pa, int *pb);//prototype int main() { int a=3; int b=5; cout<<a<<" "<<b<<endl; swap(&a, &b); cout<<a<<" "<<b<<endl; return 0; } void swap(int *pa, int *pb){ int *temp; temp=pa; pa=pb; pb=temp; }

  11. Calling Functions with Pointers void convert(char *sPtr); main() { char string[] = "characters and $32.98"; cout << "The string before conversion is: " << string << endl; convert(string); cout << "The string after conversion is: " << string << endl; return 0; } void convert(char *sPtr) { while (*sPtr != '\0') { if (*sPtr >= 'a' && *sPtr <= 'z') *sPtr = toupper(*sPtr); ++sPtr; // increment sPtr to point to the next character } }

  12. Dynamic allocation The new operator int *x_ptr; x_ptr=new int; OR int *x_ptr=new int; //heap

  13. Dynamic allocation int *x_ptr=new int; *x_ptr=73; int *x2_ptr=new int; *x2_ptr=65;

  14. Dynamic allocation What is wrong here? int *x_ptr=new int; *x_ptr=73; int *x2_ptr; *x2_ptr=65;

  15. Dynamic allocation int *x_ptr=new int; *x_ptr=73; int *x2_ptr; x2_ptr=x_ptr; *x2_ptr=65;

  16. Dynamic allocation movie…

  17. Dynamic allocation //What is wrong here? int *x_ptr=new int; *x_ptr=73; int *x2_ptr=new int; x2_ptr=x_ptr; *x2_ptr=65;

  18. Dynamic allocation //What is wrong here? int *x_ptr=new int; *x_ptr=73; int *x2_ptr=new int; x2_ptr=x_ptr; *x2_ptr=65; //memory leak

  19. Dynamic allocation int *my_ptr=new int; *my_ptr=76; cout<<*my_ptr; delete my_ptr;

  20. Dynamic allocation int *my_ptr=new int; *my_ptr=76; cout<<*my_ptr; delete my_ptr; int *my_ptr=new int(76); cout<<*my_ptr; delete my_ptr;

  21. Dynamic allocation Another example…

  22. Dynamic allocation double *p=new double[10];/*10 element array*/ int i, size=10; for (i=0; i<size; i++){ p[i]=2.0*i; } for (i=0; i<size; i++){ cout<<p[i]<<endl; } delete []p;

More Related