Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Homogeneous aggregate // Using array string name[20]; int mid1[20]; int final[20]; double GPA[20]; name[0] = "Tom"; GPA[2] = 3.21; name: Tom, John, student-3,

Similar presentations


Presentation on theme: "1 Homogeneous aggregate // Using array string name[20]; int mid1[20]; int final[20]; double GPA[20]; name[0] = "Tom"; GPA[2] = 3.21; name: Tom, John, student-3,"— Presentation transcript:

1 1 Homogeneous aggregate // Using array string name[20]; int mid1[20]; int final[20]; double GPA[20]; name[0] = "Tom"; GPA[2] = 3.21; 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 // Using tvector class #include "tvector.h".............. tvector name(20); tvector mid1(20); tvector final(20); tvector GPA(20); name[1] = "John"; GPA[19] = 3.67;

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

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

4 4 Enumerated Types enum day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; tvector MyclassHr(7); MyclassHr[Monday] = 2; MyclassHr[Tuesday] = MyclassHr[Thursday] = 0; MyclassHr[Wednesday] = 4; day ThreeDays[3]; day ADay; ThreeDay[0]=Saturday;...... if (ADay == Saturday || ADay == Sunday) cout << "It's weekend"; day FirstDay = day(0); Sunday Monday Tuesday Wednesday Thursday Friday Saturday Monday Tuesday Wednesday Thursday Friday Saturday Sunday

5 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 A(3,3); apmatrix B(3,3); apmatrix C(3,3); A[1][2] = 3; A[1][1] = 1; C[2][1] = A[2][1]+B[2][1]; 132 113 012 240 211 021 372 324 033 + =

6 6 Operation on Matrix // Using apmatrix class #include "apmatrix.h".............. apmatrix A(3,3); apmatrix B(3,3); apmatrix C(3,3); int i,i;..... for (i=0;i<A.numrows();i++) for (j=0;j<A.numcols();j++) C[i][j] = A[i][j] + B[i][j]; Challenging problem: How to do multiplication? Easy problem: How to printout a matrix?

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

8 8 Resize a vector student 1 student 2 student 3 student 4 x#@%.. CS2 CS2[3] CS2[2] CS2[1] CS2[0] CS2[4] CS2[5] the last push back here CS2.resize(6)

9 9 Reserve a vector student 1 student 2 student 3 student 4 x#@%.. CS2 CS2[3] CS2[2] CS2[1] CS2[0] CS2[4] CS2[5] the last push back here CS2.resize(6) CS2.reserve(8)

10 10 pop back a vector student 1 student 2 student 3 student 4 x#@%.. CS2 CS2[3] CS2[2] CS2[1] CS2[0] CS2[4] CS2[5].... CS2.resize(6); CS2.pop_back(); reserved size

11 11 Stack and Queue Stack: LCFS (Last Come First Service) c1c2 c3 c5 c4 c2c3c4 c5 c1 Queue: FCFS (First Come First Service) Front Back

12 12 in STL (Double Ended Queue) #include.............. deque 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; } student 2 student 1 student 2 CS2 CS2[3] CS2[2] CS2[1] CS2[0]

13 13 pop back and pop front a deque student 1 student 2 student 3 student 4 student 5 x#@%.. CS2 CS2[3] CS2[2] CS2[1] CS2[0] CS2[4].... CS2.pop_front(); CS2.pop_back(); student 1 student 2 student 3 student 4 student 5 x#@%.. CS2 CS2[2] CS2[1] CS2[0] CS2[4]


Download ppt "1 Homogeneous aggregate // Using array string name[20]; int mid1[20]; int final[20]; double GPA[20]; name[0] = "Tom"; GPA[2] = 3.21; name: Tom, John, student-3,"

Similar presentations


Ads by Google