Chapter 11 – Structured Data

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

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.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
A function (procedure) code to perform a task (carry out an algorithm) Return_type Func(parameters) ---- Func (arguments) --- Return value.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
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.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Lecture 18: Structured Data Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 CS102 Introduction to Computer Programming Chapter 11 Structured Data.
1 Principles of Programming I Note Set #12. 2 Semester Overview Functions Character File I/O Arrays Pointers and Dynamic Memory Allocation Characters.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
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,
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
Starting Out with C++ 2nd Edition, by Tony Gaddis 1 Chapter 7 – Classes and Structured Data.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
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 © 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 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.
+ 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.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
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.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
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.
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.
Introduction to C++. Introducing C++ C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Structured Data.
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.
11 Chapter Structured Data
CO1401 Program Design and Implementation
Chapter 9: Pointers.
Chapter 7 – Arrays.
Structured Data (Lecture 07)
Data Types – Structures
Chapter 9: Pointers.
Enumeration used to make a program more readable
Data Types – Structures
Chapter 9: Pointers.
Starting Out with C++: From Control Structures through Objects
Chapter 11: Structured Data.
Standard Version of Starting Out with C++, 4th Edition
Structures Structured Data types Data abstraction structs ---
Presentation transcript:

Chapter 11 – 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 operations that may be performed on them.

Abstraction An abstraction is a general model of something.

Data Types C++ has several primitive data types: bool int unsigned long int char long int float unsigned char unsigned short int double short int unsigned int long double

What groups/types were there in high school? How did you recognize them? What they looked like What they did This is the concept of a data type!

Abstract Data Types A data type created by the programmer: The programmer decides what values are acceptable for the data type. The programmer decides what operations may be performed on the data type.

Combining Data into Structures C++ allows you to group several variables together into a single item known as a structure.

Payroll System Variable Declaration Information Held int empNumber; Employee number char name [25]; Employee name float hours; Hours worked float payRate; Hourly pay rate float grossPay; Gross pay

Payroll as a C++ struct struct PayRoll // NOTE capitalization of type names { int EmpNumber; char Name[25]; float Hours; float PayRate; float GrossPay; }; // PayRoll

PayRoll deptHead; Structure Variable Name Members deptHead empNumber hours payRate grossPay deptHead Members

PayRoll deptHead, foreman, associate; empNumber name hours payRate grossPay empNumber name hours payRate grossPay associate empNumber name hours payRate grossPay

At seats Discuss with your neighbor why you would want to group data into a structure.

Two Steps to Create a Structures Define the type: Create structure declaration. This establishes the tag (or name) of the structure and a list of items that are members. Declare variables of that type

Accessing Structure Members The dot operator (.) allows you to access structure members in a program.

Using Structures – Program struct PayRoll { int empNumber; // Employee number char name[25]; // Employee's name float hours; // Hours worked float payRate; // Hourly Payrate float grossPay; // Gross Pay }; // PayRoll

Using Structures – Program (cont) void main ( void ) { PayRoll employee; // Employee is a PayRoll structure cout << "Enter the employee's number: "; cin >> employee.empNumber; cout << "Enter the employee's name: "; cin.ignore(); // Skip the remaining '\n' character cin.getline(employee.name, 25); cout << "How many hours did the employee work? "; cin >> employee.hours; cout << "What is the employee's hourly payrate? "; cin >> employee.payRate; employee.grossPay = employee.hours * employee.payRate; cout << "Here is the employee's payroll data:\n"; cout << "Name: " << employee.name << endl; }

Displaying a Structure The contents of a structure variable cannot be displayed by passing the entire variable to cout. For example, assuming employee is a PayRoll structure variable, cout << employee; will not work: (We will learn later how to extend the system to do this.

Circle Structure – Program struct Circle { float radius; float diameter; float area; }; // Circle const float PI = 3.14159; At seats: use structs and method circleArea (which you write) to return area of give circle

Strings as Structure Members When a character array is a structure member, use the same string manipulation techniques with it as you would with any other character array. In other words, after qualification, it is just a simple type (which you know how to use).

Arrays of Structures Arrays of structures can simplify some programming tasks. struct BookInfo { char title[50]; char author[30]; char publisher[25]; float price; }; // BookInfo BookInfo bookList[20]; How would you refer to the first letter of the author in the third book? At seats: given an array of books, print out the title of all books costing more than $100.

Initializing a Structure Array struct PayInfo{ int jobId; float rate; }; PayInfo workers[5] = {{ 10, 9.75}, {15, 8.62}, {20, 10.50}, {40, 18.75}, {50, 15.65}};

Nested Structures struct Costs { float wholesale; float retail; struct Item { string partNum string description; Costs pricing; }; // Item

Questions Can we have arrays of structures? Can we have structures of arrays? Can we have structures of structures of structures of arrays of arrays of structures…. YES

Nested Structures – Program struct Date { int month; int day; int year; }; // Date struct Place string address; string city; string state; string zip; }; // Place

Nested Structures – Program (cont) struct EmpInfo { string name; int empNumber; Date birthDate; Place residence; }; // EmpInfo void main ( void ) { EmpInfo manager; cout << "Enter the manager's name: "; getline(cin,manager.name); cout << "Enter the manager's employee number: "; cin >> manager.empNumber;

Nested Structures – Program (cont) cout << "Now enter the manager's date-of-birth.\n"; cout << "Month (up to 2 digits): "; cin >> manager.birthDate.month; cout << "Day (up to 2 digits): "; cin >> manager.birthDate.day; cout << "Year (2 digits): "; cin >> manager.birthDate.year; cin.get(); // Eat the remaining newline character cout << "Enter the manager's street address: "; getline(cin,manager.residence.address); … }

Structures as Arguments – Program struct InvItem { int partNum; // Part number string description; // Item description int onHand; // Units on hand float price; // Unit price }; // InvItem // Function Prototypes void GetItem ( InvItem & ); // & Important or nothing will be changed void ShowItem ( InvItem );

Structures as Arguments – Program (cont) void main ( void ) { InvItem part; GetItem(part); ShowItem(part); } // main void GetItem ( InvItem &piece ) { … } // GetItem

Constant Reference Parameters Sometimes structures can be quite large. Therefore, passing by value can decrease a program’s performance. But passing by reference can cause problems. Instead, pass by constant reference: void ShowItem ( const InvItem &piece ) { cout.setf(ios::precision(2) | ios::fixed | ios::showpoint); cout << "Part Number: " << piece.partNum << endl; cout << "Price: $" << piece.price << endl; } // ShowItem

Returning a Structure – Program struct Circle { float radius; float diameter; float area; }; // Circle Circle getInfo ( void ); const float PI = 3.14159;

Returning a Structure – Program (cont) void main ( void ) { Circle c; c = getInfo(); c.area = PI * pow(c.radius, 2.0); cout << "The radius and area of the circle are:\n"; cout.precision(2); cout.setf(ios::fixed | ios::showpoint); cout << "Radius: " << c.radius << endl; cout << "Area: " << c.area << endl; } // main Circle getInfo ( void ) { Circle Round; cout << "Enter the diameter of a circle: "; cin >> Round.Diameter; Round.Radius = Round.Diameter / 2; return Round; } // getInfo

Pointers to Structures You may take the address of a structure variable and create variables that are pointers to structures. Circle *cirPtr; CirPtr = &piePlate; *cirPtr.Radius = 10; // incorrect (*cirPtr).Radius = 10; // correct cirPtr->Radius = 10; // easier notation

Pointers and Structures – Program (cont) void main ( void ) { PayRoll *employee; employee = new PayRoll; assert( employee == NULL );// Always good to do cout << "Enter the employee's number: "; cin >> employee->empNumber; cout << "Enter the employee's name: "; cin.ignore(); getline(cin,employee->name); … }

The Use of ., ->, and * Expression Description s->m *a.p (*s).m s is a structure pointer and m is a member. This expression accesses the m member of the structure pointer to by s. *a.p a is a structure variable and p, a pointer, is a member. This expression dereferences the value pointed to by p. (*s).m s is a structure pointer and m is a member. The * operator dereferences s, causing the expression to access the m member of the structure pointed to by s. This expression is the same as s->m. *s->p s is a structure pointer and p, a pointer, is a member of the structure pointed to by s. This expression accesses the value pointed to by p. *(*s).p

Unions A union is like a structure, except all the members occupy the same memory area (to save space). union PaySource { short hours; float sales; }; // PaySource PaySource employee; Fallen from favor as “unsafe” – cannot do type checking as can store as one type and use as another.

Unions – Program union PaySource { short hours; float sales; void main ( void ) { PaySource employee1; char payType; float payRate, grossPay;

Unions – Program (cont) if ( toupper(payType) == 'H' ) { cout << "What is the hourly pay rate? "; cin >> payRate; cout << "How many hours were worked? "; cin >> employee1.hours; GrossPay = employee1.hours * payRate; cout << "Gross pay: $" << grossPay << endl; } // if else if (toupper(payType) == 'C') { cin >> employee1.sales; grossPay = employee1.sales * 0.10; } // else if else cout << payType << " is not a valid selection.\n";