5/12/2012

Structured Programming Using C++ Lecture 12 : Data aggregation & Structures

Dr. Amal Khalifa

Lecture Contents: Structures



  

Structure types Structures as function arguments Initializing structures

2

Dr. Amal Khalifa - Spring 2012

Dr. Amal Khalifa, 2012

1

5/12/2012



aggregate data type : collection (grouping)of data  array: collection of values of same type  Structure: collection of values of different types

3

Dr. Amal Khalifa, 2012

Structure Types 

Must first "define" struct 





Prior to declaring any variables Define struct globally (typically)

No memory is allocated  

Just a "placeholder" for what our struct will "look like“ Treated as a single item

6-4

Dr. Amal Khalifa - Spring 2012

2

5/12/2012

Structure data type

5

Dr. Amal Khalifa, 2012

Structure data Types

Type definition

6

Dr. Amal Khalifa - Spring 2012

Variable declaration

Member access

Dr. Amal Khalifa, 2012

3

5/12/2012

Type definition : Syntax struct CDAccountV1 Name of new "type" { double balance;  member names double interestRate; int term; };

7

Dr. Amal Khalifa, 2012

Structure Variable declaration 

Variable declaration: CDAccountV1 account;   

Just like declaring simple types Variable account now of type CDAccountV1 It contains "member values" 

Each of the struct "parts"

8

Dr. Amal Khalifa - Spring 2012

Dr. Amal Khalifa, 2012

4

5/12/2012

Accessing Structure Members 

Dot Operator to access members   



account.balance account.interestRate account.term

Called "member variables"  

The "parts" of the structure variable Different structs can have same name member variables 

No conflicts

6-9

Example: Structure Definition (1 of 3)

6-10

Dr. Amal Khalifa - Spring 2012

5

5/12/2012

Example: A Structure Definition (2 of 3)

6-11

Example: A Structure Definition (3 of 3)

6-12

Dr. Amal Khalifa - Spring 2012

6

5/12/2012

Programming Pitfall 

Semicolon after structure definition 

; MUST exist: struct WeatherData { double temperature; double windVelocity; };  REQUIRED semicolon!



Required since you "can" declare structure variables in this location

6-13

Initializing Structures 

Can initialize at declaration 



Example: struct Date { int month; int day; int year; }; Date dueDate = {12, 31, 2003}; Declaration provides initial data to all three member variables

6-14

Dr. Amal Khalifa - Spring 2012

7

5/12/2012

Structure Assignments 

Given structure named Date



Declare two structure variables: Date d1, d2; 

Both are variables of "struct type Date"



Simple assignments are legal: d1= d2; 

Simply copies each member variable from d1 into member variables from d2

6-15

Structures as Function Arguments 

Passed like any simple data type   



Pass-by-value Pass-by-reference Or combination

Can also be returned by function  

Return-type is structure type Return statement in function definition sends structure variable back to caller

6-16

Dr. Amal Khalifa - Spring 2012

8

5/12/2012

.

Example: 2D point Structure

Define a C++ structure for a 2D point. Each point is described by an (x, y) coordinate. Define the needed functions to : Read a point coordinates Display point data Translate a point along x-axis or y-axis Compute the distance between two points using the following formula:

( x2  x1 )2  ( y2  y1 )2

17

Dr. Amal Khalifa, 2012

Array of structures 

Student Structure

struct student { char id_num[5]; char name[10]; char gender; int age; };

18

Dr. Amal Khalifa - Spring 2012

Dr. Amal Khalifa, 2012

9

5/12/2012

Example: Array of Students // an array structure of student information #include using namespace std; struct student { char id[6]; char name[50]; char gender; int age; };

// student id number, max. 5 digits // student name, max 49 characters // student gender Male or Female // student age

void main() { // declaring array of 10 element of structure type // and some of the element also are arrays student stud[10];

19

Dr. Amal Khalifa, 2012

Example: Array of Students int i = 0; cout<<"Keying in student data and then display\n"; cout<<"---------------------------------------\n"; cout<<"Enter student data\n"; for(i=0; i<2; i++) { // storing data of the first two students cout<<"\nID number (4 digit number) student #"<>stud[i].id; cout<<"First name student #"<>stud[i].name; cout<<"Gender (M or F) student #"<>stud[i].gender; cout<<"Age student #"<>stud[i].age; }

20

Dr. Amal Khalifa - Spring 2012

Dr. Amal Khalifa, 2012

10

5/12/2012

Example: Array of Students cout<<"\n----------Display the data---------\n"; cout<<"You can see that the data storage\n"; cout<<"has been reserved for the structure!\n"; cout<<"------------------------------------\n"; for(i=0; i<2; i++) { // displaying the stored data of the first two students cout<<"\nID number student # "<
21

Dr. Amal Khalifa, 2012

Structures & pointers  

Dynamic allocation of structure variables Example: student thatStudent, *studPointer; studPointer = new student; studPointer->age = 19; studPointer->gender = ‘M’; *(studPointer).age = 20;

22

Dr. Amal Khalifa - Spring 2012

Dr. Amal Khalifa, 2012

11

5/12/2012

Structures & pointers Dynamic array of structures  Example: student FBStudents[10], *studArr; studArr = new student[10]; studPointer[0].age = 19; studPointer[0].gender = ‘M’; studPointer[1].age = 21; studPointer[1].gender = ‘F’; 

23

Dr. Amal Khalifa, 2012

That’s all for today !! Thanks…..

24

Dr. Amal Khalifa - Spring 2012

Dr. Amal Khalifa, 2012

12

Lecture Contents

May 12, 2012 - 3. Structure data type. Dr. Amal Khalifa, 2012. 5. Structure data Types. Dr. Amal Khalifa, 2012. 6. Type definition. Variable declaration. Member access ... 7 struct CDAccountV1 ←Name of new "type". { double balance;. ← member names double interestRate; int term;. }; Structure Variable declaration.

527KB Sizes 4 Downloads 332 Views

Recommend Documents

Structured Programming Using C++ Lecture Contents
Recursive void Functions. ▻ Tracing recursive calls. ▻ Infinite recursion, overflows. ▻ Recursive Functions that Return a Value. ▻ Powers function. ▻ Thinking ...

Contents - multiresolutions.com
Notes on the History of Data Analysis . . . . . . . . . . . . . 3. 1.2.1. Biometry . . . . . . . . . . . . . . . . . . . . . . . . . 4. 1.2.2. Era Piscatoria . . . . . . . . . . . . . . . . . . . . . . . 4. 1.2.3. Psychometrics . . . . . . . . . . .

Contents - GitHub
May 9, 2006 - 3. 2 Requirements from SAGA Use Cases. 3. 2.1 Use Cases in Detail . ... A useful though informal way of understanding SAGA's aim and scope is to think of the MPI .... the design and implementation of specific API calls and maybe even th

Contents - GitHub
Dec 12, 2015 - 5.2.2 TIR-FCS with a square-shaped lateral detection volume . . . . . . . 30. 6 Troubleshooting. 32 ... 3http://www.gnu.org/licenses/gpl.html. 3 ..... This entry displays this documentation using the systems default PDF viewer. Wiki.

Contents - GitHub
Jan 7, 2015 - Foundation, either version 2 of the License, or (at your option) any later version. An exemplary usage of ... cython.org/src/tutorial/appendix.html.

Contents
Contents. Java. Chapter 4: Object-Oriented Analysis and Design ..... while I've been holed up in my office working on this book. I know they'd like to have ... than slapping down a few lines of code in Java (or C++, Eiffel, or any other object-orient

contents
Jun 2, 2008 - meeting. Fred Sewall made a motion to approve the minutes as ... changes down the road, the waiver should not necessarily go with it. Parker.

Contents -
A study on the use of online public access catalogue at the library of M.G.M College. DEVENDRA .... Device, Talking Typing Teacher Software, Braille Scanning Software, SARA, etc. The libraries should .... development of digital library initiatives in

contents
6 Nov 2014 - which is now in the form of a difference equation, suitable for coding in an appropriate programming language. This particular form of the PID algorithm is known as the 'positional' PID ... is initially positive. The integral component w

Contents
90. 4.4.4 Estimating by simulation: A logit-smoothed AR simulator 92. 4.5 Review and exercises . . . . . . . . . . . . . . . . . . . . . . . . . 97. 4.A Deriving the Multinomial Logit log-likelihood . . . . . . . . . . . 98. 5 Discrete Games. 100. 5.

Contents - Disability Rights California
Adult Day Health Services (ADHC). Annual Report 2010. 15. Administration. 100 Howe Ave.,. Suite 185-N. Sacramento, CA 95825. (916) 488-9955. Legal Offices. Sacramento. 100 Howe Ave.,. Suite 235-N. Sacramento, CA 95825. (916) 488-9950. Bay Area. 1330

Table of Contents - GitHub
random to receive a new welfare program called PROGRESA. The program gave money to poor families if their children went to school regularly and the family used preventive health care. More money was given if the children were in secondary school than

Table of Contents - Groups
It is intended for information purposes only, and may ... It is not a commitment to ... Levels of Security, Performance, and Availability. MySQL Enterprise. Audit ...

Contents - Beck-Shop
www.cambridge.org. © in this web service Cambridge University Press ... 3.2.2 Convex Distance Concentration and Rademacher Processes. 139. 3.2.3 A Lower ...

Contents
May 15, 2011 - describe recent E. cecorum-related lameness cases in Georgia, ... Page 2. Enterococcus cecorum is a normally innocuous inhabitant of the gastro-intestinal tract of various mam- ..... Broiler growers in the 19-State weekly program place

contents
(f) a ∧ b = 0 implies a ⊕ b = a ∨ b,. (g) a ∨ b = 1 implies a ⊙ b = a ∧ b. Proof. ... By Proposition 2.2.4 (a), it follows that 0 is the first element and 1 is the last element of A. In order to prove that l.u.b.{a, .... (d), (e) and (f)

contents
∗The Open University, ∗∗Lexical Computing Ltd. Abstract. In this paper ... be a “pernicious source of structural ambiguity in English” (Resnik 1999), they have ... the British National Corpus (bnc) (http://www.natcorp.ox.ac.uk). .... We use

Table of Contents
The Archaeological Evidence for the Jafnids and the Nas ̣rids. 172. Denis Genequand. 5. Arabs in the Conflict between Rome and Persia, AD 491–630. 214.

Contents - Ng Woon Lam
In the language of visual art, Color and Design are the most funda- mental qualities ... and today, its development and pace of practical use in the field of science ...

Table of Contents
Feb 24, 2012 - Commission for Africa (ECA) [South African. Mission]. E-mail: [email protected]. Mail: PO Box 1091, Addis Ababa, ETHIOPIA.

Contents - Ng Woon Lam
My practical experience and theoretical understanding with color issues helped me .... determine the speed of transition between the three pure colors. Fig.

Lecture 7
Nov 22, 2016 - Faculty of Computer and Information Sciences. Ain Shams University ... A into two subsequences A0 and A1 such that all the elements in A0 are ... In this example, once the list has been partitioned around the pivot, each sublist .....