Download presentation
Presentation is loading. Please wait.
Published byArchibald Hudson Modified over 8 years ago
1
Topic 4 Data Structures Program Development and Design Using C++, Third Edition
2
2 Objectives You should be able to describe: Structures Structures using 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: Used 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 Example A student record: John Smith S748675 3.56 62 Name ID CPA Credits string float int
5
Program Development and Design Using C++, Third Edition 5 Another Structure Example A Time record: 18 5 20 hour minute second int
6
Program Development and Design Using C++, Third Edition 6 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
7
Program Development and Design Using C++, Third Edition 7 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 Variable Data Type timeObject hour minute second
8
Program Development and Design Using C++, Third Edition 8 Accessing structure members Member access operators Dot operator (. ) for structure members: timeObject.hour timeObject.minute timeObject.second
9
Program Development and Design Using C++, Third Edition 9 Accessing structure members (ctd’) Read value to member hour of timeObject : cin >> timeObject.hour; Assign value: timeObject.hour=11; Print member hour of timeObject : cout << timeObject.hour; minute second timeObject hour 11
10
Program Development and Design Using C++, Third Edition 10 Example: date Create a new data type called date : struct date { char WeekDay[4] ; int DayOfMonth ; int Month; int Year; }; Declare a variable called TodaysDate of type date : date TodaysDate; DayOfMonth Month TodaysDate WeekDay Year
11
Program Development and Design Using C++, Third Edition 11 Example Program: date #include using namespace std; struct date { char WeekDay[4] ; int DayOfMonth ; int Month; 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.Month; //Read the month from user. TodaysDate.Year=2010; //Assign the year. strcpy(TodaysDate.WeekDay,”Fri”); //NOTE!!Use of function strcpy } //to assign to a string DayOfMonth Month TodaysDate WeekDay Year 19 3 3 Fri 2010 \0
12
Program Development and Design Using C++, Third Edition 12 Example Program: date (ctd’) cout<<“The date is: ”; cout<<TodaysDate.WeekDay<<“, ”<<TodaysDate.DayOfMonth<<“/” <<TodaysDate.Month<<“/”<<TodaysDate.Year<<endl; }//end main Example output following user input 19 (day) and 3 (month) The date is: Fri, 19/3/2010
13
Program Development and Design Using C++, Third Edition 13 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 ; } ; event JFK; DayOfMonth Month WeekDay Year description when JFK
14
Program Development and Design Using C++, Third Edition 14 Structures using structures when is of type date (a stucture already defined) and is a member of the event structure. JFK.when is of type date Acessing an element of the data structure requires a dot followed by a data field. For example: JFK.when.Month 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.
15
Program Development and Design Using C++, Third Edition 15 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.K. is shot”); Assigns the string J.F.K. is shot to the string decription. Output the string: cout<<JFK.description;
16
Program Development and Design Using C++, Third Edition 16 Example Program: JFK #include using namespace std; struct date { char WeekDay[4] ; int DayOfMonth ; int Month; int Year; }; struct event{ char description[20]; date when; }; void main() { event JFK; JFK.when.DayOfMonth = 22; JFK.when.Month = 11; JFK.when.Year = 1963; strcpy(JFK.description,"J.F.K. is shot"); DayOfMonth Month WeekDay Year description JFK when 22 11 1963 J. F. K. i s s ho t \0
17
Program Development and Design Using C++, Third Edition 17 //display info cout<<JFK.when.DayOfMonth<<"/"<<JFK.when.Month<<"/" <<JFK.when.Year<<":"; cout<< JFK.description<<endl; } //end main Example Program: JFK (continued)
18
Program Development and Design Using C++, Third Edition 18 Example Program: Accessing the data fields DayOfMonth Month WeekDay Year description JFK when JFK.when.Month
19
Program Development and Design Using C++, Third Edition 19 Arrays of Structures Create an array of 10 events: event HistoryList[10] ; DayOfMonth Month WeekDay Year DayOfMonth Month WeekDay Year DayOfMonth Month WeekDay Year ……. 019 HistoryList
20
Program Development and Design Using C++, Third Edition 20 Arrays of Structures 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 ;
21
Program Development and Design Using C++, Third Edition 21 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.
22
Program Development and Design Using C++, Third Edition 22 Payroll program #1 #include using namespace std; const int NUMEMPS = 10; struct EmployeeDetails { char name[40]; char ID[10]; char address[50]; char phone[15]; double rate; double hours; };
23
Program Development and Design Using C++, Third Edition 23 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; }
24
Program Development and Design Using C++, Third Edition 24 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; }
25
Program Development and Design Using C++, Third Edition 25 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.
26
Program Development and Design Using C++, Third Edition 26 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
27
Program Development and Design Using C++, Third Edition 27 #include using namespace std; 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
28
Program Development and Design Using C++, Third Edition 28 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; }
29
Program Development and Design Using C++, Third Edition 29 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; }
30
Program Development and Design Using C++, Third Edition 30 Exercises (Textbook-chapter 15): pg. 796: ex.1a-e, 2a-e. pg. 801: ex. 1, 5a,b.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.