More on Structs Sometimes we use structs even when the fields are all of the same type. If the fields are different conceptually, that is, the data stands.

Slides:



Advertisements
Similar presentations
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Advertisements

Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing.
Structure TDate struct TDate { int year, month, day; }; // Define a new data type.
Selection Sorting S[] : array of int or float Size: number of elements of s[] Pseudocode for i = 0 to size - 2 find the index of a smallest element between.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Arrays, cont MONDAY – APRIL 6, Review from lab Deep vs Shallow copy array1array array2 = array1; What happens?
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
1 Chapter 10 & 11 enum & Structured Types Dale/Weems.
Common Elementary Algorithms Some of the basic but frequently used algorithms for manipulating arrays. These algorithms are so important that: a)Some programming.
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType.
Selection Sorting Pseudocode (Forward) for i = 0 to size - 2 find the index of the required element between s[i] and s[size - 1] If i not the same as index.
Searching Arrays Linear search Binary search small arrays
Basic concepts of C++ Presented by Prof. Satyajit De
Passing Objects to Methods
Classes and Data Abstraction
11 Chapter Structured Data
CO1401 Program Design and Implementation
Two Dimensional Array Mr. Jacobs.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Arrays Part-1 Armen Keshishian.
Student Book An Introduction
solve the following problem...
Structures - Part II aggregate operations arrays of type struct
Pointer.
A solution to a list of records
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Student Data Score First Name Last Name ID GPA DOB Phone ...
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Structures Lesson xx In this module, we’ll introduce you to structures.
More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes.
CS 1430: Programming in C++.
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
One-Dimensional Array Introduction Lesson xx
Passing Structures Lesson xx
Array of Structures A structure holds only one record But a record
Review for Final Exam.
Chapter 11: Structured Data.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Summary Two basic concepts: variables and assignments Basic types:
If Statements.
Review for Final Exam.
The Function Prototype
CSC 142 Arrays [Reading: chapter 12].
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Programming Structures.
(Dreaded) Quiz 2 Next Monday.
Standard Version of Starting Out with C++, 4th Edition
Structure (i.e. struct) An structure creates a user defined data type
Programming Structures.
Structures Structured Data types Data abstraction structs ---
Selection Sorting S[] : array of int or float
Exercise 6 – Compound Types und Kontrollfluss
Presentation transcript:

More on Structs Sometimes we use structs even when the fields are all of the same type. If the fields are different conceptually, that is, the data stands for different things even though the type is the same, we may use a struct. Example: struct Time { int hour, minute, second; };

Nested Structs Since a struct is a new type, it can be used where ever types are used, included as the type of a field in another struct. For example, consider the following struct: enum Month { Jan, Feb, Mar, ... }; string monthName[] = { “January”, ... }; struct Date { Month month; int day; int year; };

Internal v. External Representation The last slide hinted at an important concept: the difference between the internal representation and the external representation of data. The internal representation is what the program uses. It should be easy to manipulate, e.g., compare values. The external representation is for the human user – it should be easy to read and understand.

Internal v. External (cont'd) The program converts from the external to the internal representation when reading in. The program converts from the internal to external representation when printing. To read in months, we would convert from strings to the enumerated type, Month. This would need a multi-way if-else statement. To print the months, an array of strings works well.

Internal v. External - Example Reading in: string m; Date date; cin >> m; if(m == “January”) date.month = Jan; else if(month == “February”) date.month = Feb; ... Printing out: cout << monthName[date.month] << endl;

Nested Struct - Example This could be used as part of the StudentRecord struct: struct StudentRecord { string firstName; string lastName; int id; Date dob; int year; double qpa; };

Example (cont'd) To access this field, we must first access the date of birth: StudentRecord sr; sr.dob.month = Jan; sr.dob.day = 14; sr.dob.year = 1995; cout << MonthName[sr.dob.month] << “ “ << sr.dob.day << “ “ << sr.dob.year << endl;

Boxes in Boxes Note that the dob field is itself a data object. Think of the dob as a box inside of another box, the StudentRecord, sr. As a data object, it can be passed to a function: void printDate(const Date& date) { cout << MonthName[date.month] << “ “ << date.day << “ “ << date.year << endl; } In main: printDate(sr.dob);

Arrays of Structs We can have an array of structs. This is common when processing records. The statement: StudentRecord sr[10]; declares (and defines) an array of ten (10) StudentRecords. As with any other array, sr[0] is the first element, a StudentRecord, sr[1], the second, and sr[9], the last.

Sorting an Array of structs for(int i=1; i<10; i++) { StudentRecord x = sr[i]; int j = i-1; while(j >= 0 && sr[j].id > x.id) { sr[j+1] = sr[j]; j--; } sr[j+1] = x; Insertion sort is particularly useful for sorting arrays of structs, since each element is copied just once.

Structs with Arrays A struct can also have a field which is an array. For example, the StudentRecord struct could have an array of grades: struct StudentRecord { ... double grades[40]; }; To access a single grade, you must use the field name and then an index: cout << sr.grades[i] << endl;

Exercise Write a boolean function, dateGreater, that takes two Dates and returns true if the first date is greater than the second date (comes after), and false otherwise. Then change the sort program to sort the student records by date of birth, from youngest student to oldest.