Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.

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

Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name;
Chapter 11 Structure. 2 Objectives You should be able to describe: Structures Arrays of Structures Structures as Function Arguments Dynamic Structure.
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.
PART I CHAPTER 16 CS116 SENEM KUMOVA METİN 1. Structures Structures : Aggregate data types built using elements of other types struct Time { int hour;
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 10: Records ( struct s)
CS1201: Programming Language 2 Structure By: Nouf Almunyif.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11. The Struct Data Type.
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
M.T.Stanhope Oct Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 1 C++ Basics u Data types. u Variables and Constants.
ARRAYS Lecture 2. 2 Arrays Hold Multiple values  Unlike regular variables, arrays can hold multiple values.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
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.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Structures (L30) u Syntax of a struct Declaration u Structure Variables u Accessing Members of Structures u Initialize Structure Variables u Array of Structures.
1 Structured Data (Lecture 11) By: Dr. Norazah Yusof FSKSM, UTM.
Struct Data Type in C++ What Are Structures?
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
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Structured Data.
LESSON 06.
Topic 6 Recursion.
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.
MT262A Review.
11 Chapter Structured Data
CO1401 Program Design and Implementation
CS1010 Programming Methodology
Structured Data (Lecture 07)
Chapter 2 Assignment and Interactive Input
Programming fundamentals 2 Chapter 1:Array
Arrays Part-1 Armen Keshishian.
Structures - Part II aggregate operations arrays of type struct
CSCE 210 Data Structures and Algorithms
C++ Arrays.
A solution to a list of records
Structures.
Student Data Score First Name Last Name ID GPA DOB Phone ...
Structures Lesson xx In this module, we’ll introduce you to structures.
One-Dimensional Array Introduction Lesson xx
Starting Out with C++: From Control Structures through Objects
Data type List Definition:
Review for Final Exam.
Strings A collection of characters taken as a set:
Programming Funamental slides
Chapter 11: Structured Data.
Lecture 12 Oct 16, 02.
Struct Data Type in C++ What Are Structures?
Chapter 10 Structures and Unions
Review for Final Exam.
Chapter 9: Data Structures: Arrays
Standard Version of Starting Out with C++, 4th Edition
More on Structs Sometimes we use structs even when the fields are all of the same type. If the fields are different conceptually, that is, the data stands.
Structure (i.e. struct) An structure creates a user defined data type
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Week 7 - Monday CS 121.
Presentation transcript:

Chapter 6 Data Structures Program Development and Design Using C++, Third Edition

2 Objectives You should be able to describe: Structures Arrays of Structures Problem solving

Program Development and Design Using C++, Third Edition 3 Structures (Textbook Chapter 15) Historical holdovers from C Record: Provides for storing information of varying types under one name. Several related items of information can be viewed as a single entity Data field: Individual data items in a record Structure: A record C++ permits methods to be included in a structure (class)

Program Development and Design Using C++, Third Edition 4 Structure Definitions Structures  Aggregate data types built using elements of other types struct Time { int hour; int minute; int second; }; Structure member naming  In same struct : must have unique names  In different struct s: can share name struct definition must end with semicolon Structure tag Structure members

Program Development and Design Using C++, Third Edition 5 Structure Definitions struct definition  Creates new data type used to declare variables  Structure variables declared like variables of other types  Examples: Time timeObject; declares timeObject to be a variable of type Time VariableData Type

Program Development and Design Using C++, Third Edition 6 Accessing structure members Member access operators  Dot operator (. ) for structure members Example:  Read value to member hour of timeObject : cin >> timeObject.hour; Or..  Assign value: timeObject.hour=11;  Print member hour of timeObject : cout << timeObject.hour;

Program Development and Design Using C++, Third Edition 7 Example Create a new data type called date : struct date { char WeekDay[3] ; int DayOfMonth ; int MonthOfYear ; int Year ; }; Declare a variable called TodaysDate of type date : date TodaysDate;

Program Development and Design Using C++, Third Edition 8 Example Program #include struct date { char WeekDay[4] ; int DayOfMonth ; int MonthOfYear ; int Year ; }; void main() { date TodaysDate; cout<<“Please enter the day of month:”; cin>>TodaysDate.DayOfMonth; //Read the day from user. cout<<“Please enter the month:”; cin>>TodaysDate.MonthOfYear; //Read the month from user. TodaysDate.Year=2008; //Assign the year. strcpy(TodaysDate.WeekDay,”Fri”); //Assign the weekday. NOTE!! //Use of function strcpy //to assign to a string

Program Development and Design Using C++, Third Edition 9 cout<<“The date is: ”; cout<<TodaysDate.WeekDay<<“, ”<<TodaysDate.DayOfMonth<<“/” <<TodaysDate.MonthOfYear<<“/”<<TodaysDate.Year<<endl; }//end main Example output following user input 28 (day) and 3 (month) The date is: Fri, 28/3/2008

Program Development and Design Using C++, Third Edition 10 Structures using structures Members of a structure can be any valid C++ data type, including arrays and structures. struct event { char description[20] ; date when ; // when is of type date, which is another structure //defined already } ; … event JFK; //declare variable of type event JFK.when.DayOfMonth = 22 ; JFK.when.MonthOfYear = 11 ; JFK.when.Year = 1963 ;

Program Development and Design Using C++, Third Edition 11 Structures using structures An array of characters is used as a member of the event structure. Accessing an element of a member array requires giving the structure’s name, followed by a period, followed by the array designation. For example: JFK.description[0] refers to the first character in the array called description which is a member of the structure event.

Program Development and Design Using C++, Third Edition 12 Structures using structures We can assign characters to a member array in the same way as with a normal string. strcpy(JFK.description,”J.F.Kennedy is shot”); Assigns the string J.F.Kennedy is shot to the string decription. Output the string: cout<<JFK.description;

Program Development and Design Using C++, Third Edition 13 Arrays of Structures Create an array of 10 events: event HistoryList[10] ; To print out the year associated with the 3rd. element of the sequence of events we could write a cout statement like this: cout << HistoryList[2].when.year ;

Program Development and Design Using C++, Third Edition 14 Example Program –Payroll #1 Create a data structure suitable for holding the information of an employee: name, ID number, address, phone, rate, hours worked in a week. Create an data structure suitable for holding the information of 10 employees. Calculate the gross pay of each employee and print it out.

Program Development and Design Using C++, Third Edition 15 Payroll program #1 #include const int NUMEMPS = 10; struct EmployeeDetails { char name[40]; char ID[10]; char address[50]; char phone[15]; double rate; double hours; };

Program Development and Design Using C++, Third Edition 16 void main() { EmployeeDetails employee[NUMEMPS]; int i; for(i = 0; i < NUMEMPS; i++) { cout << "Enter employee number: "; cin >> employee[i].ID; cin.ignore(); cout << "Enter employee name: "<<endl; cin.getline(employee[i].name,40); cout << "Enter employee phone number: "; cin >> employee[i].phone; cout << "Enter employee rate: "; cin >> employee[i].rate; cout << "Enter employee hours worked: "; cin >> employee[i].hours; cout<<endl<<endl; }

Program Development and Design Using C++, Third Edition 17 for( i = 0; i < NUMEMPS; i++) { cout<< "Employee number "<< employee[i].ID<<endl; cout<<"Gross Pay=" <<employee[i].rate*employee[i].hours<<endl; cout<<"*******************************"<<endl; }

Program Development and Design Using C++, Third Edition 18 Example Program –Payroll #2 Modify the previous program so that our data structure includes the grade of each employee (i.e. grade 0, 1 or 2), the hours worked for each day of the week. Calculate the weekly pay of each employee based on his/her grade (which corresponds to certain rate) and keep this in the data structure also. NOTE: Weekend rates are multiplied by an overtime premium rate of 1.5.

Program Development and Design Using C++, Third Edition 19 Example Grade:0, 1, 2 Rate: 4.25, 5.00, 5.50 corresponding to grade E.g. John Smith is grade 1 employee which corresponds to a rate of 5 Euro per hour. John has worked the following hours: Monday Tuesday Wednesday Thursday Friday Saturday Sunday His pay should be: 8* *5 + 7*5 + 9*5 + 6*5 + 3*5* *5*1.5 Saturday Sunday

Program Development and Design Using C++, Third Edition 20 #include const int NUMEMPS = 10; const double overtime=1.5; const double rate[3]={4.25, 5.00, 5.50}; struct EmployeeDetails { char name[40]; char ID[10]; char address[50]; char phone[15]; int grade; //0,1 or 2 double hours[7]; double pay; }; Example Program –Payroll #2

Program Development and Design Using C++, Third Edition 21 void main() { EmployeeDetails employee[NUMEMPS]; int i,j; for(i = 0; i < NUMEMPS; i++) { cout << "Enter employee number: "; cin >> employee[i].ID; //read rest of details... cout << "Enter employee grade (0,1 or 2): "; cin >> employee[i].grade; cout << "Enter employee hours worked for each day (day 0 is Monday, 1 is Tuesday...):"<<endl; for (j=0;j<7;j++) { cout<<"Day "<<j<<":"; cin>>employee[i].hours[j]; } employee[i].pay=0.0; cout<<endl<<endl; }

Program Development and Design Using C++, Third Edition 22 for( i = 0; i < NUMEMPS; i++) //calculate pay for each employee { for (j=0;j<5;j++) //pay for Mon-Fri inclusive employee[i].pay=employee[i].pay+ rate[employee[i].grade]*employee[i].hours[j]; for (j=5;j<7;j++) //pay for Sat & Sun employee[i].pay=employee[i].pay+ rate[employee[i].grade]*employee[i].hours[j]*overtime; } for( i = 0; i < NUMEMPS; i++) { cout<< "Employee number: "<< employee[i].ID<<endl; cout<<"Employee grade: "<<employee[i].grade<<endl; cout<<"Employee pay: "<<employee[i].pay<<endl; cout<<"*******************************"<<endl; }

Program Development and Design Using C++, Third Edition 23 Exercises (Textbook-chapter 15): pg. 796: ex.1a-e, 2a-e.