Topic 4 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.
1 Lecture 19 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Correction of the Handout #include //Preprocessor using namespace std; int main (){ ………….. return 0; } A namespace is a named group of definitions. When.
Function tax and buy_one double tax (double price, double tr) { return price*tr; } double buy_one() { double p; cout > p;
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
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.
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.
Lecture 18: Structured Data Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4)
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.
1 DATA STRUCTURES: LISTS. 2 LISTS ARE USED TO WORK WITH A GROUP OF VALUES IN AN ORGANIZED MANNER. A SERIES OF MEMORY LOCATIONS CAN BE DIRECTLY REFERENCED.
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.
Chapter 10: Records (structs)
Cosc237/structures1 Structures aggregate data types record - single variable name for the whole collection composed of several variables - fields,BUT,
Module 4: Structures ITEI222 Advanced Programming.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
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.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
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.
First steps Jordi Cortadella Department of Computer Science.
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 © 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.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Copyright © 2012 Pearson Education, Inc. 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.
Structures (aka records, structs) Textbook Chapter 11 sections:
Structures - Part II aggregate operations arrays of type struct nested structures compared to classes.
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.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Struct Data Type in C++ What Are Structures?
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.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
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
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
A Sample Program #include using namespace std; int main(void) { cout
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
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.
Chapter 6 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.
Topic 6 Recursion.
CO1401 Program Design and Implementation
Programming fundamentals 2 Chapter 1:Array
Structures.
Struct Data Type in C++ What Are Structures?
Presentation transcript:

Topic 4 Data Structures Program Development and Design Using C++, Third Edition

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

Program Development and Design Using C++, Third Edition 3 Structures (Textbook Chapter 15) Historical holdovers from C Record: Used 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 Example A student record: John Smith S Name ID CPA Credits string float int

Program Development and Design Using C++, Third Edition 5 Another Structure Example A Time record: hour minute second int

Program Development and Design Using C++, Third Edition 6 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 7 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 Variable Data Type timeObject hour minute second

Program Development and Design Using C++, Third Edition 8 Accessing structure members Member access operators  Dot operator (. ) for structure members: timeObject.hour timeObject.minute timeObject.second

Program Development and Design Using C++, Third Edition 9 Accessing structure members (ctd’) Read value to member hour of timeObject : cin >> timeObject.hour; Assign value: timeObject.hour=11; Print member hour of timeObject : cout << timeObject.hour; minute second timeObject hour 11

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

Program Development and Design Using C++, Third Edition 11 Example Program: date #include using namespace std; struct date { char WeekDay[4] ; int DayOfMonth ; int Month; 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.Month; //Read the month from user. TodaysDate.Year=2010; //Assign the year. strcpy(TodaysDate.WeekDay,”Fri”); //NOTE!!Use of function strcpy } //to assign to a string DayOfMonth Month TodaysDate WeekDay Year Fri 2010 \0

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

Program Development and Design Using C++, Third Edition 13 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 ; } ; event JFK; DayOfMonth Month WeekDay Year description when JFK

Program Development and Design Using C++, Third Edition 14 Structures using structures when is of type date (a stucture already defined) and is a member of the event structure.  JFK.when is of type date  Acessing an element of the data structure requires a dot followed by a data field.  For example: JFK.when.Month 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 15 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.K. is shot”); Assigns the string J.F.K. is shot to the string decription. Output the string: cout<<JFK.description;

Program Development and Design Using C++, Third Edition 16 Example Program: JFK #include using namespace std; struct date { char WeekDay[4] ; int DayOfMonth ; int Month; int Year; }; struct event{ char description[20]; date when; }; void main() { event JFK; JFK.when.DayOfMonth = 22; JFK.when.Month = 11; JFK.when.Year = 1963; strcpy(JFK.description,"J.F.K. is shot"); DayOfMonth Month WeekDay Year description JFK when J. F. K. i s s ho t \0

Program Development and Design Using C++, Third Edition 17 //display info cout<<JFK.when.DayOfMonth<<"/"<<JFK.when.Month<<"/" <<JFK.when.Year<<":"; cout<< JFK.description<<endl; } //end main Example Program: JFK (continued)

Program Development and Design Using C++, Third Edition 18 Example Program: Accessing the data fields DayOfMonth Month WeekDay Year description JFK when JFK.when.Month

Program Development and Design Using C++, Third Edition 19 Arrays of Structures Create an array of 10 events: event HistoryList[10] ; DayOfMonth Month WeekDay Year DayOfMonth Month WeekDay Year DayOfMonth Month WeekDay Year ……. 019 HistoryList

Program Development and Design Using C++, Third Edition 20 Arrays of Structures 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 21 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 22 Payroll program #1 #include using namespace std; 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 23 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 24 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 25 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 26 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 27 #include using namespace std; 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 28 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 29 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 30 Exercises (Textbook-chapter 15): pg. 796: ex.1a-e, 2a-e. pg. 801: ex. 1, 5a,b.