Top Banner
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)
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.

Jan 15, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

//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.

//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.

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.

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.

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.

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