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

Similar documents
Data Types (C/C++) Structures

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

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

Functions, Arrays & Structs

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

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

CS 141, Introduction to Computer Science Fall Midterm Exam

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

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

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

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

Chapter 10 C Structures and Unions

LECTURE 11 STRUCTURED DATA

Chapter 11: Structured Data

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

Homework #3 CS2255 Fall 2012

LAB 4.1 Relational Operators and the if Statement

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Chap 0: Overview. Overview of basic C++ syntax Refresh programming basics C++ Vs. Java differences Coding conventions used. EECS 268 Programming II 1

Chapter 11: Structured Data

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

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

CS 115 Midterm 2 Solutions

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

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

.:: UNIT 1 ::. INTRODUCTION TO ALGORITHM & DATA STRUCTURES

F4104 ALGORITHM & DATA STRUCTURE

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

C++ For Science and Engineering Lecture 12

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

CS2141 Software Development using C/C++ C++ Basics

C++ Quick Guide. Advertisements

Name Section: M/W or T/TH. True or False (14 Points)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

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

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

Overloading Operators

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

CPE Summer 2015 Exam I (150 pts) June 18, 2015

Computer Programming : C++

a data type is Types

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

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 11: Records (structs)

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


l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

Ch 6. Functions. Example: function calls function

Creating a C++ Program

Lab Instructor : Jean Lai

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

CSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1]

A structure is an aggregate data type which contains a fixed number of heterogeneous components.

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

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

LECTURE 02 INTRODUCTION TO C++

Objectives. In this chapter, you will:

C++ Final Exam 2017/2018

Programming. C++ Basics

A SHORT COURSE ON C++

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

Boolean Algebra Boolean Algebra

Test Bank for Problem Solving with C++: The Object of Programming, 8/e Chapter 2 C++ Basics

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

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

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

structs // variable declarations

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

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

download instant at Introduction to C++

Objects and streams and files CS427: Elements of Software Engineering

The C++ Language. Arizona State University 1

Structured Data. CIS 15 : Spring 2007

I/O Streams and Standard I/O Devices (cont d.)

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Introduction to Programming

Scientific Computing

Lab # 02. Basic Elements of C++ _ Part1

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.

5. Selection: If and Switch Controls

File I/O Christian Schumacher, Info1 D-MAVT 2013

Chapter 2 Basic Elements of C++

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

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

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

Chapter 2: Introduction to C++

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

C++ Programming: From Problem Analysis to Program Design, Third Edition

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

IT 1033: Fundamentals of Programming Data types & variables

If Control Construct

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

Input and Output File (Files and Stream )

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Transcription:

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 +, -, *, /, %, ==,!=, <, >, <=, >=,... Which operation is not valid for float? 1 Data Types (C/C++) 11.2 Structures Scalar (or Basic) Data Types (atomic values) Arithmetic types Integers short, int, long char, bool Floating points float, double, long double Composite (or Aggregate) Types: Arrays: ordered sequence of values of the same type Structures: named components of various types 3 Used to represent a relationship between values of different types Example: student ID Number Name Age Major Address (the values are related because they belong 4 to the same student)

Structures Structures Define the student as a struct in C++: int age; string major; NOTE: semicolon after last brace! A struct is a data type, by convention the name is capitalized. The components are called members (or fields ). 5 So far we have defined a new data type, but we haven t defined any variables of that type. To define a variable of type Student: Student csstudent; Can define multiple variables of type Student: Student student1, student2, gradstudent; Each one has its own set of the member variables in the Student data type 6 Structures Each variable of type student has its own set of the member variables from the Student data type Student student1, student2; 11.3 Accessing Structure Members Use dot notation to access members of a struct variable: student1.age = 18; student2.idnumber = 123456; cin >> gradstudent.name; gradstudent.major = Rocket Science ; student1 idnumber name age major student2 idnumber name age major 7 Member variables of structures can be used just like regular variables of the same type. student1.age++; //happy birthday myfunc(student2.idnumber); if (student1.age==student2.age) {... 8

Structures: operations Structures: output Valid operations over entire structs: assignment: student1 = student2; function call: myfunc(gradstudent,x); Invalid operations over structs: comparison: student1 == student2 output: cout << student1; input: cin >> student2; Must do these member by member 9 Output the members one at a time: cout << student1.idnumber << " "; cout << student1.name << " "; cout << student1.age << " "; cout << student1.major << endl; Output: 11122 Chris Johnson 19 Football Comparing two structs: if (student1.idnumber == student2.idnumber && student1.name == student2.name && student1.age == student2.age && student1.major == student2.major)... 10 11.4 Initializing structures 11.5 Arrays of Structures Struct variable can be initialized when it is defined: Student student1 = {123456, John Smith,22, Math Must give values in order of the struct declaration. Can NOT initialize members in structure declaration, only variable definition: You can store values of structure types in arrays. Student roster[40]; //holds 40 Student structs Each student is accessible via the subscript notation. roster[0] = student1; Members of structure accessible via dot notation struct StudentA { int id = 123456; //ILLEGAL string name = John Smith ; //ILLEGAL 11 cout << roster[0].name << endl; 12

Arrays of Structures 11.6 Nested Structures Arrays processed in loops: You can nest one structure inside another. 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 all the id numbers and names for (int i=0; i<40; i++) { cout << roster[i].idnumber << endl; cout << roster[i].name << endl; struct Address { string street; string city; string state; int zip; Address homeaddress; 13 14 Nested Structures 11.7 Structures as function arguments Use dot operator multiple times to get into the nested structure: Student student1; student1.name = Bob Lambert ; student1.homeaddress.city = San Angelo ; student1.homeaddress.state = TX ; Or set up address structure separately: Address a1; a1.street = 101 Main St. ; a1.city = San Angelo ; a1.state = TX ; a1.zip = 76903; student1.name = Bob Lambert ; Structure variables may be passed as arguments to functions. void showstudent(student x) { cout << x.idnumber << endl; cout << x.name << endl; cout << x.age << endl; cout << x.major << endl; // in main: Student student1; //input information about student1 here showstudent(student1); student1.homeaddress = a1; 15 16

Structures as function arguments 11.8 Returning Structure from Function By default, structure variables are passed by value (like most variables). If the function needs to change the value of a member, the structure variable should be passed by reference. void happybirthday(student &s) { s.age++; 17 A function may return a structure. Student inputstudent(ifstream &fin) { Student result; fin >> result.idnumber; fin >> result.name; fin >> result.age; fin >> result.major; return result; // in main: ifstream infile; infile.open( students.dat ); Student student1 = inputstudent(infile); for (int i=0; i<40; i++) roster[i] = inputstudent(infile); infile.close(); Always pass input/output streams by reference!! 18 Example: nested Structures Could have multiple structs using Address: float gpa; Address homeaddress; Address campusaddr; struct Faculty { string officelocation; Address address; struct GradStudent { int yeargraduated; Address homeaddress; Address campusaddr; 19 Example: nested Structures Could have one function to process Addresses void showaddress(address x) { cout << x.street << endl; cout << x.city <<, ; cout << x.state << ; cout << x.zip << endl; Call it for different structure types with Address: Student st; Faculty fac; GradStudent gs; //... showaddress(st.homeaddress); showaddress(fac.address); showaddress(gs.campusaddr); 20

Nested Arrays and Structures struct Course { string course; int section; string title; string days; string time; string bldg; int roomnum; string instructor; Student enrolledstudents[35000]; string major; Address address; Course schedule[10]; int numcourses; Initializing arrays of structures Provide an initialization list for one or more of the elements in the array: Student roster[40] = { {123456, John Smith,22, Math {444555, Lisa Simpson,18, Biology, {999999, Tony Jackson,25, Physics, {887766, Melissa Brown,20, Engineering enrolledstudents[8].schedule[0].course = CS2308 ; 21 22