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

Size: px
Start display at page:

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

Transcription

1 Structures Data Types A Data Type consists of: Unit 7 Gaddis: set of vaues set of operations over those vaues CS 1428 Spring 2018 Ji Seaman exampe: Integer whoe numbers, to , -, *, /, %, ==,!=, <, >, <=, >=,... 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

2 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 Accessing Structure Members Use dot operator to access members of a struct variabe: student1.age = 18; student2.idnumber = ; 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

3 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: 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) 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 = ; //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, EmpoyeePay empoyee2 = {"Ji Sandburg", 142, 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

4 Sampe output from previous program: Name: Betty Ross Empoyee Number: 141 Enter the hours worked by this empoyee: 40 [Enter] Gross Pay: Name: Ji Sandburg Empoyee Number: 142 Enter the hours worked by this empoyee: 20 [Enter] Gross Pay: 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; 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

5 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; 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

6 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

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

Ch 11. Structured Data (11.2 to 11.8) Data Types Structures. Data Types (C/C++) Data Type: CS 2308 Fall example: Integer. Ch 11. Structured Data (11.2 to 11.8) Data Types Data Type: CS 2308 Fall 2012 Jill Seaman set of values set of operations over those values example: Integer whole numbers, -32768 to 32767 +, -, *, /, %,

More information

Data Types (C/C++) Structures

Data Types (C/C++) Structures Data Types (C/C++) Scalar (or Basic) Data Types (atomic values) o Arithmetic types Integers short, int, long char, bool Floating points float, double, long double Composite (or Aggregate) Types: o Arrays:

More information

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

Week 4. Pointers and Addresses. Dereferencing and initializing. Pointers as Function Parameters. Pointers & Structs. Gaddis: Chapters 9, 11 Week 4 Pointers & Structs Gaddis: Chapters 9, 11 CS 5301 Spring 2017 Ji Seaman 1 Pointers and Addresses The address operator (&) returns the address of a variabe. int x; cout

More information

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

! The address operator (&) returns the address of a. ! Pointer: a variable that stores the address of another Week 4 Pointers & Structs Gaddis: Chapters 9, 11 CS 5301 Spring 2015 Jill Seaman 1 Pointers and Addresses! The address operator (&) returns the address of a variable. int x; cout

More information

Functions, Arrays & Structs

Functions, Arrays & Structs Functions, Arrays & Structs Unit 1 Chapters 6-7, 11 Function Definitions l Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements... Where a parameter is: datatype identifier

More information

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-3,5 Arrays Unit 5 Gaddis: 7.1-3,5 CS 1428 Spring 2018 Ji Seaman Array Data Type Array: a variabe that contains mutipe vaues of the same type. Vaues are stored consecutivey in memory. An array variabe decaration

More information

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

Arrays. Array Data Type. Array - Memory Layout. Array Terminology. Gaddis: 7.1-4,6 Arrays Unit 5 Gaddis: 7.1-4,6 CS 1428 Fa 2017 Ji Seaman Array Data Type Array: a variabe that contains mutipe vaues of the same type. Vaues are stored consecutivey in memory. An array variabe definition

More information

Functions, Arrays & Structs

Functions, Arrays & Structs Functions, Arrays & Structs Unit 1 Chapters 6-7, 11 Function Definitions! Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements... Where a parameter is: datatype identifier

More information

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

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body Week 3 Pointers, References, Arrays & Structures Gaddis: Chapters 6, 7, 9, 11 CS 5301 Fall 2013 Jill Seaman 1 Arguments passed by value! Pass by value: when an argument is passed to a function, its value

More information

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

Functions. 6.1 Modular Programming. 6.2 Defining and Calling Functions. Gaddis: 6.1-5,7-10,13,15-16 and 7.7 Functions Unit 6 Gaddis: 6.1-5,7-10,13,15-16 and 7.7 CS 1428 Spring 2018 Ji Seaman 6.1 Moduar Programming Moduar programming: breaking a program up into smaer, manageabe components (modues) Function: a

More information

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

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program? Intro to Programming & C++ Unit 1 Sections 1.1-3 and 2.1-10, 2.12-13, 2.15-17 CS 1428 Spring 2018 Ji Seaman 1.1 Why Program? Computer programmabe machine designed to foow instructions Program a set of

More information

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

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 Expressions & I/O Unit 2 Sections 2.14, 3.1-10, 5.1, 5.11 CS 1428 Spring 2018 Ji Seaman 1 3.1 The cin Object cin: short for consoe input a stream object: represents the contents of the screen that are

More information

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

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Hardware Components Illustrated Intro to Programming & C++ Unit 1 Sections 1.1-3 and 2.1-10, 2.12-13, 2.15-17 CS 1428 Fa 2017 Ji Seaman 1.1 Why Program? Computer programmabe machine designed to foow instructions Program instructions

More information

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

Straight-line code (or IPO: Input-Process-Output) If/else & switch. Relational Expressions. Decisions. Sections 4.1-6, , 4. If/ese & switch Unit 3 Sections 4.1-6, 4.8-12, 4.14-15 CS 1428 Spring 2018 Ji Seaman Straight-ine code (or IPO: Input-Process-Output) So far a of our programs have foowed this basic format: Input some

More information

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

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 Week 1 Operators, Data Types & I/O Gaddis: Chapters 1, 2, 3 CS 5301 Fa 2018 Ji Seaman Programming A program is a set of instructions that the computer foows to perform a task It must be transated from

More information

Tutorial 3 Concepts for A1

Tutorial 3 Concepts for A1 CPSC 231 Introduction to Computer Science for Computer Science Majors I Tutoria 3 Concepts for A1 DANNY FISHER dgfisher@ucagary.ca September 23, 2014 Agenda script command more detais Submitting using

More information

Searching, Sorting & Analysis

Searching, Sorting & Analysis Searching, Sorting & Anaysis Unit 2 Chapter 8 CS 2308 Fa 2018 Ji Seaman 1 Definitions of Search and Sort Search: find a given item in an array, return the index of the item, or -1 if not found. Sort: rearrange

More information

Professor: Alvin Chao

Professor: Alvin Chao Professor: Avin Chao Anatomy of a Java Program: Comments Javadoc comments: /** * Appication that converts inches to centimeters. * * @author Chris Mayfied * @version 01/21/2014 */ Everything between /**

More information

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

Week 3: File I/O and Formatting 3.7 Formatting Output Week 3: File I/O and Formatting 3.7 Formatting Output Formatting: the way a value is printed: Gaddis: 3.7, 3.8, 5.11 CS 1428 Fall 2014 Jill Seaman spacing decimal points, fractional values, number of digits

More information

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

CS 1428 Review. CS 2308 :: Spring 2016 Molly O Neil CS 1428 Review CS 2308 :: Spring 2016 Molly O Neil Structure of a C++ Program Hello world // This program prints a greeting to the screen #include using namespace std; int main() { cout

More information

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

l Tree: set of nodes and directed edges l Parent: source node of directed edge l Child: terminal node of directed edge Trees & Heaps Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Fa 2016 Ji Seaman 1 Tree: non-recursive definition Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every node

More information

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

Introduction to the Stack. Stacks and Queues. Stack Operations. Stack illustrated. elements of the same type. Week 9. Gaddis: Chapter 18 Stacks and Queues Week 9 Gaddis: Chapter 18 CS 5301 Spring 2017 Ji Seaman Introduction to the Stack Stack: a data structure that hods a coection of eements of the same type. - The eements are accessed

More information

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

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL Many programs require complex data to be represented That cannot easily

More information

Chapter 10 C Structures and Unions

Chapter 10 C Structures and Unions Chapter 10 C Structures and Unions C How to Program, 8/e, GE 2016 Pearson Education, Ltd. All rights reserved. User-Defined Structure Types A database is a collection of information subdivided into records.

More information

CS 141, Introduction to Computer Science Fall Midterm Exam

CS 141, Introduction to Computer Science Fall Midterm Exam CS 141, Introduction to Computer Science Fall 2006 Midterm Exam Name: Student ID: 1 (12 points) Data Types Where possible give 3 examples of possible values for each of the following data types. Use proper

More information

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

PL/SQL, Embedded SQL. Lecture #14 Autumn, Fall, 2001, LRX PL/SQL, Embedded SQL Lecture #14 Autumn, 2001 Fa, 2001, LRX #14 PL/SQL,Embedded SQL HUST,Wuhan,China 402 PL/SQL Found ony in the Orace SQL processor (sqpus). A compromise between competey procedura programming

More information

CSC 215 Structures. Dr. Achraf El Allali

CSC 215 Structures. Dr. Achraf El Allali CSC 215 Structures Dr. Achraf El Allali Basic Structure A Structure is a collection of related data items, possibly of different types. A structure type in C is called struct. A struct can be composed

More information

I/O streams

I/O streams I/O streams 1-24-2013 I/O streams file I/O end-of-file eof reading in strings Browse the following libraries at cplusplus.com String Library STL: Standard Template Library (vector) IOStream Library HW#2

More information

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

Searching & Sorting. Definitions of Search and Sort. Other forms of Linear Search. Linear Search. Week 11. Gaddis: 8, 19.6,19.8. Searching & Sorting Week 11 Gaddis: 8, 19.6,19.8 CS 5301 Fa 2017 Ji Seaman 1 Definitions of Search and Sort Search: find a given item in a ist, return the position of the item, or -1 if not found. Sort:

More information

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

11/28/11. Structures. structs. struct Definition. Chapter 11. structs store a collection of data elements of different data types Structures Chapter 11 1 structs Arrays are useful for storing a collection of data elements of the same data type What about storing a collection of data elements of different data types? Related information

More information

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

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 Searching & Sorting Week 11 Gaddis: 8, 19.6,19.8 CS 5301 Spring 2017 Ji Seaman 1 Definitions of Search and Sort Search: find a given item in a ist, return the position of the item, or -1 if not found.

More information

Professor: Alvin Chao

Professor: Alvin Chao Professor: Avin Chao CS149 For Each and Reference Arrays Looping Over the Contents of an Array We often use a for oop to access each eement in an array: for (int i = 0; i < names.ength; i++) { System.out.printn("Heo

More information

Chapter 2: Input, Processing, and Output

Chapter 2: Input, Processing, and Output Chapter 2: Input, Processing, and Output Starting Out with Programming Logic & Design Second Edition by Tony Gaddis Copyright 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Topics

More information

Structured Data. Abstraction Abstract Data Types TOPICS

Structured Data. Abstraction Abstract Data Types TOPICS 11 Structured Data TOPICS 11.1 Abstract Data Types 11.2 Focus on Software Engineering: Combining Data into Structures 11.3 Accessing Structure Members 11.4 Initializing a Structure 11.5 Arrays of Structures

More information

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.

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. 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. B. Outputs to the console a floating point number f1 in scientific format

More information

Outerjoins, Constraints, Triggers

Outerjoins, Constraints, Triggers Outerjoins, Constraints, Triggers Lecture #13 Autumn, 2001 Fa, 2001, LRX #13 Outerjoins, Constraints, Triggers HUST,Wuhan,China 358 Outerjoin R S = R S with danging tupes padded with nus and incuded in

More information

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

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018 Week 3 Functions & Arrays Gaddis: Chapters 6 and 7 CS 5301 Spring 2018 Jill Seaman 1 Function Definitions l Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements...

More information

Structures. Lecture 15 COP 3014 Spring April 16, 2018

Structures. Lecture 15 COP 3014 Spring April 16, 2018 Structures Lecture 15 COP 3014 Spring 2018 April 16, 2018 Motivation We have plenty of simple types for storing single items like numbers, characters. But is this really enough for storing more complex

More information

LECTURE 11 STRUCTURED DATA

LECTURE 11 STRUCTURED DATA PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 11 STRUCTURED DATA

More information

CS 115 Midterm 2 Solutions

CS 115 Midterm 2 Solutions CS115Midterm2Solutions April9,2009 Rules Youmustbrieflyexplainyouranswerstoreceivepartialcredit. Whenasnippetofcodeisgiventoyou,youcanassumethatthecodeis enclosedwithinsomefunction,evenifnofunctiondefinitionisshown.you

More information

structs // variable declarations

structs // variable declarations 1 structs Arrays are useful for storing a collection of data elements of the same data type What about storing a collection of data elements of different data types? Related information can be placed in

More information

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

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. Week 3 Functions & Arrays Gaddis: Chapters 6 and 7 CS 5301 Fall 2015 Jill Seaman 1 Function Definitions! Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements... Where

More information

C++ For Science and Engineering Lecture 12

C++ For Science and Engineering Lecture 12 C++ For Science and Engineering Lecture 12 John Chrispell Tulane University Monday September 20, 2010 Comparing C-Style strings Note the following listing dosn t do what you probably think it does (assuming

More information

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

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Recursion What is recursion? Week 10 Generally, when something contains a reference to itself Gaddis:19.1-19.5 CS 5301 Spring 2014 Jill Seaman 1 Math: defining a function in terms of itself Computer science:

More information

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

ACS-3902 Fall Ron McFadyen 3D21 Slides are based on chapter 5 (7 th edition) (chapter 3 in 6 th edition) ACS-3902 Fall 2016 Ron McFadyen 3D21 ron.mcfadyen@acs.uwinnipeg.ca Slides are based on chapter 5 (7 th edition) (chapter 3 in 6 th edition) 1 The Relational Data Model and Relational Database Constraints

More information

Chapter 11: Structured Data

Chapter 11: Structured Data Chapter 11: Structured Data 11.1 Abstract Data Types (ADT's) Abstract Data Types A data type that specifies values that can be stored attributes operations that can be done on the values behaviors User

More information

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.

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. 7 Arrays TOPICS 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C++ 7.4 Array Initialization 7.5 Processing Array Contents 7.6 Focus on Software Engineering: Using

More information

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

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011 The American University in Cairo Department of Computer Science & Engineering CSCI 106-07&09 Dr. KHALIL Exam-I Fall 2011 Last Name :... ID:... First Name:... Form I Section No.: EXAMINATION INSTRUCTIONS

More information

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

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018. Week 2 Branching & Looping Gaddis: Chapters 4 & 5 CS 5301 Spring 2018 Jill Seaman 1 Relational Operators l relational operators (result is bool): == Equal to (do not use =)!= Not equal to > Greater than

More information

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

understood as processors that match AST patterns of the source language and translate them into patterns in the target language. A Basic Compier At a fundamenta eve compiers can be understood as processors that match AST patterns of the source anguage and transate them into patterns in the target anguage. Here we wi ook at a basic

More information

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

File Operations. Lecture 16 COP 3014 Spring April 18, 2018 File Operations Lecture 16 COP 3014 Spring 2018 April 18, 2018 Input/Ouput to and from files File input and file output is an essential in programming. Most software involves more than keyboard input and

More information

Exception with arguments

Exception with arguments Exception Handling Introduction : Fundamental Syntax for Exception Handling code : try catch throw Multiple exceptions Exception with arguments Introduction Exception: An abnormal condition that arises

More information

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

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1 BEng (Hons) Electronic Engineering Cohort: BEE/10B/FT Resit Examinations for 2016-2017 / Semester 1 MODULE: Programming for Engineers MODULE CODE: PROG1114 Duration: 3 Hours Instructions to Candidates:

More information

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

Review. Modules. CS 151 Review #6. Sample Program 6.1a: Review Modules A key element of structured (well organized and documented) programs is their modularity: the breaking of code into small units. These units, or modules, that do not return a value are called

More information

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

struct Definition Structure Declaration (7.1) Chapter 7 - Introduction to Classes and Objects Chapter 7 - Introduction to Classes and Objects In the previous chapter we finished with the concept of parallel arrays where there are two or more arrays and related information is found at a specific

More information

Chapter 11: Structured Data

Chapter 11: Structured Data Chapter 11: Structured Data 11.1 Abstract Data Types Abstract Data Types A data type that specifies values that can be stored operations that can be done on the values User of an abstract data type does

More information

Ch 6. Functions. Example: function calls function

Ch 6. Functions. Example: function calls function Ch 6. Functions Part 2 CS 1428 Fall 2011 Jill Seaman Lecture 21 1 Example: function calls function void deeper() { cout

More information

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

Downloaded S. from Kiran,  PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from Downloaded S. from Kiran, www.studiestoday.com PGT (CS) KV, STRUCTURES WHAT IS A STRUCTURE? Structure is a collection of logically related data. It is also a collection of dissimilar datatype. Downloaded

More information

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

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char Week 1 Operators, Data Types & I/O Gaddis: Chapters 1, 2, 3 CS 5301 Fall 2016 Jill Seaman Programming A program is a set of instructions that the computer follows to perform a task It must be translated

More information

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

Tin học cơ sở 4$ Structures! Tin học cơ sở 4$ Structures! Outline$ Structured Data types! Type definitions! Self-referential types: linked list! Structure$ Many structured collections of data are:! Heterogeneous (components are different

More information

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. Week 10. contains a reference to itself. Gaddis: Recursion What is recursion? Week 10! Generally, when something contains a reference to itself Gaddis:19.1-19.5! Math: defining a function in terms of itself CS 5301 Spring 2015 Jill Seaman 1! Computer

More information

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

What is recursion? Recursion. How can a function call itself? Recursive message() modified. contains a reference to itself. Week 7. Gaddis: Recursion What is recursion? Week 7! Generally, when something contains a reference to itself Gaddis:19.1-19.4! Math: defining a function in terms of itself CS 5301 Fall 2013 Jill Seaman 1! Computer science:

More information

Pointers. Variable Declaration. Chapter 10

Pointers. Variable Declaration. Chapter 10 Pointers Chapter 10 Variable Declaration When a variable is defined, three fundamental attributes are associated with it: Name Type Address The variable definition associates the name, the type, and the

More information

Classes and Data Abstraction. Topic 5

Classes and Data Abstraction. Topic 5 Classes and Data Abstraction Topic 5 Introduction Object-oriented programming (OOP) Encapsulates data (attributes) and functions (behavior) into packages called classes The data and functions of a class

More information

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

Chapter 11: Abstract Data Types. Abstraction and Data Types. Combining Data into Structures 8/23/2014. Abstract Data Types Chapter 11: Structured Data 11.1 Abstract Data Types Abstract Data Types Abstraction and Data Types A data type that specifies values that can be stored operations that can be done on the values User of

More information

Chapter Two MULTIPLE CHOICE

Chapter Two MULTIPLE CHOICE Chapter Two MULTIPLE CHOICE 1. In a C++ program, two slash marks ( // ) indicate: a. The end of a statement b. The beginning of a comment c. The end of the program d. The beginning of a block of code 2.

More information

Companion C++ Examples

Companion C++ Examples 68 Appendix D Companion C++ Examples D.1 Introduction It is necessary to be multilingual in computer languages today. Since C++ is often used in the OOP literature it should be useful to have C++ versions

More information

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)

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) COMP2012H Object Oriented Programming and Data Structures Spring Semester 2013 Midterm Exam Soution March 26, 2013, 10:30-11:50am in Room 3598 Instructor: Chi Keung Tang This is a CLOSED-OOK-CLOSED-NOTES

More information

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

Review Problems for Final Exam. 1. What is the output of the following program? #include <iostream> #include <string> using namespace std; Review Problems for Final Exam 1. What is the output of the following program? int draw(int n); int n = 4; while (n>0) n = draw(n); int draw(int n) for(int i = 0; i < n; i++) cout

More information

Consider the following statements. string str1 = "ABCDEFGHIJKLM"; string str2; After the statement str2 = str1.substr(1,4); executes, the value of str2 is " ". Given the function prototype: float test(int,

More information

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.

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. Week 2: Console I/O and Operators Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.1) CS 1428 Fall 2014 Jill Seaman 1 2.14 Arithmetic Operators An operator is a symbol that tells the computer to perform specific

More information

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

Objectives: Lab Exercise 1 Part 1. Sample Run. Part 2 Objectives: king Saud University College of Computer &Information Science CSC111 Lab Object I All Sections - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

More information

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

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 1 ARRAYS Arrays 2 Arrays Structures of related data items Static entity (same size

More information

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays Outline 13.1 Test-Driving the Salary Survey Application 13.2 Introducing Arrays 13.3 Declaring and Initializing Arrays 13.4 Constructing

More information

Ch 6. Structures and Classes

Ch 6. Structures and Classes 2013-2 Ch 6. Structures and Classes September 1, 2013 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497;

More information

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

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018 C++ Basics Lecture 2 COP 3014 Spring 2018 January 8, 2018 Structure of a C++ Program Sequence of statements, typically grouped into functions. function: a subprogram. a section of a program performing

More information

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

Structs. Comp Sci 1570 Introduction to C++ Introduction. Aggregate data. Example. General syntax Object initialization Initialization and access Structs Comp Sci 1570 to C++ Outline 1 2 3 The struct A structure is a group of elements grouped together under one name. These elements, known as members, can have different types and different lengths.

More information

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

What is recursion? Recursion. Recursive message() modified. How can a function call itself? contains a reference to itself. Week 10. Gaddis: Recursion What is recursion? Week 10 Gaddis:19.1-19.5 CS 5301 Spring 2017 Jill Seaman 1 l Generally, when something contains a reference to itself l Math: defining a function in terms of itself l Computer

More information

Chapter 10 RECORDS (Structs )

Chapter 10 RECORDS (Structs ) Chapter 10 RECORDS (Structs ) In Chapter 6 we learned that an array is set up to use multiple elements of the same data type. What if we want to have several different related types within one data structure

More information

Databases and PHP. Accessing databases from PHP

Databases and PHP. Accessing databases from PHP Databases and PHP Accessing databases from PHP PHP & Databases PHP can connect to virtuay any database There are specific functions buit-into PHP to connect with some DB There is aso generic ODBC functions

More information

Classes and Data Abstraction: struct

Classes and Data Abstraction: struct Classes and Data Abstraction: struct There are many instances in programming where we need more than one variable in order to represent an object. For example, to represent yourself, you might want to

More information

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

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4 The University of Alabama in Huntsville Electrical and Computer Engineering CPE 112 02 Example of Objective Test Questions for Test 4 True or False Name: 1. The statement switch (n) case 8 : alpha++; case

More information

Special Edition Using Microsoft Excel Selecting and Naming Cells and Ranges

Special Edition Using Microsoft Excel Selecting and Naming Cells and Ranges Specia Edition Using Microsoft Exce 2000 - Lesson 3 - Seecting and Naming Ces and.. Page 1 of 8 [Figures are not incuded in this sampe chapter] Specia Edition Using Microsoft Exce 2000-3 - Seecting and

More information

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

Data Modeling with the Entity Relationship Model. CS157A Chris Pollett Sept. 7, 2005. Data Modeling with the Entity Relationship Model CS157A Chris Pollett Sept. 7, 2005. Outline Conceptual Data Models and Database Design An Example Application Entity Types, Sets, Attributes and Keys Relationship

More information

Professor: Alvin Chao

Professor: Alvin Chao Professor: Avin Chao CS149 More with Casses and Objects OverLoading Let's ook at the Car cass... Terminoogy Method definition pubic void acceerate(doube amount) { speed += amount; if (speed > MAX_SPEED)

More information

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.

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. Sets & Hash Tabes Week 13 Weiss: chapter 20 CS 5301 Spring 2018 What are sets? A set is a coection of objects of the same type that has the foowing two properties: - there are no dupicates in the coection

More information

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

C++ Structures Programming Workshop 2 (CSCI 1061U) C++ Structures Programming Workshop 2 (CSCI 1061U) Faisal Qureshi http://faculty.uoit.ca/qureshi University of Ontario Institute of Technology C++ struct struct keyword can be used to define new data types

More information

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

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma. REVIEW cout Statement The cout statement invokes an output stream, which is a sequence of characters to be displayed to the screen. cout

More information

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

Lab 15 Review of Arrays, Array of Objects and Vector Dr. John Abraham, Professor Lab 15 Review of Arrays, Array of Objects and Vector Dr. John Abraham, Professor I have noticed over the years that students have great deal of difficulty dealing with composite and abstract data types.

More information

Using Parallel Arrays. Parallel Array Example

Using Parallel Arrays. Parallel Array Example Using Parallel Arrays Parallel arrays: two or more arrays that contain related data A subscript is used to relate arrays: elements at same subscript are related Arrays may be of different types Parallel

More information

Structures Unions and Enumerated Datatypes 224

Structures Unions and Enumerated Datatypes 224 STRUCTURES, UNIONS AND ENUMERATED DATA TYPES LEARNING OBJECTIVES After reading this chapter, the readers will be able to understand the purpose of the user defined data types Structures, Unions and Enumerated

More information

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

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration) Week 7: Advanced Loops while Loops in C++ (review) while (expression) may be a compound (a block: {s) Gaddis: 5.7-12 CS 1428 Fall 2015 Jill Seaman 1 for if expression is true, is executed, repeat equivalent

More information

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

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key Starting Out with C++ Early Objects 9th Edition Gaddis TEST BANK Full clear download (no formatting errors) at: https://testbankreal.com/download/starting-c-early-objects-9thedition-gaddis-test-bank/ Starting

More information

BITG 1113: POINTER LECTURE 12

BITG 1113: POINTER LECTURE 12 BITG 1113: POINTER LECTURE 12 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the concept of pointer. 2. Write declaration and initialization of a pointer. 3. Do arithmetic

More information

COMP322 - Introduction to C++

COMP322 - Introduction to C++ COMP322 - Introduction to C++ Winter 2011 Lecture 2 - Language Basics Milena Scaccia School of Computer Science McGill University January 11, 2011 Course Web Tools Announcements, Lecture Notes, Assignments

More information

Data Types & Variables

Data Types & Variables Fundamentals of Programming Data Types & Variables Budditha Hettige Exercise 3.1 Write a C++ program to display the following output. Exercise 3.2 Write a C++ program to calculate and display total amount

More information

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

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq CS 241 Computer Programming Introduction Teacher Assistant Hadeel Al-Ateeq 1 2 Course URL: http://241cs.wordpress.com/ Hadeel Al-Ateeq 3 Textbook HOW TO PROGRAM BY C++ DEITEL AND DEITEL, Seventh edition.

More information

Computer Programming

Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Parameter Passing in Function Calls Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

More information

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

Ch 6-1. Structures. March 30, Prof. Young-Tak Kim 2014-1 Ch 6-1. Structures March 30, 2014 Advanced Networking Technology Lab. (YU-ANTL) Dept. of Information & Comm. Eng, Graduate School, Yeungnam University, KOREA (Tel : +82-53-810-2497; Fax : +82-53-810-4742

More information