C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 11: Records ( struct s)

Slides:



Advertisements
Similar presentations
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 10: Records ( structs )
Chapter 11: Records (structs)
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
An Introduction to Programming with C++ Fifth Edition
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 11: Records (structs)
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 11: Classes and Data Abstraction
Chapter 8 Arrays and Strings
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
EGR 2261 Unit 10 Records (structs)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 10: Records (structs)
Chapter 8 Arrays and Strings
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 10: Records ( struct s)
Chapter 10: Records (structs)
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Chapter 10: Records (structs)
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
Edited from Powerpoint Slides provided by Thomson Learning
Chapter 6: Arrays: Lists and Tables
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
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:
Chapter 3: Input/Output
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 11: Records ( struct s)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 12: Classes and Data Abstraction.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 11: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Chapter 7 User-Defined Methods.
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
REVISION CSC 138 – Structured Programming One Dimensional Array
Computer Programming BCT 1113
About the Presentations
Records C++ Structs Chapter 10.1.
User-Defined Functions
C++ Structs.
Records C++ Structs Chapter 12.
Chapter 9: Records (structs)
Chapter 9: Records (structs)
Chapter 10: Records (structs)
Chapter 9: Records (structs)
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Chapter 7: User-Defined Functions II
Chapter 6: User-Defined Functions I
Chapter 11: Records (structs)
Chapter 9: Records (structs)
Presentation transcript:

C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 11: Records ( struct s)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 Objectives In this chapter, you will: Learn about records ( struct s) Examine various operations on a struct Explore ways to manipulate data using a struct Learn about the relationship between a struct and functions Discover how arrays are used in a struct Learn how to create an array of struct items

C++ Programming: From Problem Analysis to Program Design, Fourth Edition3 Records ( struct s) struct : collection of a fixed number of components (members), accessed by name −Members may be of different types Syntax:

C++ Programming: From Problem Analysis to Program Design, Fourth Edition4 Records ( struct s) (continued) A struct is a definition, not a declaration

C++ Programming: From Problem Analysis to Program Design, Fourth Edition5 Records ( struct s) (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition6 Accessing struct Members The syntax for accessing a struct member is: The dot (. ) is an operator, called the member access operator

C++ Programming: From Problem Analysis to Program Design, Fourth Edition7 Accessing struct Members (continued) To initialize the members of newStudent : newStudent.GPA = 0.0; newStudent.firstName = "John"; newStudent.lastName = "Brown";

C++ Programming: From Problem Analysis to Program Design, Fourth Edition8 Accessing struct Members (continued) More examples: cin >> newStudent.firstName; cin >> newStudent.testScore >> newStudent.programmingScore; score = (newStudent.testScore + newStudent.programmingScore) / 2; if (score >= 90) newStudent.courseGrade = 'A'; else if (score >= 80) newStudent.courseGrade = 'B'; else if (score >= 70) newStudent.courseGrade = 'C'; else if (score >= 60) newStudent.courseGrade = 'D'; else newStudent.courseGrade = 'F';

C++ Programming: From Problem Analysis to Program Design, Fourth Edition9 Assignment Value of one struct variable can be assigned to another struct variable of the same type using an assignment statement The statement: student = newStudent; copies the contents of newStudent into student

C++ Programming: From Problem Analysis to Program Design, Fourth Edition10 Assignment (continued) The assignment statement: student = newStudent; is equivalent to the following statements: student.firstName = newStudent.firstName; student.lastName = newStudent.lastName; student.courseGrade = newStudent.courseGrade; student.testScore = newStudent.testScore; student.programmingScore = newStudent.programmingScore; student.GPA = newStudent.GPA;

C++ Programming: From Problem Analysis to Program Design, Fourth Edition11 Comparison (Relational Operators) Compare struct variables member-wise −No aggregate relational operations allowed To compare the values of student and newStudent :

C++ Programming: From Problem Analysis to Program Design, Fourth Edition12 Input/Output No aggregate input/output operations on a struct variable Data in a struct variable must be read one member at a time The contents of a struct variable must be written one member at a time

C++ Programming: From Problem Analysis to Program Design, Fourth Edition13 struct Variables and Functions A struct variable can be passed as a parameter by value or by reference A function can return a value of type struct

C++ Programming: From Problem Analysis to Program Design, Fourth Edition14 Arrays versus struct s

C++ Programming: From Problem Analysis to Program Design, Fourth Edition15 Arrays in struct s Two key items are associated with a list: −Values (elements) −Length of the list Define a struct containing both items:

C++ Programming: From Problem Analysis to Program Design, Fourth Edition16 Arrays in struct s (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition18 struct s in Arrays

C++ Programming: From Problem Analysis to Program Design, Fourth Edition21 struct s within a struct versus

C++ Programming: From Problem Analysis to Program Design, Fourth Edition23 Programming Example: Sales Data Analysis A company has six salespeople Every month they go on road trips to sell the company’s product At the end of each month, the total sales for each salesperson, salesperson’s ID, and the month are recorded in a file At the end of each year, the manager of the company asks for a report

C++ Programming: From Problem Analysis to Program Design, Fourth Edition24 Programming Example: Output Format Annual Sales Report ID QT1 QT2 QT3 QT4 Total ______________________________________________________________ Total Max Sale by SalesPerson: ID = 57373, Amount = $ Max Sale by Quarter: Quarter = 3, Amount = $ QT1 stands for quarter 1 (months 1 to 3), QT2 for quarter 2 (months 4 to 6), QT3 for quarter 3 (months 7 to 9), and QT4 for quarter 4 (months 10 to 12)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition25 Programming Example: Output Format (continued) The salespeople IDs are stored in one file; sales data are stored in another file The sales data is in the following form: salesPersonID month saleAmount. Sales data are not ordered

C++ Programming: From Problem Analysis to Program Design, Fourth Edition26 Programming Example: Input/Output Input: file containing each salesperson’s ID and a second file containing the sales data Output: file containing annual sales report in the above format

C++ Programming: From Problem Analysis to Program Design, Fourth Edition27 Programming Example: Problem Analysis Main components for each salesperson: −ID −Quarterly sales amount −Total annual sales amount Use a struct to group the components Six people: array of size six Program requires total sales for each quarter −Use array of size four to store the data

C++ Programming: From Problem Analysis to Program Design, Fourth Edition28 Programming Example: Problem Analysis (continued)

C++ Programming: From Problem Analysis to Program Design, Fourth Edition29 Programming Example: Program Analysis (continued) Read the salespeople IDs into the array salesPersonList Initialize the quarterly sales and total sales for each salesperson to 0

C++ Programming: From Problem Analysis to Program Design, Fourth Edition30 Programming Example: Program Analysis (continued) For each entry in the file with the sales data: −Read ID, month, sale amount for the month −Search salesPersonList to locate the component corresponding to this salesperson −Determine the quarter corresponding to the month −Update the sales for the quarter by adding the sale amount for the month

C++ Programming: From Problem Analysis to Program Design, Fourth Edition31 Programming Example: Program Analysis (continued) Once the sales data file is processed: −Calculate the total sale by salesperson −Calculate the total sale by quarter −Print the report

C++ Programming: From Problem Analysis to Program Design, Fourth Edition32 Programming Example: Algorithm Design Translates into the following algorithm: −Initialize the array salesPersonList −Process the sales data −Calculate the total sale by salesperson −Calculate the total sale by quarter −Print the report −Calculate and print maximum sale by salesperson −Calculate and print maximum sale by quarter

C++ Programming: From Problem Analysis to Program Design, Fourth Edition33 Programming Example: Main Algorithm Declare the variables Prompt user to enter name of file containing the salesperson’s ID data Read the name of the input file Open the input file If input file does not exist, exit Initialize the array salesPersonList by calling the function initialize

C++ Programming: From Problem Analysis to Program Design, Fourth Edition34 Programming Example: Main Algorithm (continued) Close input file containing salesperson’s ID Prompt user to enter name of file containing sales data Read the name of the input file Open the input file If input file does not exist, exit Prompt user to enter name of output file Read the name of the output file

C++ Programming: From Problem Analysis to Program Design, Fourth Edition35 Programming Example: Main Algorithm (continued) Open the output file Output data to two decimal places Process sales data −Call the function getData Calculate the total sale by quarter by calling the function saleByQuarter Calculate the total sale by salesperson by calling the function totalSaleByPerson

C++ Programming: From Problem Analysis to Program Design, Fourth Edition36 Programming Example: Main Algorithm (continued) Print the report in the tabular form; call the function printReport Find and print the salesperson who produces the maximum sales for the year by calling maxSaleByPerson Find and print the quarter producing the maximum sale for the year by calling maxSaleByQuarter Close files

C++ Programming: From Problem Analysis to Program Design, Fourth Edition37 Summary struct : collection of a fixed number of components Components can be of different types −Called members −Accessed by name struct is a reserved word No memory is allocated for a struct −Memory when variables are declared

C++ Programming: From Problem Analysis to Program Design, Fourth Edition38 Summary (continued) Dot (. ) operator: member access operator −Used to access members of a struct The only built-in operations on a struct are the assignment and member access Neither arithmetic nor relational operations are allowed on struct s struct can be passed by value or reference A function can return a value of type struct struct s can be members of other struct s