1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.
-
Upload
prudence-boyd -
Category
Documents
-
view
216 -
download
0
Transcript of 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.
![Page 1: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/1.jpg)
1
Structured Data Structured Data (Lecture 11)(Lecture 11)
By:
Dr. Norazah Yusof
FSKSM, UTM
![Page 2: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/2.jpg)
2
Data Types■ Primitive data types - defined as a basic part of the
language.
■ Abstract Data Type (ADT) – created by the programmer and is composed of one or more primitive data types.
■ Have own range (or domain) of data and own set of operations that may be performed on them.
bool int unsigned long int
char long int float
unsigned char unsigned short int double
short int unsigned int long double
![Page 3: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/3.jpg)
3
Abstract Data Type■Example: Write a program to
simulate a 12-hour clock.
![Page 4: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/4.jpg)
4
Abstract Data Type■ Example: Write a program to simulate a 12-hour
clock.■ The program could contain three ADTs:
1. Hours2. Minutes3. Seconds
■ The range of values 1. Hours : integers 1 through 122. Minutes : integers 0 through 593. Seconds : integers 0 through 59
■ If an Hours object is set to 12 and then incremented, it will then take on the value 1.
■ If an Minutes object or Seconds object is set to 59 and then incremented, it will then take on the value 0.
![Page 5: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/5.jpg)
5
Combining Data into Structures
■ Structure – group several variables together into a single item
■ Declaration format of a structure:struct tag{
variable declaration;
//more declaration;
};
![Page 6: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/6.jpg)
6
Example: Structure declaration of Timestruct Time
{
int hours;
int minutes;
int seconds;
};
![Page 7: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/7.jpg)
7
Definition of Structure Variables
■ Structure declaration does not define a variable. It only create a new data type.
■ Variable(s) of this type can be defined.
■ Format:tag varname;
■ Example: Define structure variable named now of type Time.
Time now;
![Page 8: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/8.jpg)
8
Memory layout of structure variable, now
hours
minutes
seconds
Time
Members
![Page 9: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/9.jpg)
9
Example 2■ Declare a structure named PayRoll that has the
following members:1. Employee number: integer2. Employee’s name : array of characters3. Hours worked : floating point numbers4. Hourly pay rate : floating point numbers5. Gross pay : floating point numbers
■ Define three PayRoll variables: deptHead, foreman, associate
■ Draw the memory layout.
![Page 10: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/10.jpg)
10
Example 2: Structure declaration of PayRoll and variables definition.
cont int SIZE = 25;
struct PayRoll
{
int empNumber;
char name[SIZE];
double hourWork, hourPayRate, grossPay;
};
PayRoll deptHead, foreman, associate;
![Page 11: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/11.jpg)
11
Memory Layout of PayRoll variables
empNumber
name
hourWork
deptHead
hourPayRate
grossPay
associate
empNumber
name
hourWork
hourPayRate
grossPay
foreman
empNumber
name
hourWork
hourPayRate
grossPay
![Page 12: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/12.jpg)
12
Assessing Structure Members■ Dot operator (.) allows you to access structure
members in a program.
■ Example 1: Access the empNumber member of deptHead by assigning value of 475 to it.
■ Example 2: Access the name, hourWork, and hourPayRate by assigning values of Muhammad, 200, and 15 to it, respectively. Then, assign the value to the grossPay by assigning the result of arithmetic operation: hourWork x hourPayRate
■ Example 3: Display the contents of deptHead’s member
![Page 13: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/13.jpg)
13
Assessing Structure Members
deptHead.empNumber = 475;
strcpy(deptHead.name, "Muhammad");
deptHead.hourWork = 200;
deptHead.hourPayRate = 15;
deptHead.grossPay =
deptHead.hourWork * deptHead.hourPayRate;
![Page 14: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/14.jpg)
14
Assessing Structure Members
■ Example 3: Display the contents of deptHead’s member.– Cannot display the content by passing the entire
variable:
cout << deptHead; //will not work!
![Page 15: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/15.jpg)
15
Assessing Structure Members■ Example 3: Display the contents of deptHead’s member. Each member must be passed to cout, separately.
cout << "Name: " << deptHead.name << endl;
cout << "ID Number: " << deptHead.empNumber << endl;
cout << "Hours worked: " << deptHead.hourWork;
cout << endl;
cout << "Hourly pay rate: " << deptHead.hourPayRate;
cout << endl;
cout << "Gross pay: " << deptHead.grossPay << endl;
![Page 16: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/16.jpg)
16
Read data from the keyboard
■Change Example 2: Read data from the keyboard for the members name, hourWork, and hourPayRate.
![Page 17: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/17.jpg)
17
Read data from the keyboardcout << "Enter the employee number:";
cin >> deptHead.empNumber; cout << "Enter the employee’s name:"; cin.ignore();
//ignore the next character in // the input buffer.
cin.getline(deptHead.name, SIZE); cout << "How many hours did the employee work?"; cin >> deptHead.hourWork; cout << "What is the employee’s hourly pay rate?"; cin >> deptHead.hourPayRate;
![Page 18: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/18.jpg)
18
Comparing Structure Variables■ Cannot perform comparison operations
directly on structure variables. if (deptHead == foreman) //Error!
■ To compare between two structures, need to compare individual members, as follows: if (deptHead.hourWork == foreman.hourWork) :if (strcmp(deptHead.name,foreman.name) == 0) :
![Page 19: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/19.jpg)
19
Initializing a Structure
■ The members of a structure variable may be initialized with starting values when it is defined.
■ Example: Declare a structure variable named CityInfo. Define a variable named location with initialization values. structure CityInfo{ char cityName[30]; char state[3]; long population; int distance;};
![Page 20: 1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.](https://reader035.fdocuments.in/reader035/viewer/2022062500/5697bf8b1a28abf838c8b5da/html5/thumbnails/20.jpg)
20
Initializing a Structure
structure CityInfo{ char cityName[30]; char state[3]; long population; int distance;};i. CityInfo location = {"Johor Bahru", "JH", 80000, 280};
ii. CityInfo location = {"Kuantan", "PH", 50000};iii. CityInfo location = {"Ipoh", "PK"};iv. CityInfo location = {"Kuala Terengganu"};v. CityInfo location = {"Seremban", "NS", ,68};//illegal!