Chapter 11: Structured Data. Slide 11- 2 Introduction An array makes it possible to access a list or table of data of the same data type by using a single.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Advertisements

Lesson 11 Structured Data CS1 Lesson John Cole1.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Data Structures Lecture 3: Struct Azhar Maqsood NUST Institute of Information Technology (NIIT)
Starting Out with C++: Early Objects 5th Edition
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 11 – Structured Data
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
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.
Lecture 18: Structured Data Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 Principles of Programming I Note Set #12. 2 Semester Overview Functions Character File I/O Arrays Pointers and Dynamic Memory Allocation Characters.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 10: Records ( struct s)
CS1201: Programming Language 2 Structure By: Nouf Almunyif.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
計算機程式語言 Lecture 13-1 國立臺灣大學生物機電系 13 Structures.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Programmer Defined Structures (Records)
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 7: Introduction.
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.
Structured Data and Classes Chapter 7. Combining Data into Structures Structure: C++ construct that allows multiple variables to be grouped together Structure.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Structures Combining data types into a logical groupings.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures.
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.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Structured Data and Classes
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 7 Arrays.
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.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
C Programming Structured Data.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7. 12: Structures Starting Out with C++ Early Objects Eighth.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Chapter Structured Data 11. Combining Data into Structures 11.2.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Structured Data.
11 Chapter Structured Data
CO1401 Program Design and Implementation
Chapter 7: Introduction to Classes and Objects
Chapter 11: Structured Data.
Data Types – Structures
Data Types – Structures
Heterogeneous aggregate datatypes
Chapter 11: Structured Data.
Arrays Arrays A few types Structures of related data items
Standard Version of Starting Out with C++, 4th Edition
Structure (i.e. struct) An structure creates a user defined data type
Programming Fundamental
Presentation transcript:

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 variable name. At times, however, we may want to store information of varying types, such as a string name, an integer part number, and a real price, together in one structure. A data structure that stores different types of data under a single variable name is called a record For example, consider preparing an employee record. Each of the individual data items listed below is an entity by itself that is referred to as a data field. Taken together, all the data fields form a single unit that is referred to as a record. In C++, a record is referred to as a structure. Name: Identification Number: Regular Pay Rate: Overtime Pay Rate:

Slide Combining Data into Structures Structure: C++ construct that allows multiple variables to be grouped together General Format: struct { type1 field1; type2 field2;... };

Slide Example struct Declaration struct Student { int studentID; string name; short yearInSchool; double gpa; }; structure tag structure members

Slide struct Declaration Notes Must have ; after closing } struct names commonly begin with uppercase letter Multiple fields of same type can be in comma- separated list: string name, address;

Slide Defining Variables struct declaration does not allocate memory or create variables It creates a new data type To define variables, use structure tag as type name: Student bill; studentID name yearInSchool gpa bill

Slide Accessing Structure Members Use the dot (.) operator to refer to members of struct variables: cin >> bill.studentID; getline(cin, bill.name); bill.gpa = 3.75; Member variables can be used in any manner appropriate for their data type

Slide 11- 8

Slide Program11-1 (Continued)

Slide

Slide Displaying a struct Variable To display the contents of a struct variable, must display each field separately, using the dot operator: cout << bill; // won’t work cout << bill.studentID << endl; cout << bill.name << endl; cout << bill.yearInSchool; cout << " " << bill.gpa;

Slide Comparing struct Variables Cannot compare struct variables directly: if (bill == william) // won’t work Instead, must compare on a field basis: if (bill.studentID == william.studentID)...

Slide Initializing a Structure struct variable can be initialized when defined: Student s = {11465, "Joan", 2, 3.75}; Can also be initialized member-by-member after definition: s.name = "Joan"; s.gpa = 3.75;

Slide More on Initializing a Structure May initialize only some members: Student bill = {14579}; If you leave a structure member uninitialized, you must leave all members that follow it uninitialized as well!! Cannot skip over members: Student s = {1234, "John",, 2.83}; // illegal Cannot initialize in the structure declaration, since this does not allocate memory

Slide Excerpts From Program 11-4

Slide Nested Structures A structure can contain another structure as a member: To include a structure within a structure (Nested Structures, p. 642), we follow the same rules for including any data type in a structure. For example, assume that a structure is to consist of a name and a date of birth, where a Date structure, has been declared as struct Date { int month; int day; int year; }; A suitable definition of a structure that includes a name and a Date structure is struct Person { string name; Date birth; };

struct Date { int month; int day; int year; }; struct Person { string name; Date birth; }; int main ( void ) { Person employee; employee.name = "Mary Hill"; employee.birth.month = 12; employee.birth.day = 25; employee.birth.year = 2002; cout << employee.name << " birthdate is: “ << employee.birth.month << "/" << employee.birth.day << "/“ << employee.birth.year; return 0; }

Slide Arrays of Structures Structures can be defined in arrays Can be used in place of parallel arrays const int NUM_STUDENTS = 20; Student stuList[NUM_STUDENTS]; Individual structures accessible using subscript notation Fields within structures accessible using dot notation: cout << stuList[5].studentID;

Slide

Slide

Slide

Slide Structures as Function Arguments Individual structure members may be passed to a function in the same manner as any variable. May pass members of struct variables to functions: computeGPA(stu.gpa); May pass entire struct variables to functions: showData(stu); Can use reference parameter if function needs to modify contents of structure variable

struct Employee // declare a global type { int id_number; double pay_rate; double hours; }; double calcNet(double, double); // function prototype int main ( void ) { Employee emp = {6782, 8.93, 40.5}; double net_pay; net_pay = calcNet(emp.pay_rate, emp.hours); //PASS BY VALUE cout << setw(10) << fixed << showpoint << setprecision(2); cout << "The net pay for employee " << emp.id_number << " is $" << net_pay << endl; return 0; } double calcNet(double pay_rate, double hours) { return (pay_rate * hours); } //PASS BY VALUE //Output: The net pay for employee 6782 is $ This example passes copies of the values stored in structure members emp.pay_rate and emp.hours to the function named calcNet( ).

struct Employee // declare a global type { int id_number; double pay_rate; double hours; }; double calcNet(Employee); // function prototype int main ( void ) { Employee emp = {6782, 8.93, 40.5}; double net_pay; net_pay = calcNet(emp); cout << setw(10) << fixed <<showpoint << setprecision(2); cout << "The net pay for employee " << emp.id_number << " is $" << net_pay << endl; return 0; } double calcNet(Employee temp) {return (temp.pay_rate * temp.hours); } Output The net pay for employee 6782 is $361.66

Slide Returning a Structure from a Function Function can return a struct : Student getStudentData(); // prototype stu1 = getStudentData(); // call Function must define a local structure – for internal use – for use with return statement

Slide Returning a Structure from a Function - Example Student getStudentData() { Student tempStu; cin >> tempStu.studentID; getline(cin, tempStu.pData.name); getline(cin, tempStu.pData.address); getline(cin, tempStu.pData.city); cin >> tempStu.yearInSchool; cin >> tempStu.gpa; return tempStu; }

Slide

Slide

Slide Program 11-8 (Continued)