© Janice Regan, CMPT 128, Feb.. 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students Structures.

Slides:



Advertisements
Similar presentations
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Structure.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Structures.
© 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.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
CS Winter 2011 Further Introduction to the C programming language.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
© 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.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Basic Elements of C++ Chapter 2.
Review of C++ Programming Part II Sheng-Fang Huang.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
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:
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
CS 261 – Data Structures Introduction to C Programming.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
© 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.
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
1 CS161 Introduction to Computer Science Topic #15.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
1 Introduction to Object Oriented Programming Chapter 10.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Structures  Structure types  Pointers and structure types  Structures.
Operator Overloading Introduction
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++.
New Structure Recall “average.cpp” program
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Basic Elements of C++ Chapter 2.
Learning Objectives Structures Structure types
Chapter 3 Introduction to Classes, Objects Methods and Strings
6 Chapter Functions.
CS150 Introduction to Computer Science 1
Presentation transcript:

© Janice Regan, CMPT 128, Feb CMPT 128: Introduction to Computing Science for Engineering Students Structures

© Janice Regan, CMPT 128, February 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 128, February 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 128, February 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 128, February 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  An integer indicating the number of components that failed the load test in the recorded conditions

© Janice Regan, CMPT 128, February 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

© Janice Regan, CMPT 128, February 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 128, February Review: what is scope?  Global scope:  Variable exists and can be used anywhere within the file in which it is defined int myGlobalVariable; // myGlobalVariable has global scope // myGlobalVariable 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 128, February 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 128, February 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 must know  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 128, February 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

© Janice Regan, CMPT 128, February 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 128, February Declaring structure variables  Declare variable of type studentRecord  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 128, February Declaring Structures: Example struct labMeasurement { int measurementNum; int date; int numOfComponents; int duration; double temperature; int numFailures; } ; int someFunction (void) { labMeasurement firstMeasurement;

© Janice Regan, CMPT 128, February 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 128, February 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 128, February 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 128, February 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  A member of a structure may have the same identifier as a variable within the same scope  Using only the names of the members does not distinguish between the member of a structure and a simple variable with the same identifier

© Janice Regan, CMPT 128, February 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 128, February Sample Structure  Recall the definition of structure labMeasurement struct labMeasurement { int measurementNum; int date; int numOfComponents; int duration; double temperature; int numFailures; };

© Janice Regan, CMPT 128, February Sample: accessing members  Consider two instances of this structure declared within a particular function labMeasurement M1, M2;  To access the elements of the labMeasurement structures M1 and M2 M1.measurementNum OR M2.measurmentNum M1.date OR M2.dat M1.temperature OR M2.temperature etc.

© Janice Regan, CMPT 128, February Initialization of sample structures  At time of declaration  Initialization of structures labMeasurement firstResult = { 1, , 3, 300, 50.3, 0.2};  Partial initialization of structures labMeasurement firstResult = { 1, , 3}; // fills structure with // { 1, , 3, 0, 0.0, 0}

© Janice Regan, CMPT 128, February Initialization of sample structures  Initialized after declarations are complete firstResult.measurementNum = 1; firstResult.date = ; firstResult.numOfComponents= 3; firstResult.duration = 300; firstResult.temperature = 50.3; firstResult.numFailures = 2;

© Janice Regan, CMPT 128, February 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 128, February 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 statement  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 128, February Printing a structure void printLabMeasurement(labMeasurement measure) { cout << "Number of measurements: " << measure.measurementNum; cout << endl << "Date MM/DD/YYYY: " << measure.date;cout << endl << "Number of components tested: " << measure.numOfComponents; cout << endl << "Duration of experiment: " << measure.duration; cout << endl << "Temperature for test: " << measure.temperature; cout << endl << "Number (of MAXEXP) of failed components: " << measure.numFailures << endl << endl; }}

© Janice Regan, CMPT 128, February 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 (more later)  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 128, February Pass by value (C or C++) bool compareLabMeasurements(labMeasurement meas1, labMeasurement meas2) { if(meas1.measurementNum !=meas2.measurementNum ) { return false; } if(meas1.date != meas2.date ) {return false; } if(meas1.numOfComponents != meas2.numOfComponents ) { return false; } if(meas1.duration != meas2.duration ) {return false; } if(meas1.numFailures != meas2.numFailures ) {return false; } if(fabs(meas1.temperature - meas2.temperature) > DBL_EPSILON ) {return 1; } return true; }

© Janice Regan, CMPT 128, February Pass by reference (C++) bool compareLabMeasurements(labMeasurement &meas1, labMeasurement &meas2) { if(meas1.measurementNum !=meas2.measurementNum ) { return false; } if(meas1.date != meas2.date ) {return false; } if(meas1.numOfComponents != meas2.numOfComponents ) { return false; } if(meas1.duration != meas2.duration ) {return false; } if(meas1.numFailures != meas2.numFailures ) {return false; } if(fabs(meas1.temperature - meas2.temperature) > DBL_EPSILON ) {return 1; } return true; }

© Janice Regan, CMPT 128, February Pass using pointers (C) int compareLabMeasurements(labMeasurement *meas1, labMeasurement* meas2) { if(meas1->measurementNum != meas2 -> measurementNum ) { return1; } if(meas1->date != meas2->date ) {return 1; } if(meas1->numOfComponents != meas2->numOfComponents ) { return 1; } if(meas1->duration != meas2->duration ) {return 1; } if(meas1->numFailures != meas2->numFailures ) {return 1; } if(fabs(meas1->temperature - meas2->temperature) > DBL_EPSILON ) {return 1; } return 0; }

© Janice Regan, CMPT 128, February Returning a structure labMeasurement readLabMeasurement(ifstream& fileInput) { labMeasurement measure; fileInput >> measure.measurementNum; fileInput >> measure.date; fileInput >> measure.numOfComponents; fileInput >> measure.duration; fileInput >> measure.temperature; fileInput >> measure.numFailures; return measure; }