Structures. Data Types Structures. Data Types (C/C++) Gaddis: A Data Type consists of: Unit 7. example: Integer. CS 1428 Spring 2018

Similar documents
Ch 11. Structured Data (11.2 to 11.8) Data Types Structures. Data Types (C/C++) Data Type: CS 2308 Fall example: Integer.

Data Types (C/C++) Structures

Week 4. Pointers and Addresses. Dereferencing and initializing. Pointers as Function Parameters. Pointers & Structs. Gaddis: Chapters 9, 11

! The address operator (&) returns the address of a. ! Pointer: a variable that stores the address of another

Functions, Arrays & Structs

Arrays. Array Data Type. Array - Memory Layout. Array Terminology. Gaddis: 7.1-3,5

Arrays. Array Data Type. Array - Memory Layout. Array Terminology. Gaddis: 7.1-4,6

Functions, Arrays & Structs

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

Functions. 6.1 Modular Programming. 6.2 Defining and Calling Functions. Gaddis: 6.1-5,7-10,13,15-16 and 7.7

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

3.1 The cin Object. Expressions & I/O. Console Input. Example program using cin. Unit 2. Sections 2.14, , 5.1, CS 1428 Spring 2018

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Hardware Components Illustrated

Straight-line code (or IPO: Input-Process-Output) If/else & switch. Relational Expressions. Decisions. Sections 4.1-6, , 4.

l A program is a set of instructions that the l It must be translated l Variable: portion of memory that stores a value char

Tutorial 3 Concepts for A1

Searching, Sorting & Analysis

Professor: Alvin Chao

Week 3: File I/O and Formatting 3.7 Formatting Output

CS 1428 Review. CS 2308 :: Spring 2016 Molly O Neil

l Tree: set of nodes and directed edges l Parent: source node of directed edge l Child: terminal node of directed edge

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

Chapter 10 C Structures and Unions

CS 141, Introduction to Computer Science Fall Midterm Exam

PL/SQL, Embedded SQL. Lecture #14 Autumn, Fall, 2001, LRX

CSC 215 Structures. Dr. Achraf El Allali

I/O streams

Searching & Sorting. Definitions of Search and Sort. Other forms of Linear Search. Linear Search. Week 11. Gaddis: 8, 19.6,19.8.

11/28/11. Structures. structs. struct Definition. Chapter 11. structs store a collection of data elements of different data types

Searching & Sorting. Definitions of Search and Sort. Other forms of Linear Search. Linear Search. Week 11. Gaddis: 8, 19.6,19.8. CS 5301 Spring 2017

Professor: Alvin Chao

Chapter 2: Input, Processing, and Output

Pointer Variables. Pointers, Structures and Arrays. Schematics Example. Pointer Schematics

Structured Data. Abstraction Abstract Data Types TOPICS

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Outerjoins, Constraints, Triggers

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Structures. Lecture 15 COP 3014 Spring April 16, 2018

LECTURE 11 STRUCTURED DATA

CS 115 Midterm 2 Solutions

structs // variable declarations

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

C++ For Science and Engineering Lecture 12

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself.

ACS-3902 Fall Ron McFadyen 3D21 Slides are based on chapter 5 (7 th edition) (chapter 3 in 6 th edition)

Chapter 11: Structured Data

7 Arrays. 7.1 Arrays Hold Multiple Values TOPICS. CONCEPT: An array allows you to store and work with multiple values of the same data type.

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

understood as processors that match AST patterns of the source language and translate them into patterns in the target language.

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Exception with arguments

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

struct Definition Structure Declaration (7.1) Chapter 7 - Introduction to Classes and Objects

Chapter 11: Structured Data

Ch 6. Functions. Example: function calls function

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Tin học cơ sở 4$ Structures!

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Gaddis:

What is recursion? Recursion. How can a function call itself? Recursive message() modified. contains a reference to itself. Week 7. Gaddis:

Pointers. Variable Declaration. Chapter 10

Classes and Data Abstraction. Topic 5

Chapter 11: Abstract Data Types. Abstraction and Data Types. Combining Data into Structures 8/23/2014. Abstract Data Types

Chapter Two MULTIPLE CHOICE

Companion C++ Examples

This is a CLOSED-BOOK-CLOSED-NOTES exam consisting of five (5) questions. Write your answer in the answer booklet provided. 1. OO concepts (5 points)

Review Problems for Final Exam. 1. What is the output of the following program? #include <iostream> #include <string> using namespace std;


Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Objectives: Lab Exercise 1 Part 1. Sample Run. Part 2

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Ch 6. Structures and Classes

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Structs. Comp Sci 1570 Introduction to C++ Introduction. Aggregate data. Example. General syntax Object initialization Initialization and access

What is recursion? Recursion. Recursive message() modified. How can a function call itself? contains a reference to itself. Week 10. Gaddis:

Chapter 10 RECORDS (Structs )

Databases and PHP. Accessing databases from PHP

Classes and Data Abstraction: struct

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

Special Edition Using Microsoft Excel Selecting and Naming Cells and Ranges

Data Modeling with the Entity Relationship Model. CS157A Chris Pollett Sept. 7, 2005.

Professor: Alvin Chao

l A set is a collection of objects of the same l {6,9,11,-5} and {11,9,6,-5} are equivalent. l There is no first element, and no successor of 9.

C++ Structures Programming Workshop 2 (CSCI 1061U)

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Lab 15 Review of Arrays, Array of Objects and Vector Dr. John Abraham, Professor

Using Parallel Arrays. Parallel Array Example

Structures Unions and Enumerated Datatypes 224

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

BITG 1113: POINTER LECTURE 12

COMP322 - Introduction to C++

Data Types & Variables

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

Computer Programming

Ch 6-1. Structures. March 30, Prof. Young-Tak Kim

Transcription:

Structures Data Types A Data Type consists of: Unit 7 Gaddis: 11.2-8 set of vaues set of operations over those vaues CS 1428 Spring 2018 Ji Seaman exampe: Integer whoe numbers, -32768 to 32767 +, -, *, /, %, ==,!=, <, >, <=, >=,... Which operation is not vaid for foat? 1 2 Data Types (C/C++) 11.2 Structures Primitive Data Types atomic vaues, such as: Integers: Composite data type used to group mutipe variabes together into a unit. Exampe: student short, int, ong, char, boo ID Number Foating Points: Name foat, doube, ong doube Composite (or Aggregate) Types: vaues of these types are composed from other vaues. Arrays: sequence of vaues of the same type Age Major Each student has a vaue for each of these variabes (or attributes). Structures: named components of various types 3 4

Structures in C++ Define the student as a struct in C++: struct Student { int idnumber; int age; string major; NOTE: semicoon after ast cury bracket! A struct is a data type, and by convention the name is capitaized. The components are caed members (or fieds ). 5 Defining structure variabes So far we have defined a new data type, but we haven t defined any variabes of that type. To define a variabe of type Student: Student mystudent; Can define mutipe variabes of type Student: Student student1, student2, agradstudent; Each one has its own set of the member variabes in the Student data type 6 Defining structure variabes Each variabe of type Student has its own set of the member variabes from the Student data type student1 idnumber name age major Student student1, student2; student2 idnumber name age major 7 11.3 Accessing Structure Members Use dot operator to access members of a struct variabe: student1.age = 18; student2.idnumber = 123456; cin >> agradstudent.name; agradstudent.major = Rocket Science ; Member variabes of structures can be used just ike reguar variabes of the same type. student1.age++; //happy birthday myfunc(student2.idnumber); if (student1.age==student2.age) {... 8

Operations over structures: Vaid operations over entire structs: Outputting & comparing structure variabes Output the members one at a time: assignment: student1 = student2; function ca: myfunc(gradstudent,x); Invaid operations over entire structs: comparison: student1 == student2 cout << student1.idnumber << " "; cout << student1.name << " "; cout << student1.age << " "; cout << student1.major << end; Output: 11122 Chris Johnson 19 Chemistry output: cout << student1; input: cin >> student2; Comparing two structs: Must do these member by member! How is this different from Arrays? 9 if (student1.idnumber == student2.idnumber && student1.name == student2.name && student1.age == student2.age && student1.major == student2.major)... 10 11.4 Initiaizing a Structure Struct variabe can be initiaized when it is defined: Student student1 = {123456, John Smith,22, Math Must give vaues of members in order of the struct decaration. Can NOT initiaize members in structure decaration, ony variabe definition: struct StudentA { int id = 123456; //ILLEGAL string name = John Smith ; //ILLEGAL 11 struct EmpoyeePay { int empnum; doube payrate; doube hours; doube grosspay; // Empoyee name // Empoyee number // Houry pay rate // Hours worked // Gross pay int main() { EmpoyeePay empoyee1 = {"Betty Ross", 141, 18.75 EmpoyeePay empoyee2 = {"Ji Sandburg", 142, 17.50 cout << fixed << setprecision(2); // Cacuate pay for empoyee1 cout << "Name: " << empoyee1.name << end; cout << "Empoyee Number: " << empoyee1.empnum << end; cout << "Enter the hours worked by this empoyee: "; cin >> empoyee1.hours; empoyee1.grosspay = empoyee1.hours * empoyee1.payrate; cout << "Gross Pay: " << empoyee1.grosspay << end << end; // Cacuate pay for empoyee2 cout << "Name: " << empoyee2.name << end; cout << "Empoyee Number: " << empoyee2.empnum << end; cout << "Enter the hours worked by this empoyee: "; cin >> empoyee2.hours; empoyee2.grosspay = empoyee2.hours * empoyee2.payrate; cout << "Gross Pay: " << empoyee2.grosspay << end; Initiaizes ony name, empnum, and payrate 12

Sampe output from previous program: Name: Betty Ross Empoyee Number: 141 Enter the hours worked by this empoyee: 40 [Enter] Gross Pay: 750.00 Name: Ji Sandburg Empoyee Number: 142 Enter the hours worked by this empoyee: 20 [Enter] Gross Pay: 350.00 11.5 Arrays of Structures You can store vaues of structure types in arrays. Student roster[40]; //hods 40 Student structs Each student structure is accessibe via the subscript notation: roster[0] = student1; //copies student1 to first eem. Members of structure accessibe via dot operator cout << roster[0].name << end; 13 14 Arrays of Structures: initiaization To initiaize an array of structs: struct Student { int idnumber; int age; string major; int main() { Student roster[] = { {123456,"Ann Page",22,"Math", {111222,"Jack Spade",18,"Physics" Arrays of Structures Arrays of structures processed in oops: Student roster[40]; //input for (int i=0; i<40; i++) { cout << "Enter the name, age, idnumber and " << "major of the next student: \n"; cin >> roster[i].name >> roster[i].age >> roster[i].idnumber >> roster[i].major; //output a the id numbers and names for (int i=0; i<40; i++) { cout << roster[i].idnumber << end; cout << roster[i].name << end; 15 16

11.6 Nested Structures You can nest one structure inside another. struct Address { string street; string city; string state; int zip; struct Student { int idnumber; Address homeaddress; Nested Structures Use dot operator mutipe times to get into the nested structure: Student student1; student1.name = Bob Lambert ; student1.homeaddress.city = San Angeo ; student1.homeaddress.state = TX ; Or set up address structure separatey: Address a1; a1.street = 101 Main St. ; a1.city = San Angeo ; a1.state = TX ; a1.zip = 76903; 17 student1.name = Bob Lambert ; student1.homeaddress = a1; 18 11.7 Structures as function arguments Structure variabes may be passed as arguments to functions. void showstudent(student x) { cout << x.idnumber << end; cout << x.name << end; cout << x.age << end; cout << x.major << end; int main() { Student student1; //input information about student1 here Note: Student decaration must be goba!! Structures as function arguments By defaut, structure variabes are passed by vaue (ike most variabes). If the function needs to change the vaue of a member, the structure variabe shoud be passed by reference. void happybirthday(student &s) { s.age++; //or s.age = s.age+1; showstudent(student1); 19 20

11.8 Returning a Structure from a Function A function may return a structure. Student inputstudent(ifstream &fin) { Student resut; fin >> resut.idnumber; fin >> resut.name; fin >> resut.age; fin >> resut.major; return resut; int main() { ifstream infie; infie.open( students.dat ); Student student1 = inputstudent(infie); for (int i=0; i<40; i++) roster[i] = inputstudent(infie); infie.cose(); Note: aways pass iostreams by reference!! 21