CMPT 102 Introduction to Scientific Computer Programming Structures

![Recall: Composite Data Types ] Data Type includes S S S A collection of Recall: Composite Data Types ] Data Type includes S S S A collection of](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-2.jpg)
![Using Structures ] Define Structure ] Declare variables of type of the structure ] Using Structures ] Define Structure ] Declare variables of type of the structure ]](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-3.jpg)
![Defining Structures ] Define Structure (A new composite type) S Must tell the compiler Defining Structures ] Define Structure (A new composite type) S Must tell the compiler](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-4.jpg)
![Defining Structures: Example ] Define Structure lab. Measurement S lab. Measurement contains all data Defining Structures: Example ] Define Structure lab. Measurement S lab. Measurement contains all data](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-5.jpg)

![Review: what is scope? ] Local or function scope: S Variable exists and can Review: what is scope? ] Local or function scope: S Variable exists and can](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-7.jpg)
![Review: what is scope? ] Global scope: S Variable exists and can be used Review: what is scope? ] Global scope: S Variable exists and can be used](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-8.jpg)
![Defining Structures ] Defining a structure creates a data type that can be used Defining Structures ] Defining a structure creates a data type that can be used](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-9.jpg)
![Defining Structures ] Once a structure is defined the compiler can determine S How Defining Structures ] Once a structure is defined the compiler can determine S How](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-10.jpg)
![Declaring structure variables ] Defining a structure creates a new composite data type ] Declaring structure variables ] Defining a structure creates a new composite data type ]](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-11.jpg)
![Declaring structure variables ] Declare variable of type student. Record S lab. Measurement first. Declaring structure variables ] Declare variable of type student. Record S lab. Measurement first.](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-12.jpg)



![Memory for declared structures ] Memory is assigned when a structure is declared. ] Memory for declared structures ] Memory is assigned when a structure is declared. ]](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-16.jpg)
![Accessing Structure members (1) ] Just as we needed to access individual elements of Accessing Structure members (1) ] Just as we needed to access individual elements of](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-17.jpg)
![Accessing Structure members (2) ] We can have multiple instances of the same structure Accessing Structure members (2) ] We can have multiple instances of the same structure](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-18.jpg)
![Accessing Structure members (3) ] To refer to a member of a structure we Accessing Structure members (3) ] To refer to a member of a structure we](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-19.jpg)
![Sample Structure ] Recall the definition of structure lab. Measurement typedef strut { int Sample Structure ] Recall the definition of structure lab. Measurement typedef strut { int](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-20.jpg)
![Initialization of sample structures ] At time of declaration S Initialization of structures lab. Initialization of sample structures ] At time of declaration S Initialization of structures lab.](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-21.jpg)
![Using the Sample Structure ] Using elements of structure of type lab. Measurement first. Using the Sample Structure ] Using elements of structure of type lab. Measurement first.](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-22.jpg)
![Printing the Sample Structure ] Structures must be printed one element at at time. Printing the Sample Structure ] Structures must be printed one element at at time.](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-23.jpg)
![Structures as Function Arguments ] A structure can be passed like any simple data Structures as Function Arguments ] A structure can be passed like any simple data](https://slidetodoc.com/presentation_image_h2/1b075b8b0cd8fdfab522788f321c93db/image-24.jpg)



- Slides: 27
CMPT 102 Introduction to Scientific Computer Programming Structures 12/19/2021 © Janice Regan, CMPT 102, Sept. 2006 0
Recall: Composite Data Types ] Data Type includes S S S A collection of objects (can be numbers, characters …) A group of operations that can be performed on those objects (the result of the operation should also be a member of the collection) A way to represent object of the type in memory ] Basic data types (int, double, …) identify the type of a single variable which references a single location in memory. ] Composite data types identify a group of values located in successive memory locations. The group of values is referred to by a single identifier. S S If all the component values have the same basic type – use an array If component values have a variety of basic types – use a structure © Janice Regan, CMPT 102, Sept. 2006 1
Using Structures ] Define Structure ] Declare variables of type of the structure ] Use variables declared above ] Structures can be used as parameters and return values of functions © Janice Regan, CMPT 102, Sept. 2006 2
Defining Structures ] Define Structure (A new composite type) S Must tell the compiler what the structure is before it can be used, S In particular you must specify the following for your structure: l l l An identifier (name) A list of component variables or elements The basic types of each of the elements © Janice Regan, CMPT 102, Sept. 2006 3
Defining Structures: Example ] Define Structure lab. Measurement S lab. Measurement contains all data regarding the running of a load test on a sample group of components ] lab. Measurement includes An integer indicating the date the measurement was taken An integer indicating the number of components to be tested in the experiment S An integer containing the duration of the experiment is seconds S A floating point number containing the measured temperature in degrees Celsius S A floating point number indicating the number of components that failed the load test in the recorded conditions S S © Janice Regan, CMPT 102, Sept. 2006 4
Defining Structures: Example struct lab. Measurement { int measurement. Num; int date; int num. Of. Components; int duration; double temperature; int num. Failures; }; typdef struct { int measurement. Num; int date; int num. Of. Components; int duration; double temperature; int num. Failures; } lab. Measurement; ] ; at the end of the structure definition is required © Janice Regan, CMPT 102, Sept. 2006 5
Review: what is scope? ] Local or function scope: S Variable exists and can be used only within the function in which it is defined int my. Funct( double my. Param 1, in my. Param 2) { /* start of body of function my. Funct */ int my. Local. Variable; /* my. Local Variable has local or function scope*/ /* my. Local Variable exists and can be used only*/ /* within the body of function my. Funct */ /* end of body of function my. Funct */ } © Janice Regan, CMPT 102, Sept. 2006 6
Review: what is scope? ] Global scope: S Variable exists and can be used anywhere within the file in which it is defined int my. Local. Variable; /* my. Local Variable has global scope*/ /* my. Local Variable exists and can be used within the body of any function */ /* within the file in which it is defined */ Int main ( ) { /* body of function main */ } int my. Funct( double my. Param 1, in my. Param 2) { /* body of function my. Funct */ } © Janice Regan, CMPT 102, Sept. 2006 7
Defining Structures ] Defining a structure creates a data type that can be used in your program. A structure may be used only within the scope of it’s definition. ] Defining a Structure does not reserve memory for any variable with the new type ] You need to define structures and arrays. This provides flexibility to define custom designed composite variable types for your specific needs ] simple types like int and double are defined for you, they do not need this flexibility. © Janice Regan, CMPT 102, Sept. 2006 8
Defining Structures ] Once a structure is defined the compiler can determine S How much memory must be set aside when a structure of the new type is declared ] The programmer who defined the new structure type now knows S How to declare and use structures of the new type S How to refer to the component variables and the new structure data type © Janice Regan, CMPT 102, Sept. 2006 9
Declaring structure variables ] Defining a structure creates a new composite data type ] Within any functions in the scope of a structure definition you can declare variables of the newly defined composite type. ] If a variable of the new composite type is declared inside function myfunct then that variable has local scope inside myfunct ] You may declare any number of structures in any number of functions in the scope of the structure definition. Each will be allocated it own memory area © Janice Regan, CMPT 102, Sept. 2006 10
Declaring structure variables ] Declare variable of type student. Record S lab. Measurement first. Measurement; Or S struct lab. Measurement first. Measurement; ] This declaration creates an instance of a structure of type lab. Measurement, ] Enough memory is allocated to hold one structure of type lab. Measurement and is associated with identifier first. Measurement © Janice Regan, CMPT 102, Sept. 2006 11
Declaring Structures: Example struct lab. Measurement { int measurement. Num; int date; int num. Of. Components; int duration; double temperature; int num. Failures; }; int some. Function (void) { struct lab. Measurement first. Measurement; © Janice Regan, CMPT 102, Sept. 2006 12
Declaring Structures: Example typdef struct { int measurement. Num; int date; int num. Of. Components; int duration; double temperature; int num. Failures; } lab. Measurement; int some. Function (void) { lab. Measurement first. Measurement; © Janice Regan, CMPT 102, Sept. 2006 13
Scope for declared structures int some. Function (void) { lab. Measurement first. Measurement; } int some. Other. Function (void) { lab. Measurement old. Measurement, first. Measurement; } ] ] old. Measurement only exists and is available to be used within some. Other. Function One variable first. Measurement exists only in some. Function and can be used only within some. Function. Another different variable first. Measurement exists only in some. Other. Function and can be used only in some. Other. Function. These are two different variables that just happen to have the same name. © Janice Regan, CMPT 102, Sept. 2006 14
Memory for declared structures ] Memory is assigned when a structure is declared. ] Memory for each element in the structure follows the memory for the previously defined element. measurement. Num date num. Of. Components duration temperature num. Failures © Janice Regan, CMPT 102, Sept. 2006 15
Accessing Structure members (1) ] Just as we needed to access individual elements of an array we need to be able to access individual members in a structure ] Members of a structure have different types and are associated with variables that have specific meanings. ] Each member of the structure has its own identifier. We wish to use these identifiers to access the members of the structure. © Janice Regan, CMPT 102, Sept. 2006 16
Accessing Structure members (2) ] We can have multiple instances of the same structure type. S Using only the names of the members does not distinguish between the members in different instances of the structure. ] We can have different types of structure that contain members with the same identifier S Using only the names of the members does not distinguish between the members of the different types of structures or ] A member of a structure may have the same identifier as a variable within the same scope S Using only the names of the members down not distinguish between the member of a structure and a simple variable with the same identifier © Janice Regan, CMPT 102, Sept. 2006 17
Accessing Structure members (3) ] To refer to a member of a structure we need to refer to the instance of the structure and the identifier of the member. ] In C the member of a structure is accessed using a 'dot' notation S The identifier of the instance of the structure is followed by a. then by the name of the member to be accessed l structure. Identifer. member. Identifier © Janice Regan, CMPT 102, Sept. 2006 18
Sample Structure ] Recall the definition of structure lab. Measurement typedef strut { int measurement. Num; int date; int num. Of. Components; int duration; double temperature; int num. Failures; } lab. Measurement; © Janice Regan, CMPT 102, Sept. 2006 19
Initialization of sample structures ] At time of declaration S Initialization of structures lab. Measurement first. Result = { 1, 120303, 3, 300, 50. 3, 0. 2}; S Partial initialization of structures (other numerical values initialized to 0 or null) lab. Measurement second. Result = {. date=120303, . duration=1000}; ] Initialized after declarations are complete first. Result. measurement. Num = 1; first. Result. date = 120303; first. Result. num. Of. Components= 3; first. Result. duration = 300; first. Result. temperature = 50. 3; first. Result. num. Failures = 0. 2; © Janice Regan, CMPT 102, Sept. 2006 20
Using the Sample Structure ] Using elements of structure of type lab. Measurement first. Result. num. Of. Components = firstnum. Of. Components; S Will place the value of the integer variable firstnum. Of. Components in the third element of the structure first. Result second. Duration = second. Result. duration; S Will place the value of the structure member duration in lab. Measurement structure second. Result (1000) into the variable second. Duration second. Result = first. Result; S Copy the values of all members in first. Result into the corresponding members in structure second. Result © Janice Regan, CMPT 102, Sept. 2006 21
Printing the Sample Structure ] Structures must be printed one element at at time. ] There is no mechanism in C to print a structure using a single print conversion ] It is usual to write a function to read or to write the members of a structure one by one, so that this does not have to be done repeatedly within your code © Janice Regan, CMPT 102, Sept. 2006 22
Structures as Function Arguments ] A structure can be passed like any simple data type (whole structure or any element) S Pass-by-value S Pass-by-reference S Or combination ] Can also be returned by function S Return-type is the structure type S Return statement in function definition sends structure variable back to caller © Janice Regan, CMPT 102, Sept. 2006 23
Passing by value: example void print. Lab. Measurement(lab. Measurement measure) { printf("Number of measurements: %dn”, measurement. Num); printf("Date MM/DD/YYYY: %dn", measure. date); printf("Instrument number: %dn", measure. num. Of. Components); printf("Duration of experiment: %dn", measure. duration); printf("Temperature for test: %fn", measure. temperature); printf("Number (of 100) of failed components: %dnn", measure. num. Failures); } © Janice Regan, CMPT 102, Sept. 2006 24
Passing by reference: example int compare. Lab. Measurements(lab. Measurement *m 1, lab. Measurement *m 2) { if(m 1 ->measurement. Num != m 2 ->measurement. Num ) { return 1; } if(m 1 ->date != m 2 ->date ) { return 1; } if(m 1 ->num. Of. Components != m 2 ->num. Of. Components ) { return 1; } if(m 1 ->duration != m 2 ->duration ) { return 1; } if( (m 1 ->temperature - m 2 ->temperature ) > DBL_EPSILON ) { return 1; } if(m 1 ->num. Failures != m 2 ->num. Failures ) { return 1; } return 0; } © Janice Regan, CMPT 102, Sept. 2006 25
Returning a structure: example lab. Measurement read. Lab. Measurement(FILE *filep) { lab. Measurement measure; fscanf(filep, "%d", &measurement. Num); fscanf(filep, "%d", &measure. date); fscanf(filep, "%d", &measure. num. Of. Components); fscanf(filep, "%d", &measure. duration); fscanf(filep, "%lf", &measure. temperature); fscanf(filep, "%d", &measure. num. Failures); return measure; } © Janice Regan, CMPT 102, Sept. 2006 26