Structured Data (Lecture 07)

Slides:



Advertisements
Similar presentations
Starting Out with C++, 3 rd Edition 1 Chapter 11 – Structured Data Abstract data types (ADTs) are data types created by the programmer. ADTs have their.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Lesson 11 Structured Data CS1 Lesson John Cole1.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Chapter 11 – Structured Data
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 10 Structured Data.
More Storage Structures A Data Type Defined by You Characteristics of a variable of a specific ‘data type’ has specific values or range of values that.
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Lecture 18: Structured Data Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
CS1201: Programming Language 2 Structure By: Nouf Almunyif.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
Chapter 7 A Data Types – Structures Structures Structure: C++ construct that allows multiple variables to be grouped together Structure Declaration.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
Chapter Structured Data 11. Combining Data into Structures 11.2.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Bill Tucker Austin Community College COSC 1315
Chapter 12 Classes and Abstraction
Structured Data.
Chapter Topics The Basics of a C++ Program Data Types
Structured Data Abstract data types (ADTs) are data types created by the programmer. ADTs have their own range (or domain) of data and their own set of.
Classes and Data Abstraction
11 Chapter Structured Data
CO1401 Program Design and Implementation
Chapter 7 – Arrays.
Chapter 7: Introduction to Classes and Objects
Basic Elements of C++.
Objectives Identify the built-in data types in C++
Chapter 11: Structured Data.
Introduction to C++ October 2, 2017.
C++ Arrays.
By: Syed Shahrukh Haider
Data Types – Structures
Basic Elements of C++ Chapter 2.
Structures Lesson xx In this module, we’ll introduce you to structures.
Enumeration used to make a program more readable
Data Types – Structures
Chapter 2 Elementary Programming
CS148 Introduction to Programming II
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Starting Out with C++: From Control Structures through Objects
Chapter 11: Structured Data.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Chapter 2 Variables.
CS148 Introduction to Programming II
Structured Data Types array union struct class.
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Arrays Arrays A few types Structures of related data items
Fundamental Programming
Standard Version of Starting Out with C++, 4th Edition
Modifying Objects Assignment operation Assignment conversions
Structures Structured Data types Data abstraction structs ---
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Variables and Constants
Presentation transcript:

Structured Data (Lecture 07) By: Dr. Norazah Yusof FSKSM, UTM

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

Abstract Data Type Example: Write a program to simulate a 12-hour clock.

Abstract Data Type Example: Write a program to simulate a 12-hour clock. The program could contain three ADTs: Hours Minutes Seconds The range of values Hours : integers 1 through 12 Minutes : integers 0 through 59 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.

Combining Data into Structures Structure – group several variables together into a single item Declaration format of a structure: struct tag { variable declaration; //more declaration; };

Example: Structure declaration of Time struct Time { int hours; int minutes; int seconds; };

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;

Memory layout of structure variable, now hours minutes seconds Time Members

Example 2 Declare a structure named PayRoll that has the following members: Employee number: integer Employee’s name : array of characters Hours worked : floating point numbers Hourly pay rate : floating point numbers Gross pay : floating point numbers Define three PayRoll variables: deptHead, foreman, associate Draw the memory layout.

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;

Memory Layout of PayRoll variables empNumber name hourWork deptHead hourPayRate grossPay foreman empNumber name hourWork hourPayRate grossPay associate empNumber name hourWork hourPayRate grossPay

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

Assessing Structure Members deptHead.empNumber = 475; strcpy(deptHead.name, "Muhammad"); deptHead.hourWork = 200; deptHead.hourPayRate = 15; deptHead.grossPay = deptHead.hourWork * deptHead.hourPayRate;

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!

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 << "Gross pay: " << deptHead.grossPay << endl;

Read data from the keyboard Change Example 2: Read data from the keyboard for the members name, hourWork, and hourPayRate.

Read data from the keyboard cout << "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;

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)

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; };

Initializing a Structure structure CityInfo { char cityName[30]; char state[3]; long population; int distance; }; CityInfo location = {"Johor Bahru", "JH", 80000, 280}; CityInfo location = {"Kuantan", "PH", 50000}; CityInfo location = {"Ipoh", "PK"}; CityInfo location = {"Kuala Terengganu"}; CityInfo location = {"Seremban", "NS", ,68};//illegal!