Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.

Similar presentations


Presentation on theme: "Chapter 6 Data Structures Program Development and Design Using C++, Third Edition."— Presentation transcript:

1 Chapter 6 Data Structures Program Development and Design Using C++, Third Edition

2 2 Objectives You should be able to describe: Structures Arrays of Structures Problem solving

3 Program Development and Design Using C++, Third Edition 3 Structures (Textbook Chapter 15) Historical holdovers from C Record: Provides for storing information of varying types under one name. Several related items of information can be viewed as a single entity Data field: Individual data items in a record Structure: A record C++ permits methods to be included in a structure (class)

4 Program Development and Design Using C++, Third Edition 4 Structure Definitions Structures  Aggregate data types built using elements of other types struct Time { int hour; int minute; int second; }; Structure member naming  In same struct : must have unique names  In different struct s: can share name struct definition must end with semicolon Structure tag Structure members

5 Program Development and Design Using C++, Third Edition 5 Structure Definitions struct definition  Creates new data type used to declare variables  Structure variables declared like variables of other types  Examples: Time timeObject; declares timeObject to be a variable of type Time VariableData Type

6 Program Development and Design Using C++, Third Edition 6 Accessing structure members Member access operators  Dot operator (. ) for structure members Example:  Read value to member hour of timeObject : cin >> timeObject.hour; Or..  Assign value: timeObject.hour=11;  Print member hour of timeObject : cout << timeObject.hour;

7 Program Development and Design Using C++, Third Edition 7 Example Create a new data type called date : struct date { char WeekDay[3] ; int DayOfMonth ; int MonthOfYear ; int Year ; }; Declare a variable called TodaysDate of type date : date TodaysDate;

8 Program Development and Design Using C++, Third Edition 8 Example Program #include struct date { char WeekDay[4] ; int DayOfMonth ; int MonthOfYear ; int Year ; }; void main() { date TodaysDate; cout<<“Please enter the day of month:”; cin>>TodaysDate.DayOfMonth; //Read the day from user. cout<<“Please enter the month:”; cin>>TodaysDate.MonthOfYear; //Read the month from user. TodaysDate.Year=2008; //Assign the year. strcpy(TodaysDate.WeekDay,”Fri”); //Assign the weekday. NOTE!! //Use of function strcpy //to assign to a string

9 Program Development and Design Using C++, Third Edition 9 cout<<“The date is: ”; cout<<TodaysDate.WeekDay<<“, ”<<TodaysDate.DayOfMonth<<“/” <<TodaysDate.MonthOfYear<<“/”<<TodaysDate.Year<<endl; }//end main Example output following user input 28 (day) and 3 (month) The date is: Fri, 28/3/2008

10 Program Development and Design Using C++, Third Edition 10 Structures using structures Members of a structure can be any valid C++ data type, including arrays and structures. struct event { char description[20] ; date when ; // when is of type date, which is another structure //defined already } ; … event JFK; //declare variable of type event JFK.when.DayOfMonth = 22 ; JFK.when.MonthOfYear = 11 ; JFK.when.Year = 1963 ;

11 Program Development and Design Using C++, Third Edition 11 Structures using structures An array of characters is used as a member of the event structure. Accessing an element of a member array requires giving the structure’s name, followed by a period, followed by the array designation. For example: JFK.description[0] refers to the first character in the array called description which is a member of the structure event.

12 Program Development and Design Using C++, Third Edition 12 Structures using structures We can assign characters to a member array in the same way as with a normal string. strcpy(JFK.description,”J.F.Kennedy is shot”); Assigns the string J.F.Kennedy is shot to the string decription. Output the string: cout<<JFK.description;

13 Program Development and Design Using C++, Third Edition 13 Arrays of Structures Create an array of 10 events: event HistoryList[10] ; To print out the year associated with the 3rd. element of the sequence of events we could write a cout statement like this: cout << HistoryList[2].when.year ;

14 Program Development and Design Using C++, Third Edition 14 Example Program –Payroll #1 Create a data structure suitable for holding the information of an employee: name, ID number, address, phone, rate, hours worked in a week. Create an data structure suitable for holding the information of 10 employees. Calculate the gross pay of each employee and print it out.

15 Program Development and Design Using C++, Third Edition 15 Payroll program #1 #include const int NUMEMPS = 10; struct EmployeeDetails { char name[40]; char ID[10]; char address[50]; char phone[15]; double rate; double hours; };

16 Program Development and Design Using C++, Third Edition 16 void main() { EmployeeDetails employee[NUMEMPS]; int i; for(i = 0; i < NUMEMPS; i++) { cout << "Enter employee number: "; cin >> employee[i].ID; cin.ignore(); cout << "Enter employee name: "<<endl; cin.getline(employee[i].name,40); cout << "Enter employee phone number: "; cin >> employee[i].phone; cout << "Enter employee rate: "; cin >> employee[i].rate; cout << "Enter employee hours worked: "; cin >> employee[i].hours; cout<<endl<<endl; }

17 Program Development and Design Using C++, Third Edition 17 for( i = 0; i < NUMEMPS; i++) { cout<< "Employee number "<< employee[i].ID<<endl; cout<<"Gross Pay=" <<employee[i].rate*employee[i].hours<<endl; cout<<"*******************************"<<endl; }

18 Program Development and Design Using C++, Third Edition 18 Example Program –Payroll #2 Modify the previous program so that our data structure includes the grade of each employee (i.e. grade 0, 1 or 2), the hours worked for each day of the week. Calculate the weekly pay of each employee based on his/her grade (which corresponds to certain rate) and keep this in the data structure also. NOTE: Weekend rates are multiplied by an overtime premium rate of 1.5.

19 Program Development and Design Using C++, Third Edition 19 Example Grade:0, 1, 2 Rate: 4.25, 5.00, 5.50 corresponding to grade E.g. John Smith is grade 1 employee which corresponds to a rate of 5 Euro per hour. John has worked the following hours: Monday Tuesday Wednesday Thursday Friday Saturday Sunday 8 6.5 7 9 6 3 4 His pay should be: 8*5 + 6.5*5 + 7*5 + 9*5 + 6*5 + 3*5*1.5 + 4*5*1.5 Saturday Sunday

20 Program Development and Design Using C++, Third Edition 20 #include const int NUMEMPS = 10; const double overtime=1.5; const double rate[3]={4.25, 5.00, 5.50}; struct EmployeeDetails { char name[40]; char ID[10]; char address[50]; char phone[15]; int grade; //0,1 or 2 double hours[7]; double pay; }; Example Program –Payroll #2

21 Program Development and Design Using C++, Third Edition 21 void main() { EmployeeDetails employee[NUMEMPS]; int i,j; for(i = 0; i < NUMEMPS; i++) { cout << "Enter employee number: "; cin >> employee[i].ID; //read rest of details... cout << "Enter employee grade (0,1 or 2): "; cin >> employee[i].grade; cout << "Enter employee hours worked for each day (day 0 is Monday, 1 is Tuesday...):"<<endl; for (j=0;j<7;j++) { cout<<"Day "<<j<<":"; cin>>employee[i].hours[j]; } employee[i].pay=0.0; cout<<endl<<endl; }

22 Program Development and Design Using C++, Third Edition 22 for( i = 0; i < NUMEMPS; i++) //calculate pay for each employee { for (j=0;j<5;j++) //pay for Mon-Fri inclusive employee[i].pay=employee[i].pay+ rate[employee[i].grade]*employee[i].hours[j]; for (j=5;j<7;j++) //pay for Sat & Sun employee[i].pay=employee[i].pay+ rate[employee[i].grade]*employee[i].hours[j]*overtime; } for( i = 0; i < NUMEMPS; i++) { cout<< "Employee number: "<< employee[i].ID<<endl; cout<<"Employee grade: "<<employee[i].grade<<endl; cout<<"Employee pay: "<<employee[i].pay<<endl; cout<<"*******************************"<<endl; }

23 Program Development and Design Using C++, Third Edition 23 Exercises (Textbook-chapter 15): pg. 796: ex.1a-e, 2a-e.


Download ppt "Chapter 6 Data Structures Program Development and Design Using C++, Third Edition."

Similar presentations


Ads by Google