300 likes | 462 Views
Recitation Class IX. Content. Style Pointer. Style. Some examples in Yahtzee. Example 1. Example 1. Example 1. Example 1. Example 2. Example 2. Example 2. Example 2. Pointer. Declare a pointer 1) int *pointer_1; char *pointer_2; 2) int i; int *pointer_1 = &i;
E N D
Content • Style • Pointer
Style • Some examples in Yahtzee
Pointer • Declare a pointer 1) int *pointer_1; char *pointer_2; 2) int i; int *pointer_1 = &i; 3) int *pointer_1=2000;
Example Use Pointer to get the maximum and minimum of two integers.
Example: What’ s the result? #include <iostream> using namespace std; int main() { int *p1, *p2, *p, a, b; cin >> a >> b; p1 = &a; p2 = &b; if (a < b) {p=p1; p1=p2; p2=p;} cout << "max=" << *p1 << endl; cout << "min=" << *p2 << endl; return 0; } 1 3 max = 3 min = 1
Example: What’ s the result? 1 3 max = 3 min = 1 #include <iostream> using namespace std; int main() { void swap(int *p1,int *p2); int *pointer_1,*pointer_2,a,b; cin>>a>>b; pointer_1=&a; pointer_2=&b; if(a<b) swap(pointer_1,pointer_2); cout<<"max="<<a<<endl; cout<<"min="<<b<<endl; return 0; } void swap(int *p1,int *p2) { int temp; temp=*p1; *p1=*p2; *p2=temp; }
Example: What’ s the result? #include <iostream> using namespace std; int main() { void swap(int *p1,int *p2); int *pointer_1,*pointer_2,a,b; cin>>a>>b; pointer_1=&a; pointer_2=&b; if(a<b) swap(pointer_1,pointer_2); cout<<"max="<<a<<endl; cout<<"min="<<b<<endl; return 0; } 1 3 void swap(int *p1,int *p2) { int *temp; *temp=*p1; *p1=*p2; *p2=*temp; }
Example: What’ s the result? 1 3 max = 1 min = 3 #include <iostream> using namespace std; int main() { void swap(int *p1,int *p2); int *pointer_1,*pointer_2,a,b; cin>>a>>b; pointer_1=&a; pointer_2=&b; if(a<b) swap(pointer_1,pointer_2); cout<<"max="<<a<<endl; cout<<"min="<<b<<endl; return 0; } void swap(int *p1,int *p2) { int* temp; temp=p1; p1=p2; p2=temp; }
Example: What’ s the result? 1 3 max = 3 min = 1 #include <iostream> using namespace std; int main() { void swap(int &a,int &b); int a,b; cin>>a>>b; if(a<b) swap(a,b); cout<<"max="<<a<<endl; cout<<"min="<<b<<endl; return 0; } void swap(int &a,int &b) { int temp=a; a=b; b=temp; }
Pointer • Array and pointer int a[10]; // Declare an array int *p=&a[0]; // int *p=a;
Example: What’ s the result? #include <iostream> using namespace std; int main() { char str[7]="Samson"; char* p=str; for (p=str; p<(str+3); p++) cout<<*p; } Sam
Example: What’ s the result? #include <iostream> using namespace std; void convert(char *p, int n); int main() { char str[7]="Samson"; convert(str,6); for (int i=0;i<6;i++) cout<<str[i]; } void convert(char *p, int n) { for (int i=0;i<n;i++) (*(p+i))++; } Tbntpo
Pointer • Function and pointer I Return a pointer II Pointer to a function
Example: What’ s the result? #include <iostream> using namespace std; char *convert(char str[], int n); int main() { char str[7]="Samson"; char* s=convert(str,6); for (int i=0;i<6;i++) cout<<s[i]; } char *convert(char str[], int n) { for (int i=0;i<n;i++) str[i]++; return str; } Tbntpo
Example: What’ s the result? #include <iostream> using namespace std; char *convert(char str[], int n); int main() { char str[7]="Samson"; char* s=convert(str,6); for (int i=0;i<6;i++) cout<<s[i]; } char *convert(char str[], int n) { char *p=str; for (int i=0;i<n;i++) (*(p+i))++; return p; } Tbntpo
Example: What’ s the result? #include <iostream> using namespace std; int main() { void output(char str[]); void (*p)(char[]); p=output; char str[7]="Samson"; p(str); } void output(char str[]) { cout<<str; } Samson
Pointer • Dynamic Allocation int *p=new int[10]; int *p=new int[];
Example: Sort #include <iostream> using namespace std; void convert(char *p, int n); int main() { int *p = new int [1]; int a,temp,i=0; while (cin>>a && a!=0) { p[i]=a; i++; } for (int j=0;j<i-1;j++) for (int k=0;k<i-1-j;k++) if (p[k]>p[k+1]) { temp=p[k]; p[k]=p[k+1]; p[k+1]=temp; } for (int k=0;k<i;k++) cout<<p[k]<<" "; delete [] p; // important } -1 2 3 4 -6 9 2 3 5 -2 0 -6 -2 -1 2 2 3 3 4 5 9
Pointer • Pointer to a Pointer
Example: What’ s the result? #include <iostream> using namespace std; void convert(char *p, int n); int main() { char **p; char *subject[]={"Maths","Chemistry","C++","Physics"}; p=subject+2; cout<<*p<<endl; cout<<**p<<endl; } C++ C
The end. Thank you!