1 / 30

Recitation Class IX

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;

saddam
Download Presentation

Recitation Class IX

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. Recitation Class IX

  2. Content • Style • Pointer

  3. Style • Some examples in Yahtzee

  4. Example 1

  5. Example 1

  6. Example 1

  7. Example 1

  8. Example 2

  9. Example 2

  10. Example 2

  11. Example 2

  12. Pointer • Declare a pointer 1) int *pointer_1; char *pointer_2; 2) int i; int *pointer_1 = &i; 3) int *pointer_1=2000;

  13. Example Use Pointer to get the maximum and minimum of two integers.

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

  15. 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; }

  16. 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; }

  17. 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; }

  18. 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; }

  19. Pointer • Array and pointer int a[10]; // Declare an array int *p=&a[0]; // int *p=a;

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

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

  22. Pointer • Function and pointer I Return a pointer II Pointer to a function

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

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

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

  26. Pointer • Dynamic Allocation int *p=new int[10]; int *p=new int[];

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

  28. Pointer • Pointer to a Pointer

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

  30. The end. Thank you!

More Related