Structures Jordi Cortadella Department of Computer Science.

Slides:



Advertisements
Similar presentations
Exposure C++ Chapter XVI C++ Data Structures, the Record.
Advertisements

1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Introduction to Programming (in C++) Subprograms: procedures and functions Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science,
Introduction to Programming (in C++) Numerical methods I Jordi Cortadella Dept. of Computer Science, UPC.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Introduction to Programming (in C++) Vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
More Storage Structures A Data Type Defined by You Characteristics of a variable of a specific ‘data type’ has specific values or range of values that.
Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Conclusions Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Reasoning with invariants Jordi Cortadella Department of Computer Science.
Data types and their representation Jordi Cortadella Department of Computer Science.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chars and strings Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Data structures Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Matrices Jordi Cortadella Department of Computer Science.
Prime numbers Jordi Cortadella Department of Computer Science.
The power of logarithmic computations Jordi Cortadella Department of Computer Science.
A little bit of geometry Jordi Cortadella Department of Computer Science.
Gaussian elimination Jordi Cortadella Department of Computer Science.
Recursion Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
Approximate computations Jordi Cortadella Department of Computer Science.
Sequences Jordi Cortadella Department of Computer Science.
Jordi Cortadella Dept. of Computer Science, UPC
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
Operator Overloading & Exception Handling TCP1201 OOPDS 1 Lecture 5 1.
Data structure design Jordi Cortadella Department of Computer Science.
Polynomials Jordi Cortadella Department of Computer Science.
Greatest Common Divisor Jordi Cortadella Department of Computer Science.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
Sudoku Jordi Cortadella Department of Computer Science.
First steps Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Complexity Analysis of Algorithms
Reusing computations Jordi Cortadella Department of Computer Science.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Sorting Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Numerical algorithms Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
CPS120: Introduction to Computer Science Data Structures.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Vectors Jordi Cortadella Department of Computer Science.
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.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
Parameter passing Jordi Cortadella Department of Computer Science.
Functions Jordi Cortadella Department of Computer Science.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
Jordi Cortadella, Ricard Gavaldà, Fernando Orejas
Introduction to Programming (in C++) Data structures
Reasoning with invariants
Jordi Cortadella Department of Computer Science
Programming Languages Dan Grossman 2013
For loops, nested loops and scopes
CS150 Introduction to Computer Science 1
Object-Oriented Programming Using C++
CS360 Windows Programming
Jordi Cortadella Department of Computer Science
Algorithmics and Programming II: Introduction
CS148 Introduction to Programming II
Jordi Cortadella Department of Computer Science
Jordi Cortadella Department of Computer Science
Complexity Analysis of Algorithms
CS148 Introduction to Programming II
CPS120: Introduction to Computer Science
Search algorithms for vectors
Jordi Cortadella and Jordi Petit Department of Computer Science
Jordi Cortadella Department of Computer Science
Presentation transcript:

Structures Jordi Cortadella Department of Computer Science

Structures Also known as tuples or records in other languages All components of a vector have – the same type (e.g. int) – a uniform name: vector_name[index] Sometimes we want a structured variable to contain – Components of different types – Specific meanings – they should have different names Introduction to Programming© Dept. CS, UPC2

Example: Person // A var_person variable contains // heterogeneous information on a person struct { string first_name, last_name; int age; bool can_vote; ; // assuming an type exists } var_person; Introduction to Programming© Dept. CS, UPC3 first_name, last_name, age, can_vote and are the fields of var_person

Example: Person // An alternative way of defining a type // with a struct in C++ struct Person { string first_name, last_name; int age; bool can_vote; ; }; Person var_person; Introduction to Programming© Dept. CS, UPC4

Structures: another example struct Address { string street; int number; }; struct Person { string first_name, last_name; int age; Address address; bool can_vote; }; Introduction to Programming© Dept. CS, UPC5

Example: Person The dot operator. selects fields of a struct variable, the same way that the [ ] operator selects components of a vector variable Person p; cin >> p.first_name >> p.last_name; cin >> p.address.street; cin >> p.address.number; cin >> p.age; p.can_vote = (p.age >= 18); Introduction to Programming© Dept. CS, UPC6

Structures to return results Structs can be used in the definition of functions that return more than one result: struct Result { int location; bool found; }; Result search(int x, const vector & A); Introduction to Programming© Dept. CS, UPC7

Example: rational numbers Two components, same type, different meaning: struct Rational { int num, den; }; We could also use a vector of size 2, but we should always remember: “was the numerator in v[0] or in v[1]?” It produces uglier code that leads to errors Introduction to Programming© Dept. CS, UPC8

Invariants in data structures Data structures frequently have some properties (invariants) that must be preserved by the algorithms that manipulate them. Examples: struct Rational { int num, den; // Inv: den > 0 }; struct Clock { int hours, minutes, seconds; / Inv: 0 <= hours < 24, 0 <= minutes < 60, 0 <= seconds < 60 / }; Introduction to Programming© Dept. CS, UPC9

Example: rational numbers // Returns a + b. Rational sum(Rational a, Rational b) { Rational c; c.num = a.numb.den + b.numa.den; c.den = a.denb.den; // c.den > 0 since a.den > 0 and b.den > 0 reduce(c); // simplifies the fraction return c; } Introduction to Programming© Dept. CS, UPC10

Example: rational numbers // Post: r is in reduced form. void reduce(Rational& r) { int m = gcd(abs(r.num), r.den); r.num = r.num/m; r.den = r.den/m; // r.den > 0 is preserved } Introduction to Programming© Dept. CS, UPC11

Structures Exercise: using the definition struct Clock { int hours; // Inv: 0 <= hours < 24 int minutes; // Inv: 0 <= minutes < 60 int seconds; // Inv: 0 <= seconds < 60 } write a function that returns the result of incrementing a Clock by 1 second. // Returns c incremented by 1 second. Clock increment (Clock c); (Note: the invariants of Clock are assumed in the specification.) Introduction to Programming© Dept. CS, UPC12

Structures Clock increment(Clock c) { c.seconds = c.seconds + 1; if (c.seconds == 60) { c.seconds = 0; ++c.minutes; if (c.minutes == 60) { c.minutes = 0; ++c.hours; if (c.hours == 24) c.hours = 0; } } // The invariants of Clock are preserved return c; } Introduction to Programming© Dept. CS, UPC13

Summary Structures can hold multiple heterogeneous data under a single variable. They can provide simple abstractions of complex objects. The generalization of structures with their own methods (functions) leads to the Object- Oriented Programming (OOP) paradigm. Introduction to Programming© Dept. CS, UPC14