Structures Part II aggregate operations arrays of type


![Arrays of Structures struct Salaried { char dept[5]; int salary; int vac_days; }; // Arrays of Structures struct Salaried { char dept[5]; int salary; int vac_days; }; //](https://slidetodoc.com/presentation_image_h2/0485bd4ead0675943119ef304a626fc2/image-3.jpg)
![Arrays of Structures emp[0]. dept = “Purc”; emp[0]. salary = 34560; emp[0]. vac_days = Arrays of Structures emp[0]. dept = “Purc”; emp[0]. salary = 34560; emp[0]. vac_days =](https://slidetodoc.com/presentation_image_h2/0485bd4ead0675943119ef304a626fc2/image-4.jpg)
![Arrays of Structures struct Payroll { int id; char name[15]; double payrate; }; // Arrays of Structures struct Payroll { int id; char name[15]; double payrate; }; //](https://slidetodoc.com/presentation_image_h2/0485bd4ead0675943119ef304a626fc2/image-5.jpg)

![Arrays of Structures struct Payroll { int id; char name[15]; double payrate; }; // Arrays of Structures struct Payroll { int id; char name[15]; double payrate; }; //](https://slidetodoc.com/presentation_image_h2/0485bd4ead0675943119ef304a626fc2/image-7.jpg)


![Arrays of Structures void showarray(Payroll staff[3]) { cout << setiosflags(ios: : fixed) << setprecision(2); Arrays of Structures void showarray(Payroll staff[3]) { cout << setiosflags(ios: : fixed) << setprecision(2);](https://slidetodoc.com/presentation_image_h2/0485bd4ead0675943119ef304a626fc2/image-10.jpg)





![Nested Structures void loadarray(vital_Data personnel[ARR_SIZE ]) { for(int ndx = 1; ndx < ARR_SIZE Nested Structures void loadarray(vital_Data personnel[ARR_SIZE ]) { for(int ndx = 1; ndx < ARR_SIZE](https://slidetodoc.com/presentation_image_h2/0485bd4ead0675943119ef304a626fc2/image-16.jpg)





- Slides: 21
Structures - Part II aggregate operations arrays of type struct nested structures compared to classes
Aggregate Operations Operation Arrays I/O No (except strings) No Assignment No Arithmetic No Comparison No Parameter pass. Ref. only Structs Yes No No Either value or ref. Funct. return value No Yes
Arrays of Structures struct Salaried { char dept[5]; int salary; int vac_days; }; // array of 20 elements, each of type Salaried emp[20];
Arrays of Structures emp[0]. dept = “Purc”; emp[0]. salary = 34560; emp[0]. vac_days = 14; . . . emp[2]. salary = 32100; emp[2]. dept = “Ship”; emp[2]. vac_days = 10; … emp[19]. dept = “Acct”; emp[19]. salary = 22500; emp[19]. vac_days = 12;
Arrays of Structures struct Payroll { int id; char name[15]; double payrate; }; // an array of 3 records of type Payroll employee[3];
Arrays of Structures // load array -- there are other ways to load the array Payroll employee[3] = { {11, “Begay”, 7. 25}, {12, “Gioseffi”, 6. 50}, {13, “Marra”, 9. 00} }; // display array for(ndx = 0; ndx < 3; ndx++) cout << ‘n’ << employee[ndx]. id << setw(20) << employee[ndx]. name << setw(20) << employee[ndx]. payrate;
Arrays of Structures struct Payroll { int id; char name[15]; double payrate; }; // prototypes void loadarray(Payroll [3]); void showarray(Payroll [3]); *
Arrays of Structures void main() { //declare array of 3 records of type Payroll // and initialize the first record Payroll employee[3] = { 11, "Begay", 7. 25 }; loadarray(employee); showarray(employee); cout << endl<<endl; } // end main() // calls // formatting
Arrays of Structures // load array - data typically entered via file input void loadarray(Payroll staff[3]) { // begin at 1 because [0] already entered for(int ndx = 1; ndx < 3; ndx++) { cout << "Enter the ID, name, and pay rate: "; cin >> staff[ndx]. id >> staff[ndx]. name >> staff[ndx]. payrate; cout << endl; } }
Arrays of Structures void showarray(Payroll staff[3]) { cout << setiosflags(ios: : fixed) << setprecision(2); } for(int ndx = 0; ndx < 3; ndx++) cout << 'n' << setw(5) << staff[ndx]. id << setw(13) << staff[ndx]. name << setw(10) << staff[ndx]. payrate;
In Class Assign. 3 Element at. num tungsten (W) sulfur (S) 16 carbon (C) at. mass density 74 183. 850 19. 300 32. 064 2. 07 6 12. 011 2. 260 9. Write a program which: a. creates an array of structures b. uses a global constant for the array size c. contains functions to load and to display d. format similar to the above chart
In Class Assign. 3 -ans. #include<iostream. h> #include<iomanip. h> struct Element { char symbo; int at_num; double at_mass; double density; }; const int ARR_SIZE = 3; void main() { //declare array - type Element atom[ARR_SIZE ]; loadarray(atom); showarray(atom); // calls cout << endl<<endl; } // end main() void loadarray(Element [ARR_SIZE ]); void showarray(Element [ARR_SIZE ]);
Nested Structures struct Date { int month; int day; int year; }; struct vital_Data { char name[15]; char dept[10]; int ID; Date birth; // Date must be previously defined double payrate; }; *
Nested Structures vital_Data Courtney; // declaration of an object // assignments of data to an object Courtney. name = “Lawrence”; Courtney. dept = “personnel”; Courtney. ID = 1234; Courtney. birth = {10, 25, 87}; // this is a struct Coutrney. payrate = 12. 75;
Nested Structures 1. Write the cin statements for the department and the birthday. 2. Write the cout statements for the department and the birthday.
Nested Structures void loadarray(vital_Data personnel[ARR_SIZE ]) { for(int ndx = 1; ndx < ARR_SIZE ; ndx++) { cout << "n. Enter the name: "; cin >>personnel[ndx]. name; cout << "Enter the department: "; cin >> personnel[ndx]. dept; cout << "Enter the id# and the payrate: "; cin >> personnel[ndx]. ID >>personnel[ndx]. payrate; cout << "Enter the birth date (dd mm yy) "; cin >> personnel[ndx]. birth. day >> personnel[ndx]. birth. month >> personnel[ndx]. birth. year; } } *** **
Formatted Output ID# name department birthday payrate 1234 Lawrence personnel 10/ 5/87 12. 75 cout << setiosflags(ios: : fixed | ios: : right); 765 Newman shipping 2/29/59 for(int ndx = 0; ndx < ARR_SIZE ; ndx++) 3. 11 cout << 'n' << setw(5) << setprecision(0) << personnel[ndx]. ID << setw(12) << personnel[ndx]. name << setw(11) << personnel[ndx]. dept << setw(5) << personnel[ndx]. birth. day <<'/' << setw(2) << personnel[ndx]. birth. month <<'/' << setw(2) << personnel[ndx]. birth. year << setw(7) << setprecision(2) << personnel[ndx]. payrate;
Structure vs. Class By default: default struct have public member variables class have private member variables
Class syntax: class Classname { public: list of function prototypes private: list of private variable declarations };
Class An example: class Date { public: }; private: int day; int month; int year;
Imagination is more important than knowledge. Albert Einstein