Thursday, January 18, 2007 The question of whether computers can think is just like the question of...
-
date post
15-Jan-2016 -
Category
Documents
-
view
218 -
download
0
Transcript of Thursday, January 18, 2007 The question of whether computers can think is just like the question of...
![Page 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/1.jpg)
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)
![Page 2: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/2.jpg)
Self Test : String tokenizing: Skip multiple spaces between words.
![Page 3: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/3.jpg)
Function to swap two numbers.
![Page 4: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/4.jpg)
void swap(int* sa, int *sb); //prototypeint 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; }
![Page 5: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/5.jpg)
There are three ways in C++ to pass arguments to a function:
Call by valueCall by reference with pointer
argumentsCall by reference with reference
arguments
Calling Functions
![Page 6: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/6.jpg)
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
![Page 7: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/7.jpg)
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
![Page 8: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/8.jpg)
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
![Page 9: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/9.jpg)
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
![Page 10: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/10.jpg)
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?
![Page 11: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/11.jpg)
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
![Page 12: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/12.jpg)
The new operator
int *x_ptr;x_ptr=new int;
OR
int *x_ptr=new int;
//heap
Dynamic allocation
![Page 13: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/13.jpg)
int *x_ptr=new int;*x_ptr=73;
int *x2_ptr=new int;*x2_ptr=65;
Dynamic allocation
![Page 14: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/14.jpg)
What is wrong here?
int *x_ptr=new int;*x_ptr=73;
int *x2_ptr;*x2_ptr=65;
Dynamic allocation
![Page 15: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/15.jpg)
int *x_ptr=new int;*x_ptr=73;
int *x2_ptr;x2_ptr=x_ptr;*x2_ptr=65;
Dynamic allocation
![Page 16: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/16.jpg)
movie…
Dynamic allocation
![Page 17: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/17.jpg)
//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
![Page 18: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/18.jpg)
//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
![Page 19: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/19.jpg)
int *my_ptr=new int;*my_ptr=76;cout<<*my_ptr;
delete my_ptr;
Dynamic allocation
![Page 20: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/20.jpg)
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
![Page 21: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/21.jpg)
Another example…
Dynamic allocation
![Page 22: 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.](https://reader035.fdocuments.in/reader035/viewer/2022062409/56649d585503460f94a3862a/html5/thumbnails/22.jpg)
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