## PIC10A Lecture 18

p224-236, p191-203

### Structure

#### Use array and other structure in a sturcutre

• You can use arrays as members of a structure.
• You can also use other structures as members of a structure.
 ```#includeusing namespace std;struct Date { int month; int day; int year;};struct Employee { char name[51]; // 50 characters char gender; Date birthday;// Date, defined above double income; }; // display information of employee void display(Employee employee); // display information of date, function overloading void display(Date date); int main() { // magic formula cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); // --------------- name, gender, date , income Employee david = {"David", 'M', {11,1,71}, 122322.22}; display(david); cout << endl; Employee mary; // setting mary's data strcpy(mary.name,"Mary"); mary.gender = 'F'; Date date = {5,6,80}; mary.birthday = date; mary.income = 31415; display(mary); return 0; } void display(Employee employee) { cout << "Name: " << employee.name << endl << "Gender: " << employee.gender << endl << "Date of Birth: "; display(employee.birthday); cout << endl << "Annual income: " << employee.income << endl; }void display(Date date) { cout << date.month << "/" << date.day << "/" << date.year;}```

Output
 `Name: DavidGender: MDate of Birth: 11/1/71Annual income: 122322.22Name: MaryGender: FDate of Birth: 5/6/80Annual income: 31415.00`

Another example
 ```#includeusing namespace std;struct Score { char classID[10]; int studentNum; // less than a 100; double scores[100]; }; double average(Score score); int main() { // magic formula cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); //------------ classID, studentNum, scores Score score = {"PIC10A", 5, {85.2, 30.1, 42, 55.55, 98}}; cout << "The average score of " << score.classID << " class is " << average(score) << endl; return 0; } double average(Score score) { double sum=0; for(int i=0; i < score.studentNum; i++) { sum+=score.scores[i]; } return sum/score.studentNum; } ```

Output
 The average score of PIC10A class is 62.17

Use structure in array
• You can use structure in an array

Example
 ```#includeusing namespace std;struct Date { int month; int day; int year;};void display(Date date);int main() { Date holidays[3] = {{12,25,2002}, {11,11,2002}, {1,1, 2003}}; for(int i = 0; i < 3; i++) { display(holidays[i]); cout << endl; } return 0; } void display(Date date) { cout << date.month << "/" << date.day << "/" << date.year; } ```

Output
 `12/25/200211/11/20021/1/2003`

### Sorting

Program
 `#includeusing namespace std;// return the index of the minimun after startIndexint minIndex(int a[], int size, int startIndex);// swap the content of 2 integers. void swap(int& a, int& b);// sort the array use selection sortvoid sort(int a[], int size);// display the arrayvoid display(int a[], int size);int main() { int a[] = {8,6,10,2,16,4,18,14,12,20}; cout << "Before sorting: \n"; display(a,10); cout << endl; sort(a, 10); cout << "After sorting: \n"; display(a,10); cout << endl; return 0;}int minIndex(int a[], int size, int startIndex) { int min = a[startIndex]; // assume the first one is the min int minInd = startIndex; for(int i = startIndex + 1; i < size; i++) { if(min > a[i]) { // a[i] is actually smaller minInd = i; min = a[i]; } } return minInd;}void swap(int& a, int& b) { int temp = a; a = b; b = temp;}void sort(int a[], int size) { for(int i = 0; i < size; i++) { int minInd = minIndex(a, size, i); // swap a[i] and a[minInd] swap(a[i], a[minInd]); }}void display(int a[], int size) { for(int i = 0; i < size; i++) { cout << a[i] << " "; }}`

Output
 `Before sorting:8 6 10 2 16 4 18 14 12 20After sorting:2 4 6 8 10 12 14 16 18 20`
The smallest number is 2(index is 3)
 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[9] a[9] 8 6 10 2 16 4 18 14 12 20

Exchange it with the 0th element.
 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[9] a[9] 2 6 10 8 16 4 18 14 12 20

The next smallest number is 4 (index is 5)
 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[9] a[9] 2 6 10 8 16 4 18 14 12 20

Exchange it with the element right after 2.
 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[9] a[9] 2 4 10 8 16 6 18 14 12 20

The next smallest number is 6 (index is 5)
 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[9] a[9] 2 4 10 8 16 6 18 14 12 20

Exchange it with the element right after 4.
 a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[9] a[9] 2 4 6 8 16 10 18 14 12 20
The process goes on till all the numbers are sorted.
`for(int index = 0; index < size; index++)  {   place the indexth smallest element in a[index];}`
In more details
`for(int index = 0; index < size; index++) {    find the smallest element among a[index], a[index+1],...,a[size-1]	let the index corresponding the the element be minIndex	swap a[index] and a[minIndex]}`
How to find the min element and the min index?
Assume the first one is the smallest. `min` is 3, `minIndex` is 0.
 3 2 5 1 4

Compare the `min 3`, to the 1st element `2`. `2` is smaller.
Now `min` is 2, `minIndex` is 1.
 3 2 5 1 4

Compare `2` to the next element `5`. `2` is still the min.
 3 2 5 1 4

Compare `2`, to the next element `1`. `1` is smaller.
Now `min` is 1, `minIndex` is 3.
 3 2 5 1 4

Compare `1` to the next element `4`. `1` remains the smallest.
`min` is 1, `minIndex` is 3
 3 2 5 1 4
The codes
`int min = a[0];int minIndex = 0;for(int index = 1; index < size; i++) {   compare min to a[index]   if a[index] is smaller       minIndex = index       min = a[index]}`
Swap
The following swap function won't work. Why?
`void swap(int& a, int& b) {   a = b;   b = a;}`
The correct one is
 `void swap(int& a, int& b) { int temp = a; a = b; b = temp;}`