1 / 38

Friday, January 19, 2007

Friday, January 19, 2007. Anyone who has never made a mistake has never tried anything new. -Albert Einstein. new delete Static array: Size must be known at compile time int size; cin>>size; int array[size]; //Not allowed. new delete Static array: Size must be known at compile time

Download Presentation

Friday, January 19, 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. Friday, January 19, 2007 Anyone who has never made a mistake has never tried anything new. -Albert Einstein

  2. new • delete Static array: Size must be known at compile time int size; cin>>size; int array[size]; //Not allowed

  3. new • delete Static array: Size must be known at compile time int size; cin>>size; int array[size]; //Not allowed Dynamically allocated array: i.e On the fly, at runtime int size; cin>>size; int* array=new int[size]; //OK

  4. pointers - dynamic allocation int size; cin>>size; int* array=new int[size]; int i; for (i=0; i<size; i++){ array[i]=2*i; } for (i=0; i<size; i++){ cout<<array[i]<<endl; } delete []array;

  5. Calling Functions with Arrays • When an array is an argument to a function, only address of the first element of the array is passed,not a copy of the entire array. • Array name without an index is a pointer to first element in the array.

  6. Calling Functions with Arrays void display(int num[]); void fill_in(int num[]); /* C++ will automatically convert it to an integer pointer */ int main() { int t[10],i; fill_in(t); display(t);//pass array t to a function return 0; }

  7. Calling Functions with Arrays void fill_in(int num[]) { int i; for(i=0; i<10; i++) num[i]=i; } void display(int num[]) { int i; for(i=0; i<10; i++) cout << num[i] << ' '; }

  8. Calling Functions with Arrays void display(int num[10]); void fill_in(int num[10]); /* C++ will automatically convert it to an integer pointer */ int main() { int t[10],i; fill_in(t); display(t);//pass array t to a function return 0; }

  9. Calling Functions with Arrays void fill_in(int num[10]) { int i; for(i=0; i<10; i++) num[i]=i; } void display(int num[10]) { int i; for(i=0; i<10; i++) cout << num[i] << ' '; }

  10. Calling Functions with Arrays void display(int *num) /* This method is most commonly used in professionally written C++ programs when dealing with single dimensional numeric arrays*/ { int i; for(i=0; i<10; i++) cout << num[i] << ' '; }

  11. Calling Functions with Arrays void display(int *num); int main() { int t[10], i; fill_in(t); display(t); int *pt=t; display(pt); cout<<*pt; return 0; }

  12. Calling Functions with Arrays void display(int *num) { int i; for(i=0; i<10; i++) cout << *(num++) << ' '; cout<<endl; }

  13. SELF TEST: Calling Functions with Arrays void cube(int *n, int num); int main() { int i, nums[10]; for(i=0; i<10; i++) nums[i] = i+1; cout << "Original contents: "; for(i=0; i<10; i++) cout << nums[i] << ' '; cout << '\n'; cube(nums, 10); // compute cubes cout << "Altered contents: "; for(i=0; i<10; i++) cout << nums[i] << ' '; return 0; }

  14. SELF TEST: Calling Functions with Arrays void cube(int *n, int num) { while(num) { *n = (*n) * (*n) * (*n); num--; n++; } }

  15. SELF TEST: Functions void sqr_it(int *i); // prototype int main() { int x; x = 10; sqr_it(x); return 0; }//ERROR? void sqr_it(int *i) { *i = (*i) * (*i); }

  16. 2-Dimensional Arrays • 2-Dimentional Arrays as arguments to functions • column size must be declared in prototype

  17. void print2D(int twoD[][2]){ int i,j; for(i=0; i<2; i++){ for(j=0; j<2; j++){ cout<<twoD[i][j]<<endl; } } twoD[1][1]=100; } int main(){ int twoD[2][2]={1,2, 3,4}; print2D(twoD); print2D(twoD); return 0; }

  18. 1 2 3 4 1 2 3 100

  19. Initialization of strings char str1[80]="hello"; char str2[80]={"hello"}; char str3[80]={'h','e','l','l','o','\0'};

  20. When the compiler encounters a string constant, it stores it in the program string table and generates a pointer to the string. char *s; s = "Pointers are fun to use.\n"; cout << s;

  21. Strings What is wrong with following? char str1[80]; str1="cs192";

  22. Strings What is wrong with following? char str1[80]; str1="cs192"; //Error Use strcpy(str1, “cs192”); We can also initialize at the time of declaration.

  23. Arrays of Pointers int *pi[4]; int var=10; pi[2] = &var; /* assign address of an integer var to third element of pointer array */ To find value of var: *pi[2] Make a memory drawing!

  24. Initializing Arrays of Pointers int main( ){ char *fortunes[5] = { "Soon, you will come into some money.\n", “You will see an old friend today.\n", "You will live long and prosper.\n", "Now is a good time to invest for the future.\n", "A close friend will ask for a favor.\n" }; cout<<fortunes[2];//what is the output??

  25. Self Test: Arrays of Pointers int chance, input, i; cout << "To see your fortune, enter a random integer: "; cin>>input; // randomize the random number generator for (i=0; i<input; i++) rand(); chance = rand(); chance = chance % 5; cout << fortunes[chance]; return 0; }

  26. Calling Functions with Arrays int main(void){ char* entries[]={ {"ambidextrous"}, {"fata morgana"}, {"infrangible"}, {"serene"}, {"tousle"} }; printentries(entries); return 0; }

  27. Calling Functions with Arrays void printentries (char* ent[]){ int i; for(i=0; i<5; i++){ cout<<ent[i]<<endl; } } int main(void){ char* entries[]={ {"ambidextrous"}, {"fata morgana"}, {"infrangible"}, {"serene"}, {"tousle"} }; printentries(entries); return 0; }

  28. const int rows=13, cols=4; int i; char *Harvest[rows][cols]= { {"unknown month", " ", " ", " "}, {"sugar beet","cabbage","cauliflower", "Brussels sprouts"}, {"turnip", "radish", "caraway", "carrot"}, {"apple", "plum", "pear", "bean"}, {"peas", "clover", "lettuce", "onion"}, {"oat", "asparagus", "barley", "rye"}, {"maize", "wheat", "rice", "sugar cane"}, {"cotton", "cocoa", "tea", "grape"}, {"raspberry","hazelnut", "garlic", "ginger"}, {"potato", "sesame", "rye", "green gram"}, {"olive", "peppermint", "millet", "parsely"}, {"root mustard", "tomato", "jojoba", "fig"}, {"pecan", "thyme", "lime", "parsnip"} };

  29. cout<<Harvest[0]<<endl; cout<<Harvest[0][0]<<endl; cout<<Harvest[0][0][0]<<endl;

  30. cout<<Harvest[0]<<endl; cout<<Harvest[0][0]<<endl; cout<<Harvest[0][0][0]<<endl; 0x0012FDE4 unknown month u

  31. for (i=0; i<cols; i++){ cout<<Harvest[2][i]<<"\t"; } cout<<endl; for (i=0; i<cols; i++){ cout<<Harvest[7][i]<<"\t"; } cout<<endl;

  32. turnip radish caraway carrot cotton cocoa tea grape

  33. int dept1=50, dept2=60, dept3=100, dept4=30; int *production[2][2]= {{&dept1, &dept2}, {&dept3, &dept4} }; for (i=0; i<2; i++){ cout<<production[1][i]<<"\t"; } cout<<endl; for (i=0; i<2; i++){ cout<<*production[1][i]<<"\t"; }

  34. 0x0012FEB0 0x0012FEA4 100 30

  35. void ptrprint2D(int *ptr2D[][2]){ int i,j; for(i=0; i<2; i++){ for(j=0; j<2; j++){ cout<<*ptr2D[i][j]<<endl; } }} int main(){ int a1=3, a2=4, a3=5, a4=6; int* ptrtwoD[2][2]={&a1, &a2, &a3, &a4}; ptrprint2D(ptrtwoD); return 0;}

  36. 3 4 5 6

  37. char* myFunction(char *strA, char *strB){ while (*strB){ *strA=*strB; strA++; strB++; } return strA; } int main(void) { char stringa[]={"Rather Long String“}; char stringb[]={"bit short“}; char *pa=stringa; char *pb=stringb; char *pret; pret=myFunction(pa, pb); cout<<pa<<endl<<pb<<endl<<pret<<endl; return 0;} Functions that return pointers

  38. bit shortng String bit short ng String

More Related