Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Structures.

Similar presentations


Presentation on theme: "© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Structures."— Presentation transcript:

1 © Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Structures

2 © Janice Regan, CMPT 102, Sept. 2006 1 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

3 © Janice Regan, CMPT 102, Sept. 2006 2 Structures and composite members  Structures can include elements that are of aggregate data types  Consider a structure that includes a member that is an array or another structure.  structureIdentifier.memberIdentifier[i]  structureIdentifier1.structureIdentifier2.memberIdentifier  Structures can be members or elements of aggregate data types  Can have and array of structures. To reference a member of the i'th structure in the array StructureIdentifier[i].memberIdentifier

4 © Janice Regan, CMPT 102, Sept. 2006 3 Return to our 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

5 © Janice Regan, CMPT 102, Sept. 2006 4 Consider our example typedef struct { int measurementNum; int date; int numOfComponents; int duration; double temperature; int numFailures; } labMeasurement;

6 © Janice Regan, CMPT 102, Sept. 2006 5 Add a new structure to our example  Define a new structure experiment  experiment contains all data regarding the running of a series of load tests for a range of conditions and components  experiment includes  An integer indicating the number of labMeasurements in the experiment  An character array of length 80 containing a label explaining the purpose of the particular experiment  A floating point number indicating the percent of tested components that failed in each of the labMeasurements  A floating point number containing the measured temperature in degrees Fahrenheit (converted from the Celsius measurement in the labMeasurement itself)  A floating point number indicating the number of components that failed the load test in the recorded conditions

7 © Janice Regan, CMPT 102, Sept. 2006 6 struct experiment { int numMeasurements; char description[80]; double percentFailures[MAXEXP]; double tempScaled[MAXEXP]; labMeasurement measures[MAXEXP]; };

8 © Janice Regan, CMPT 102, Sept. 2006 7 New experiment structure  The experiment structure illustrates several ways of using composite variables as members of a structure.  The description variable containing a label describing the experiment is a character array.  The scaled temperature and percent of components that failed are arrays of double variables. The ith element of each of these arrays correspond to the ith labMeasurement that is part of the experiment  The array of labMeasurement structures measures illustrates that not only can we have arrays as members of a structure we can also have other structures or even arrays of those other structures

9 © Janice Regan, CMPT 102, Sept. 2006 8 Accessing elements (1)  Consider accessing individual elements of arrays that are members of a structure e1 of type experiment. struct experiment e1; /* values of members of e1 are set */ e1.percentFailures[k] = 24.3; outint = e1.tempScaled[k];

10 © Janice Regan, CMPT 102, Sept. 2006 9 Accessing elements (2)  Consider accessing members of individual elements of an array of structures of type experiment. Each element of the array of structures is a structure of type experiment. struct experiment e1; labMeasurement L1; /* The k th element of the array of structures is assigned to */ /* labMeasurement structure L1. Each member of L1 will then */ /* contain the same value as the corresponding member of */ /* experiment struture e1.measures[k] */ L1 = e1.measures[k]; celsiusTemp = e1.measure[k].temperature; /* statement above is equivalent to celsiusTemp = L1.temperature */


Download ppt "© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Structures."

Similar presentations


Ads by Google