© janice regan, cmpt 102, sept. 2006 0 cmpt 102 introduction to scientific computer programming...
Post on 18-Dec-2015
230 views
TRANSCRIPT
1 © Janice Regan, CMPT 102, Sept. 2006
CMPT 102Introduction to Scientific Computer Programming
Structures
© Janice Regan, CMPT 102, Sept. 2006 2
Recall: Composite Data Types Data Type includes
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. 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 3
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 4
Defining Structures
Define Structure (A new composite type) Must tell the compiler what the structure is
before it can be used, In particular you must specify the following for
your structure: An identifier (name) A list of component variables or elements The basic types of each of the elements
© Janice Regan, CMPT 102, Sept. 2006 5
Defining Structures: Example Define Structure labMeasurement
labMeasurement contains all data regarding the running of a load test on a sample group of components
labMeasurement includes An integer indicating the date the measurement was taken An integer indicating the number of components to be
tested in the experiment An integer containing the duration of the experiment is
seconds A floating point number containing the measured
temperature in degrees Celsius A floating point number indicating the number of
components that failed the load test in the recorded conditions
© Janice Regan, CMPT 102, Sept. 2006 6
Defining Structures: Examplestruct labMeasurement{
int measurementNum;int date;int numOfComponents;int duration;double temperature;int numFailures;
} ;
; at the end of the structure definition is required
typdef struct {
int measurementNum;
int date;
int numOfComponents;
int duration;
double temperature;
int numFailures;} labMeasurement;
© Janice Regan, CMPT 102, Sept. 2006 7
Review: what is scope? Local or function scope:
Variable exists and can be used only within the function in which it is defined
int myFunct( double myParam1, in myParam2){ /* start of body of function myFunct */
int myLocalVariable;/* myLocal Variable has local or function scope*//* myLocal Variable exists and can be used only*/
/* within the body of function myFunct */
/* end of body of function myFunct */}
© Janice Regan, CMPT 102, Sept. 2006 8
Review: what is scope? Global scope:
Variable exists and can be used anywhere within the file in which it is defined
int myLocalVariable;
/* myLocal Variable has global scope*/
/* myLocal 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 myFunct( double myParam1, in myParam2)
{
/* body of function myFunct */
}
© Janice Regan, CMPT 102, Sept. 2006 9
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 10
Defining Structures Once a structure is defined the compiler can
determine 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 How to declare and use structures of the new type How to refer to the component variables and the the
new structure data type
© Janice Regan, CMPT 102, Sept. 2006 11
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 12
Declaring structure variables Declare variable of type studentRecord
labMeasurement firstMeasurement;
Or struct labMeasurement firstMeasurement;
This declaration creates an instance of a structure of type labMeasurement,
Enough memory is allocated to hold one structure of type labMeasurement and is associated with identifier firstMeasurement
© Janice Regan, CMPT 102, Sept. 2006 13
Declaring Structures: Examplestruct labMeasurement{
int measurementNum;int date;int numOfComponents;int duration;double temperature;int numFailures;
} ;
int someFunction (void){struct labMeasurement firstMeasurement;
© Janice Regan, CMPT 102, Sept. 2006 14
Declaring Structures: Exampletypdef struct {
int measurementNum;
int date;
int numOfComponents;
int duration;
double temperature;
int numFailures;} labMeasurement;
int someFunction (void)
{
labMeasurement firstMeasurement;
© Janice Regan, CMPT 102, Sept. 2006 15
Scope for declared structuresint someFunction (void){ labMeasurement firstMeasurement;
} int someOtherFunction (void){ labMeasurement oldMeasurement, firstMeasurement;
} oldMeasurement only exists and is available to be used within
someOtherFunction One variable firstMeasurement exists only in someFunction and
can be used only within someFunction. Another different variable firstMeasurement exists only in someOtherFunction and can be used only in someOtherFunction. These are two different variables that just happen to have the same name.
© Janice Regan, CMPT 102, Sept. 2006 16
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.
measurementNum
date
numOfComponents
duration
temperature
numFailures
© Janice Regan, CMPT 102, Sept. 2006 17
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 18
Accessing Structure members (2) We can have multiple instances of the same structure type.
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 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 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 19
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 The identifier of the instance of the structure is
followed by a . then by the name of the member to be accessed
structureIdentifer.memberIdentifier
© Janice Regan, CMPT 102, Sept. 2006 20
Sample Structure Recall the definition of structure labMeasurementtypedef strut {
int measurementNum;
int date;
int numOfComponents;
int duration;
double temperature;
int numFailures;} labMeasurement;
© Janice Regan, CMPT 102, Sept. 2006 21
Initialization of sample structures At time of declaration
Initialization of structureslabMeasurement firstResult = { 1, 120303, 3, 300, 50.3, 0.2};
Partial initialization of structures (other numerical values initialized to 0 or null)
labMeasurement secondResult =
{.date=120303, .duration=1000};
Initialized after declarations are completefirstResult.measurementNum = 1; firstResult.date = 120303;
firstResult.numOfComponents= 3; firstResult.duration = 300;
firstResult.temperature = 50.3; firstResult.numFailures = 0.2;
© Janice Regan, CMPT 102, Sept. 2006 22
Using the Sample Structure Using elements of structure of type labMeasurement
firstResult.numOfComponents = firstnumOfComponents; Will place the value of the integer variable
firstnumOfComponents in the third element of the structure firstResult
secondDuration = secondResult.duration; Will place the value of the structure member duration in
labMeasurement structure secondResult (1000) into the variable secondDuration
secondResult = firstResult; Copy the values of all members in firstResult into the
corresponding members in structure secondResult
© Janice Regan, CMPT 102, Sept. 2006 23
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 24
Structures as Function Arguments A structure can be passed like any simple data
type (whole structure or any element) Pass-by-value Pass-by-reference Or combination
Can also be returned by function Return-type is the structure type Return statement in function definition
sends structure variable back to caller
© Janice Regan, CMPT 102, Sept. 2006 25
Passing by value: examplevoid printLabMeasurement(labMeasurement measure){ printf("Number of measurements: %d\n”,
measure.measurementNum); printf("Date MM/DD/YYYY: %d\n", measure.date); printf("Instrument number: %d\n",
measure.numOfComponents); printf("Duration of experiment: %d\n", measure.duration); printf("Temperature for test: %f\n", measure.temperature); printf("Number (of 100) of failed components: %d\n\n", measure.numFailures);}
© Janice Regan, CMPT 102, Sept. 2006 26
Passing by reference: exampleint compareLabMeasurements(labMeasurement *m1, labMeasurement *m2){ if(m1->measurementNum != m2->measurementNum ) { return 1; } if(m1->date != m2->date ) { return 1; } if(m1->numOfComponents != m2->numOfComponents ) { return 1; } if(m1->duration != m2->duration ) { return 1; } if( (m1->temperature - m2->temperature ) > DBL_EPSILON ) { return 1; } if(m1->numFailures != m2->numFailures ) { return 1; } return 0;}
© Janice Regan, CMPT 102, Sept. 2006 27
Returning a structure: examplelabMeasurement readLabMeasurement(FILE *filep){ labMeasurement measure;
fscanf(filep, "%d", &measure.measurementNum); fscanf(filep, "%d", &measure.date); fscanf(filep, "%d", &measure.numOfComponents); fscanf(filep, "%d", &measure.duration); fscanf(filep, "%lf", &measure.temperature); fscanf(filep, "%d", &measure.numFailures);
return measure;}