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

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
C Language.
Structure.
Programming Languages and Paradigms The C Programming Language.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Kernighan/Ritchie: Kelley/Pohl:
Lecture 2 Introduction to C Programming
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Functions Call by reference.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Road Map Introduction to object oriented programming. Classes
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
CS Winter 2011 Further Introduction to the C programming language.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Encapsulation by Subprograms and Type Definitions
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Structures.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Building User Libraries.
Review of C++ Programming Part II Sheng-Fang Huang.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
C Programming Tutorial – Part I CS Introduction to Operating Systems.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
1 Using Structures and Classes COSC 1557 C++ Programming Lecture 4.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
CS 261 – Data Structures Introduction to C Programming.
CSCI 171 Presentation 6 Functions and Variable Scope.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
CPS120: Introduction to Computer Science Lecture 15A Structures.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions Parameters passed by reference.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
CPS120: Introduction to Computer Science Data Structures.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
1 CS161 Introduction to Computer Science Topic #15.
© Janice Regan, CMPT 128, Feb CMPT 128: Introduction to Computing Science for Engineering Students Structures.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Structures  Structure types  Pointers and structure types  Structures.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Structures and Classes
Object Oriented Programming COP3330 / CGS5409
Learning Objectives Structures Structure types
Chapter 3 Introduction to Classes, Objects Methods and Strings
Java Programming Language
Defining Classes and Methods
Presentation transcript:

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

© Janice Regan, CMPT 102, Sept 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 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 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 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 Defining Structures: Example struct 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 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 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 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 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 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 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 Declaring Structures: Example struct labMeasurement { int measurementNum; int date; int numOfComponents; int duration; double temperature; int numFailures; } ; int someFunction (void) { struct labMeasurement firstMeasurement;

© Janice Regan, CMPT 102, Sept Declaring Structures: Example typdef struct { int measurementNum; int date; int numOfComponents; int duration; double temperature; int numFailures; } labMeasurement; int someFunction (void) { labMeasurement firstMeasurement;

© Janice Regan, CMPT 102, Sept Scope for declared structures int 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 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 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 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 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 Sample Structure  Recall the definition of structure labMeasurement typedef strut { int measurementNum; int date; int numOfComponents; int duration; double temperature; int numFailures; } labMeasurement;

© Janice Regan, CMPT 102, Sept Initialization of sample structures  At time of declaration  Initialization of structures labMeasurement firstResult = { 1, , 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 complete firstResult.measurementNum = 1; firstResult.date = ; firstResult.numOfComponents= 3; firstResult.duration = 300; firstResult.temperature = 50.3; firstResult.numFailures = 0.2;

© Janice Regan, CMPT 102, Sept 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 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 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 Passing by value: example void 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 Passing by reference: example int 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 Returning a structure: example labMeasurement 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; }