1 / 31

Homogeneous aggregate

Homogeneous aggregate. name: Tom, John, student-3, student_4,........, student-20. mid1: 70, 67, 86, 59, ........, 80. final: 69, 77, 79, 64, ........, 90.

gforet
Download Presentation

Homogeneous aggregate

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. Homogeneous aggregate name: Tom, John, student-3, student_4,........, student-20 mid1: 70, 67, 86, 59, ........, 80 final: 69, 77, 79, 64, ........, 90 GPA: 3.02, 2.89, 3.21, 2.78, ........, 3.67 <tvector> in STL (Standard Template Library) // Using array string name[20]; int mid1[20]; int final[20]; double GPA[20]; name[0] = "Tom"; GPA[2] = 3.21; // Using tvector class #include "tvector.h" .............. tvector<string> name(20); tvector<int> mid1(20); tvector<int> final(20); tvector<double> GPA(20); name[1] = "John"; GPA[19] = 3.67; IT 279

  2. Search in a tvector (or an array) // Using tvector class #include "tvector.h" .............. tvector<string> name(20); tvector<int> mid1(20); tvector<int> final(20); tvector<double> GPA(20); ..... ..... // What is Susan's GAP? for (int i=0; i < name.length(); i++) { if (name[i] == "Susan") cout << GPA[i]; } Sequentially IT 279

  3. What can be in an Array // Using tvector class ..... ..... struct student { string name; int mid1; int final; double GPA; }; ...... struct student class279[20]; ..... // What is Susan's GAP? for (int i=0; i < class279.length(); i++) { if ( (class279[i]).name == "Susan") cout << (class279[i]).GPA; } A bit old fashioned IT 279

  4. The index can be an Enumerated Type Sunday Monday Tuesday Wednesday Thursday Friday Saturday Monday Tuesday Wednesday Thursday Friday Saturday Sunday enum day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; tvector<int> MyclassHr(7); MyclassHr[Monday] = 2; MyclassHr[Tuesday] = MyclassHr[Thursday] = 0; MyclassHr[Wednesday] = 4; enum day ThreeDays[3]; enum day ADay; ThreeDay[0]=Saturday; ...... if (ADay == Saturday || ADay == Sunday) cout << "It's weekend"; day FirstDay = day(0); IT 279

  5. Two dimensional array, Matrix + = // Using array int A[3][3]; int B[3][3]; int C[3][3]; A[1][2] = 3; A[1][1] = 1; C[2][1] = A[2][1]+B[2][1]; // Using apmatrix class #include "apmatrix.h" .............. apmatrix<int> A(3,3); apmatrix<int> B(3,3); apmatrix<int> C(3,3); A[1][2] = 3; A[1][1] = 1; C[2][1] = A[2][1]+B[2][1]; IT 279

  6. <vector> in STL (Standard Template Library) #include <iostream> #include <vector> #include <string> .............. struct student { string name; int ssn; }; ............... student s; vector<student> IT279; while (true) { cout << "Input SSN:"; cin >> s.ssn; if (s.ssn == 0) break; cout << "Input name:"; cin >> s.name; IT279.push_back(s); } for (int i=0;i< IT279.size();i++) { cout << "\nName:" << IT279[i].name << " "; cout << "SSN:" << IT279[i].ssn; } IT279 IT279[0] IT279[1] IT279[2] IT279[3] IT 279

  7. Resize a vector CS CS[0] CS[1] CS[2] the last push back here CS[3] CS[4] CS.resize(6) CS[5] IT 279

  8. Reserve a vector CS CS[0] CS[1] CS[2] the last push back here CS[3] CS[4] CS.resize(6) CS[5] CS.reserve(8) IT 279

  9. pop back a vector CS2 CS2[0] size CS2[1] .... CS2.resize(6); CS2.pop_back(); CS2.pop_back(); CS2.pop_back(); CS2[2] reserved CS2[3] CS2[4] CS2[5] IT 279

  10. Stack and Queue Stack: LCFS (Last Come First Service) c5 c4 Queue: FCFS (First Come First Service) c5 c1 Back Front IT 279

  11. <deque> in STL (Double Ended Queue) #include <iostream> #include <deque> #include <string> .............. deque<student> CS2; .............. while (true) { cout << "Input SSN:"; cin >> s.ssn; if (s.ssn == 0) break; cout << "Input name:"; cin >> s.name; CS2.push_front(s); CS2.push_back(s); } for (int i=0;i<CS2.size();i++) { cout << "\nName:" << CS2[i].name << " "; cout << "SSN:" << CS2[i].ssn; } CS2 CS2[0] CS2[1] CS2[2] CS2[3] IT 279

  12. pop back and pop front a deque CS2 CS2[0] CS2[0] CS2 CS2[1] CS2[0] CS2[2] .... CS2.pop_front(); CS2.pop_back(); CS2[1] CS2[3] CS2[2] CS2[4] CS2[4] IT 279

  13. A Pointer: an address, a reference, a location of the computer memory A pointer of what? int, char, bool, double, or any kind of data type Can be need to know so the computer knows how to read or store the contents of the memory. IT 279

  14. Define pointers // define pointers int a, *ap; // can’t use a and *a at the same time; double b, *bp; token k, *kp; a is an integer, ap is a pointer to an integer; b is a double, bp is a pointer to a double; k is a token, kps a pointer to a token. IT 279

  15. A Pointer points to a location of the computer memory } 4 bytes FFFFA080 FFFFA084 a ap b bp k kp 5 int a=5; int *ap; double b=2.3; double *bp; token k; token *kp; bp = new double; *bp = 3.14; *ap = 7; // not new-ed yet ? 2.3 1 3.14 1 ? IT 279

  16. A Pointer points to a location of the computer memory } 4 bytes FFFFA080 FFFFA084 a ap b c 5.1 double a=5.1; double *ap; double *b,*c; ap = &a; // &a :the address of a *ap=1.7; b = new double; *b = 3.14; b = &a; c = b; 1.7 3 1 4 1 new 2 2 3.14 3 4 IT 279

  17. The name of an array is a pointer } 4 bytes a p int a[4]={5,60,700,8000}; int *p; p=a; cout << *p << endl; cout << *p+1 << endl; cout << *(p+1) << endl; cout << *(p+2) << endl; cout << *(p+3) << endl; cout << *(p+4) << endl; 1 1 2 2 FFFFA080 FFFFA084 5 6 60 700 8000 #$&@ IT 279

  18. Dynamic arrays, resizable arrays a p a int a[3]={5,60,700}; int *p,a=4; 1 p = newint[3]; for (int i=0; i<3; i++) p[i] = i; 1 2 p = newint[a]; for (int i=0; i<4; i++) p[i] = i*i; 2 IT 279

  19. Create and Delete Dynamic arrays a p int a[3]={5,60,700}; int *p,a=4; p = newint[3]; for (int i=0; i<3; i++) p[i] = i; delete [] p; p = newint[4]; for (int i=0; i<4; i++) p[i] = i*i; 1 a 4 3 1 3 IT 279

  20. Forget to Delete Dynamic arrays a p int a[3]={5,60,700}; int *p,i; p = newint[3]; for (i=0; i<3; i++) p[i] = i; // delete [] p; p = newint[4]; for (i=0; i<4; i++) p[i] = i*i; 1 i 4 1 3 3 IT 279

  21. An array  pointers a p int a[4]={0,1,4,9} int *p; p = new int[3]; for (i=0; i<=3; i++) { p[i] = i; *(p+i) *= 2; } // a[4] = 16; *a = a[0]*a[0]; *(a+1) = a[1]*a[1]; *(a+2) = a[2]*a[2]; *(a+3) = a[3]*a[3]; //*(a+4) = a[4]*a[4]; X X IT 279

  22. Arrays Pointers int *a[4]; int **p; a[1] = new int[2]; a[2] = new int[3]; a[3] = new int[3]; p = new int*[3]; p[2] = new int[2]; p[2][0]=5; p[2][1]=7; 1 1 a p a[0] a[1] a[2] a[3] 2 2 3 3 4 5 5 4 p[0] p[1] p[2] 6 7 6 7 IT 279

  23. Call by value double fun(int n, double p) { for (int i=0; i < 2; i++) p = p-n; return p; } n p i 113.4 118.4 2 1 1+4=5 i k p s 123.4 int main() { int i=1,k=4; double p,s=123.4; p = fun(i+k,s); cout << i << “\n“ << k << “\n“ << p << “\n“ << s; } 1 4 113.4 123.4 113.4 IT 279

  24. Call by reference double fun(int n, double& p) { for (int i=0;i < 2;i++) p = p-n; return p; } n p i 1 2 1+4=5 &s i k p s int main() { int i=1,k=4; double p,s=123.4; p = fun(i+k,s); cout << i << “\n“ << k << “\n“ << p << “\n“ << s; } 1 4 113.4 113.4 113.4 118.4 IT 279 113.4

  25. Call by Value vs. Call by Reference int add_one_V(int n) { n++; cout << “1:“ << n << endl; return n; } n int sub_one_R(int &n) { n--; return n; } n 2 1 1:6 2:6 5 3:4 4 int main() { int a,b=5; a = add_one_V(b); cout << “2:“ << a << “ “ << b << endl; a = sub_one_R(b); cout << “3:“ << a << “ “ << b << endl; } &b a b 1 3 2 3 IT 279

  26. Passing Arrays as Parameters int top(int a[]) { int i=0; return a[i]; } a i int third(int a[]) { int i=2; return a[i]; } a i int main() { int a[4]={10,200,300,400}; cout << top(a) << endl; cout << third(a) << endl; } a a+2 10 300 IT 279

  27. Arrays passed as References void fun(int a[]) { int i=2; a[i] += 5; } a i int main() { int a[4]={10,200,300,400}; fun(a); cout << a[0] << “ “; cout << a[1] << “ “; cout << a[2] << “ “; cout << a[3] << endl; return 1; } a 305 a[2] 10 200 305 400 IT 279

  28. Passing the size of an Array void add_one_to_all(int A[], int size) { for (int i=0;i<size;i++) A[i]++; } i A size int main() { const int size=4; int a[size]={1,2,3,4}; add_one_to_all(a,size); for (int i=0;i<size;i++) cout << a[i] << “ “; return 1; } size i a 2 3 4 5 IT 279

  29. Vector passed as Value ? (right? wrong?) void add_one_to_all(vector<int> v) { for (int i=0;i<v.size();i++) v[i]++; } i v int main() { vector<int> a(4); int i; for (i=0;i<a.size();i++) a[i]=i+1; add_one_to_all(a); for (i=0;i<a.size();i++) cout << a[i] << “ “; } i a 1 2 3 4 IT 279

  30. Vector passed as Reference void add_one_to_all(vector<int> &v) { for (int i=0;i<a.size();i++) a[i]++; } i v int main() { vector<int> a(4); for (int i=0;i<a.size();i++) a[i]=i+1; add_one_to_all(a); for (int i=0;i<a.size();i++) cout << a[i] << “ “; } &a i a 2 3 4 5 IT 279

  31. C-string is an array of char } 4 bytes a b c d 1 byte } char *a = "Dennis"; char b[7] = {'D','e','n', 'n','i','s','\0'}; char *c=b; cout << *a << “ “ << b[0] << endl; cout << *(a+2) << “ “ << b[2] << endl; cout << a << “ “ << b << endl; char *d[2] = {c, "Li"}; cout << d[0] << " " << d[1] << endl;; 1 1 d[0] d[1] D D n n Dennis Dennis Dennis Li IT 279

More Related