Presentation is loading. Please wait.

Presentation is loading. Please wait.

Friday, January 19, 2007 Anyone who has never made a mistake has never tried anything new. -Albert Einstein.

Similar presentations


Presentation on theme: "Friday, January 19, 2007 Anyone who has never made a mistake has never tried anything new. -Albert Einstein."— 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 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; pointers - dynamic allocation

5 §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. Calling Functions with Arrays

6 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; } Calling Functions with Arrays

7 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] << ' '; } Calling Functions with Arrays

8 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; } Calling Functions with Arrays

9 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] << ' '; } Calling Functions with Arrays

10 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] << ' '; } Calling Functions with Arrays

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

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

13 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; } SELF TEST: Calling Functions with Arrays

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

15 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); } SELF TEST: Functions

16 2-Dimensional Arrays §2-Dimentional Arrays as arguments to functions l 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); return 0; }

18 1 2 3 4 1 2 3 100

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

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 What is wrong with following? char str1[80]; str1="cs192"; Strings

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

23 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] Arrays of Pointers Make a memory drawing!

24 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?? Initializing Arrays of Pointers

25 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; } Self Test: Arrays of Pointers

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

27 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; } Calling Functions with Arrays

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 34563456

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


Download ppt "Friday, January 19, 2007 Anyone who has never made a mistake has never tried anything new. -Albert Einstein."

Similar presentations


Ads by Google