tuesday, january 23, 2007 "we can't solve problems by using the same kind of thinking we...

25
Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

Post on 19-Dec-2015

216 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

Tuesday, January 23, 2007

"We can't solve problems by using the same kind of thinking we used when we created them."

-Albert Einstein

Page 2: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

Handout 2Assignment 4

Page 3: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

int* function_returns_ptr(int *a){

return (a+4);

}

int main(void){

int array[8]={1,2,3,4,5,6,7,8};

int *ptrArray=array;

int *result=function_returns_ptr(ptrArray);

cout<<*result<<endl;

return 0;

}

Page 4: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

int* function_returns_ptr(int *a){

return (a+4);

}

int main(void){

int array[8]={1,2,3,4,5,6,7,8};

int *ptrArray=array;

int *result=function_returns_ptr(ptrArray);

cout<<*result<<endl; //prints 5

return 0;

}

Page 5: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

What is wrong here?

int* function_returns_ptr(void){

int array[8]={1,2,3,4,5,6,7,8};

int *ptrArray=array;

return (ptrArray+4);

}

int main(void){

int *result=function_returns_ptr();

cout<<*result<<endl;

return 0;

}

Page 6: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

What is wrong here?

int* function_returns_ptr(void){

int array[8]={1,2,3,4,5,6,7,8};

int *ptrArray=array;

return (ptrArray+4); //dangerous

}

int main(void){

int *result=function_returns_ptr();

cout<<*result<<endl;

return 0;

}

Page 7: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

What is wrong here?

int* f2_returns_ptr(int *a, int *b){

int c=*a+*b;

int d=*a-*b;

if (c>d)

a=&c;

else

a=&d;

return a;

}

Page 8: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

What is wrong here?

int* f2_returns_ptr(int *a, int*b){

int c=*a+*b;

int d=*a-*b;

if (c>d)

a=&c;

else

a=&d;

return a; //very serious mistake

}

Page 9: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

char *get_substr(char *sub, char *str);

int main() {

char *substr;

substr = get_substr("two", "one two three four");

if(substr)

cout << "substring found: " << substr<<endl;

else

cout << "substring not found"<<endl;

return 0;

}

Functions that return pointers

Page 10: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

char *get_substr(char *sub, char *str) {

int t;

char *p, *p2, *start;

for(t=0; str[t]; t++) {

p = &str[t]; // reset pointers

start = p;

p2 = sub;

while(*p2 && *p2==*p) { // check for substring

p++;

p2++;

} /* If at end of p2 (i.e., substring), then a match has been found. */

if(!*p2)

return start; // return pointer to beginning of substring

}

return '\0'; }// no match found

Page 11: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

substring found: two three four

Page 12: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

int strcmp ( const char *s1, const char *s2 );

strcmp will accept two strings. It will return an integer.

This integer will either be:

Negative if s1 is less than s2.Zero if s1 and s2 are equal. Positive if s1 is greater than s2.

Strings

Page 13: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

char *strcat ( char *dest, const char *src ); strcat is short for string concatenate, which means to add to the end, or append. It adds the second string to the first string.

It returns a pointer to the concatenated string.

Beware this function, it assumes that dest is large enough to hold the entire contents of src as well as its own contents.

Strings

Page 14: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

char *strcpy ( char *dest, const char *src );

strcpy is short for string copy, which means it copies the entire contents of src into dest.

It returns a pointer to the copied string.

The contents of dest after strcpy will be exactly the same as src such that strcmp ( dest, src ) will return 0.

Strings

Page 15: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

size_t strlen ( const char *s );

strlen will return the length of a string, minus the terminating character ('\0').

Treat size_t as an integer that cannot be negative.

Strings

Page 16: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

char str1[80]={"one"}, str2[80]={"twenty"};

char *str3=strcpy(str1,str2);

cout<<str1<<endl;

cout<<str2<<endl;

cout<<str3<<endl;

Page 17: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

A function can be declared to return any valid data type, except that a function cannot return arrays.

Page 18: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

Example - PalindromesA palindrome is a word (noon) , sentence (Draw a level award) or number (18781) that reads the same backward or forward. Write a function that takes a string as input and determines if the string is a palindrome. (Note: white space is ignored when determining if a word, sentence or number is a palindrome.)

Page 19: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

Self Test: do it with pointers (account for white spaces also)int is_palindrome(char string[]) { int is_pal, begin=0, end; end = strlen(string) -1; is_pal = 1; while(is_pal && begin <=end){ if(string[begin] != string[end])

{ is_pal = 0; } begin++; end--; }

return(is_pal); }

Page 20: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

References

Safer and simpler use of memory addresses are references

When a reference parameter is used, the address of an argument is automatically passed to the function.

Within the function the operations on reference parameter are automatically de-referenced.

Page 21: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

With call by reference, the caller gives the called function the ability to directly access the caller’s data and to modify that data if the called function so chooses.

To indicate that a function parameter is passed by reference, simply follow the parameter’s type in the function prototype by an ampersand &.

e.g. void myFunction( int &x);

Calling Functions

Page 22: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

int squareByValue(int a);

void squareByReference(int &aref);

Calling Functions

Page 23: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

int main() {

int x = 2, z = 4;

cout << "x = " << x << " before squareByValue" ;

squareByValue(x) ;

cout<< "x = " << x << " after squareByValue" ;

cout << "z = " << z << "before squareByReference”;

squareByReference(z);

cout << "z = " << z << " after squareByReference";

return 0;

}

Calling Functions

Page 24: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

int squareByValue(int a)

{

return a *= a; //caller's argument not modified

}

void squareByReference(int &cRef)

{

cRef *= cRef; // caller's argument modified

}

Calling Functions

Page 25: Tuesday, January 23, 2007 "We can't solve problems by using the same kind of thinking we used when we created them." -Albert Einstein

Function to swap two numbers