Programmer Defined Structures (Records)

Slides:



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

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.
Structure.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Data Structures Lecture 3: Struct Azhar Maqsood NUST Institute of Information Technology (NIIT)
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
1 Structures. 2 C gives you several ways to create a custom data type. –The structure, which is a grouping of variables under one name and is called an.
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified by use by the MSU CMPS Dept. Chapter 10:
Lecture 18: Structured Data Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
1 Principles of Programming I Note Set #12. 2 Semester Overview Functions Character File I/O Arrays Pointers and Dynamic Memory Allocation Characters.
Structured Data Types array array union union struct struct class class.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
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,
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
CSCI-383 Object-Oriented Programming & Design Lecture 5.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Lecture 19 Structs HW 5 has been posted. 2 C++ structs l Syntax:Example: l Think of a struct as a way to combine heterogeneous data values together.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
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.
C++ Classes and Data Structures Jeffrey S. Childs
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.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
Structures (aka records, structs) Textbook Chapter 11 sections:
+ 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.
CPS120: Introduction to Computer Science Lecture 15A Structures.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
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.
Struct Data Type in C++ What Are Structures?
1 CS161 Introduction to Computer Science Topic #15.
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.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Programming 2. Arrays & structure Arrays : allow you to define variables that combine several data items of the same kind. Structure : is another user.
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.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
1 C++ Classes and Data Structures Course link…..
11 Chapter Structured Data
Chapter 11: Structured Data.
Data Types – Structures
Student Data Score First Name Last Name ID GPA DOB Phone ...
Data Types – Structures
Chapter 11: Structured Data.
CS148 Introduction to Programming II
Structures.
CPS120: Introduction to Computer Science
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

Programmer Defined Structures (Records) CS362

C++ Structures A Structure (Record) Contains Data About a Single Entity: Each Data Item is Called a Member (or Field) Members Can Be Of Different Data Types Member Data Types Can Be Basic or Complex Basic – int, float, double, string Complex – array, other structures In The End, Any Valid Data Type Can Used to Define a Member in a Structure

C++ Structures Data About a Student May Consist of: Last Name string First Name string Middle Initial char Id Number int Sex char GPA float Putting This All Together We Can Define A Structure

Defining A Structure Structures Require a Two Step Definition Process: First – Define a struct Data Type That Defines the Members of the Required Structure Second – Use the struct to Declare Variables Needed for the Program struct Data Type Definition: struct struct-name { type member1-name; type member2-name; : type memberN-name; };

Defining A Structure Definition Rules: The Structure Definition Must Begin With Reserved Word – struct Each Member Must Have a Type (the Type Can Be Different Then Other Members) Semicolons Separate the Structure Members Structure Definition Must End With };

Defining A Structure Variable The struct Definition Only Defines a Data Type To Use the Structure, Memory Must be Allocated Via Variable Declaration: struct-type variable_identifier; Where struct-type is the Structure Type Declared Where variable identifier is a Valid Identifier Name

Initializing A Structure Variable Like Other Variables Structures Can be Initialized at Declaration: struct-type variable_identifier = {member1-value, member2-value, ….memberN-value};

Structure Example struct studentRec { int idNumber; string lname; string fname; char initial; char sex; double gpa; };

Structure Variable Initialized studentRec stu1; studentRec stu1 = {123456,”Sanders”,”Mark”,’S’,’M’,3.50}; idNumber lname fname initial sex Gpa

Accessing Structure Data A Structure is a Single Data Unit Members Can Be Accessed Individually To Access a Member Use The DOT notation: struct variable_name.member_name studentRec.lname = “Sanders”; Members Can Be Used Like Any Other Variable

Accessing Structure Data (cont.) More Examples cout << “Enter student GPA: “; cin >> stu1.gpa; stu1.gpa = stu1.gpa + 0.5; cout << fixed << showpoint << setprecision(2); cout << stu1.lname << “, “ << stu1.fname << “ “ <<; cout << stu1.gpa << endl;

Using Structure Data Once You Have Data in the Structure, it Can Be Used Like Any Other Variable: if (stu1.sex == ‘F’) { cout << stu1.fname << ‘ ’ << stu1.lname << “ is Female.”; cout << endl; } if (stu1.gpa < 2.5) cout << stu1.lname << “, ” << stu1.fname; cout << “ is on probation.” << endl;

Structures As Arguments Structures Can Be Passed as Data to Functions If Data is to be Modified, Pass By Reference void readStuInfo (studentRec& stu1) { cout << “Enter student last name: “; cin >> stu1.lname; : cout << “Enter student GPA: “; cin >> stu1.gpa; }

Arrays of Structures Rather Than Using Parallel Arrays, we can use an Array of Structures Does Not Violate Single Data Type Rule Each Cell In The Array Will Hold One Structure struct-type array_name[size]; studentRec studentList[MAX]; Declares Array to Store MAX Quantity of Structures Each Cell Contains One Record With Six Members

Initializing Structure Array Same As In The Past, Only Thing New Is the Subscript studentRec studentList[MAX] = { {123456,”Sanders”, “Mark”, ‘S’, ‘M’, 3.50}, {234789, “Blow”, “Joe”, ‘J’,‘M’, 2.50}, {367890, “Smith”, “Sally”, ‘S’,‘F’, 4.00} }; All Remaining Cells Are Initialized to “blank” or 0

Accessing Structure Array Data Just Need To Use Subscript: arrayName[index].member_name studentList[1].idNumber = 234567; studentList[2].gpa = studentList[3].gpa; for (num = 0; num < MAX; num++) { cout << studentList[num].lname << “, ” <<; cout << studentList[num].fname << endl; }

Access Structure Array Data (cont.) Swapping Structures is like Swapping Any Variables: studentRec temp; temp = studentList[top]; studentList[top] = studentList[minPosition]; studentList[minPosition] = temp;

Nested Structure A Nested Structure is a Structure that has a Member that is a Structure Used to Organize Data, and to Allow for the Reuse of Variable Names To Declare a Nested Structure You Must First Declare the Structure(s) that will be Nested Once Declared the Structure can be used as Data Type for Members of Other Structures

Nested Structure (cont.) struct address { string street; string city; string state; string zip; }; struct student string lname; string fname; int age; double gpa; address campus; address home; student studentData;

Nested Structure (cont.) Accessing Nested Structure Need to Use Entire Name: studentData.lname studentData.fname studentData.age studentData.gpa studentData.campus.street studentData.campus.city studentData.campus.state studentData.campus.zip studentData.home.street studentData.home.city studentData.home.state studentData.home.zip

Nested Structure (cont.) Arrays of Nested Structures student studentList[MAX]; studentList[n].lname studentList[n].fname studentList[n].age studentList[n].gpa studentList[n].campus.street “ “ “ studentList[n].campus.zip studentList[n].home.street studentList[n].home.zip