Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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: l 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 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 Calling Functions

6 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 } Call Functions with pointers

7 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 } Call Functions with pointers

8 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 } Functions

9 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; } Calling Functions with Pointers

10 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; } WHAT IS WRONG WITH THE FOLLOWING?

11 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 } Calling Functions with Pointers

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

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

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

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

16 movie… Dynamic allocation

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

18 //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 Dynamic allocation

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

20 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; Dynamic allocation

21 Another example… Dynamic allocation

22 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; Dynamic allocation


Download ppt "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."

Similar presentations


Ads by Google