CMPT 102 Introduction to Scientific Computer Programming Structures

  • Slides: 10
Download presentation
CMPT 102 Introduction to Scientific Computer Programming Structures 11/4/2020 © Janice Regan, CMPT 102,

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

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 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 1

Structures and composite members ] Structures can include elements that are of aggregate data

Structures and composite members ] Structures can include elements that are of aggregate data types S S S Consider a structure that includes a member that is an array or another structure. Identifier. member. Identifier[i] structure. Identifier 1. structure. Identifier 2. member. Identifier ] Structures can be members or elements of aggregate data types S Can have and array of structures. To reference a member of the i'th structure in the array l Structure. Identifier[i]. member. Identifier © Janice Regan, CMPT 102, Sept. 2006 2

Return to our Example ] Define Structure lab. Measurement ] lab. Measurement contains all

Return to our Example ] Define Structure lab. Measurement ] 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 3

Consider our example typedef struct { int measurement. Num; int date; int num. Of.

Consider our example typedef struct { int measurement. Num; int date; int num. Of. Components; int duration; double temperature; int num. Failures; } lab. Measurement; © Janice Regan, CMPT 102, Sept. 2006 4

Add a new structure to our example ] Define a new structure experiment S

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

struct experiment { int num. Measurements; char description[80]; double percent. Failures[MAXEXP]; double temp. Scaled[MAXEXP];

struct experiment { int num. Measurements; char description[80]; double percent. Failures[MAXEXP]; double temp. Scaled[MAXEXP]; lab. Measurement measures[MAXEXP]; }; © Janice Regan, CMPT 102, Sept. 2006 6

New experiment structure ] The experiment structure illustrates several ways of using composite variables

New experiment structure ] The experiment structure illustrates several ways of using composite variables as members of a structure. S The description variable containing a label describing the experiment is a character array. S 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 lab. Measurement that is part of the experiment S The array of lab. Measurement 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 © Janice Regan, CMPT 102, Sept. 2006 7

Accessing elements (1) ] Consider accessing individual elements of arrays that are members of

Accessing elements (1) ] Consider accessing individual elements of arrays that are members of a structure e 1 of type experiment. struct experiment e 1; /* values of members of e 1 are set */ e 1. percent. Failures[k] = 24. 3; outint = e 1. temp. Scaled[k]; © Janice Regan, CMPT 102, Sept. 2006 8

Accessing elements (2) ] Consider accessing members of individual elements of an array of

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 e 1; lab. Measurement L 1; /* The kth element of the array of structures is assigned to */ /* lab. Measurement structure L 1. Each member of L 1 will then */ /* contain the same value as the corresponding member of */ /* experiment struture e 1. measures[k] */ L 1 = e 1. measures[k]; celsius. Temp = e 1. measure[k]. temperature; /* statement above is equivalent to celsius. Temp = L 1. temperature */ © Janice Regan, CMPT 102, Sept. 2006 9