Fondamenti di informatica structures

Slides:



Advertisements
Similar presentations
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Advertisements

MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
CMPS 1371 Introduction to Computing for Engineers STRUCTURE ARRAYS.
Lec 13 Oct 20 cell arrays (Chapter 4) structures (Chapter 4) generating subsets generating permutations (HW 4 problem)
Lec 15 Oct 27 more examples of recursive programs more about cell arrays structures in Matlab.
Cells and Structures Array Cells and Array Structures.
Lecture 18 Oct 24 Cell arrays (Ch 4), structures recursion (Ch 12)
Lecture 4 Sept 7 Chapter 4. Chapter 4 – arrays, collections and indexing This chapter discusses the basic calculations involving rectangular collections.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Lec 14 Oct 22 more examples of recursive programs more about cell arrays structures in Matlab.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Engineering Computation with MATLAB Second Edition by David M. Smith.
Lecture 15 Chapters 6 and 7. Outline from Chapter Character String Concepts: Mapping and Casting 6.2 MATLAB Implementation Slicing and Concatenating.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS 1308 Computer Literacy and the Internet
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
A FIRST BOOK OF C++ CHAPTER 16 DATA STRUCTURES. OBJECTIVES In this chapter, you will learn about: Single Structures Arrays of Structures Structures as.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Covenant College December 18, Laura Broussard, Ph.D. Visiting Professor COS 131: Computing for Engineers Chapter 7: Cell Arrays and Structures.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
CompSci Announcements  Read Chap 3 for next time  Homework will be posted  Consulting Hours will start soon.
for-each PROS CONS easy to use access to ALL items one-by-one
OOP - Object Oriented Programming
Topic: Python Lists – Part 1
11 Map ADTs Map concepts. Map applications.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
IS 130 Information systems 1
Introduction to Computational Thinking
Chapter 10-1: Structure.
Database Systems Chapter 3 1.
Dialog Boxes Applications of Cell-Arrays
Department of Computer Science,
EGR 115 Introduction to Computing for Engineers
CMPT 120 Topic: Python strings.
About the Presentations
Java Review: Reference Types
While Loops BIS1523 – Lecture 12.
Arrays An Array is an ordered collection of variables
Structures putting data together.
Structs And Arrays.
Chapter 9: Records (structs)
Chapter 9: Records (structs)
Chapter 10: Records (structs)
Lists in Python.
Lecture 22 Inheritance Richard Gesick.
Derived types.
Chapter 9: Records (structs)
Lesson 16: Functions with Return Values
Matlab tutorial course
Java Programming Arrays
Programming in C Pointer Basics.
Type Systems CSE 340 – Principles of Programming Languages Fall 2016
Dynamic Data Structures and Generics
CS148 Introduction to Programming II
Chapter 8 Data Structures: Cell Arrays and Structures
Programming in C Pointer Basics.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Python Primer 1: Types and Operators
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Random Access Files / Direct Access Files
COP 3330 Object-oriented Programming in C++
How to organize and document your classes
Programming Logic and Design Fifth Edition, Comprehensive
CHAPTER 4: Lists, Tuples and Dictionaries
Structures In C Programming By Rajanikanth B.
OO Programming Concepts
Chapter 8 Data Structures: Cell Arrays and Structures, Sorting
Lists CMSC 202, Version 4/02.
Structures Structured Data types Data abstraction structs ---
Presentation transcript:

Fondamenti di informatica structures Prof. Luigi Maria Ingrosso

Concept: Collecting Dissimilar Objects Heterogeneous collections permit objects of different data types to be grouped in a collection. They allow data abstraction to apply to a much broader range of content. However, the fact that the contents of these collections may be of any data type severely restricts the operations that can be performed on the collections as a whole. Whereas a significant number of arithmetic and logical operations can be performed on whole number arrays, algorithms that process heterogeneous collections must deal with the data contents one item at a time. A.A. 2016/'17 prof. Luigi Maria Ingrosso

prof. Luigi Maria Ingrosso MATLAB Structures Structures allow items in the collection to be indexed by field name. The data contained in a structure is referenced by field name, e.g., item1. The rules for making a field name are the same as those for a variable. Fields of a structure, like the elements of a cell array, are heterogeneous—they can contain any MATLAB object. A.A. 2016/'17 prof. Luigi Maria Ingrosso

prof. Luigi Maria Ingrosso Constructing and Accessing One Structure To set the value of items in a structure A, the syntax is as follows: >> A.item1 = 'abcde' A = item1: 'abcde' >> A.item2 = 42 A = item1: 'abcde' item2: 42 Fields in a structure are accessed in the same way—by using the dotted notation. >> A.item2 = A.item2 ./ 2 A = item1: 'abcde' item2: 21 Da provare A.A. 2016/'17 prof. Luigi Maria Ingrosso

prof. Luigi Maria Ingrosso Manipulating Field Names To determine the names of the fields in a structure, the built-in function fieldnames(...) returns a cell array containing the field names as strings. >> names = fieldnames(A) names = 'item1' 'item2' Fields can also be accessed “indirectly” by setting a variable to the name of the field, and then using parentheses to indicate that the variable contents should be used as the field name: >> fn = names{1}; >> A.(fn) = [A.(fn) 'fg'] A = item1: 'abcdefg' item2: 21 A.A. 2016/'17 prof. Luigi Maria Ingrosso

prof. Luigi Maria Ingrosso More about Field Names You can remove a field from a structure using the built-in function rmfield(...). Be careful. rmfield(...) returns a new structure with the requested field removed. It does not remove that field from your original structure. If you want the field removed from the original, you must assign the result from rmfield(...) to replace the original structure: >> A = rmfield(A, 'item1') A = item2: 21 A.A. 2016/'17 prof. Luigi Maria Ingrosso

prof. Luigi Maria Ingrosso Why Constructor Functions? Use constructor functions, as opposed to “manually” entering data into structures, for the following reasons: Manual entry can result in strange behavior due to typographical errors or having fields in the wrong order The resulting code is generally more compact and easier to understand When constructing collections of structures, it enforces consistency across the collections A.A. 2016/'17 prof. Luigi Maria Ingrosso

prof. Luigi Maria Ingrosso Built-in Constructor Function struct(…) >> struct('first','Fred', ... 'last','Jones', ... 'phone','(123) 555-1212', ... 'birth', struct( 'day', 31, ... 'month', 'February', ... 'year', 1965 )) ans = first: 'Fred' last: 'Jones' phone: '(123) 555-1212' birth: [1x1 struct] A.A. 2016/'17 prof. Luigi Maria Ingrosso

prof. Luigi Maria Ingrosso Custom Constructor Functions A typical custom constructor function function ans = makeCD(gn, ar, ti, yr, st, pr) % integrate CD data into a structure ans.genre = gn ; ans.artist = ar ; ans.title = ti; ans.year = yr; ans.stars = st; ans.price = pr; Usage: >> CD = makeCD('Blues', 'Charles, Ray', & 'Genius Loves Company', 2004, 4.5, 15.35 ) CD = genre: 'Blues' artist: 'Charles, Ray' title: 'Genius Loves Company' year: 2004 stars: 4.5000 price: 15.3500 A.A. 2016/'17 prof. Luigi Maria Ingrosso

prof. Luigi Maria Ingrosso Building Structure Arrays Manually >> entry(1).first = 'Fred'; >> entry(1).last = 'Jones'; >> entry(1).age = 37; >> entry(1).phone = ' (123) 555-1212'; >> entry(2).first = 'Sally’; >> entry(2).last = 'Smith’; >> entry(2).age = 29; >> entry(2).phone = '(000) 555-1212' first element second element entry = 1x2 structure array with fields: first last age phone A.A. 2016/'17 prof. Luigi Maria Ingrosso

prof. Luigi Maria Ingrosso Building Structure Arrays with struct(…) genres = {'Blues', 'Classical', 'Country' }; artists = {'Clapton, Eric', 'Bocelli, Andrea', … 'Twain, Shania' }; years = { 2004, 2004, 2004 }; stars = { 2, 4.6, 3.9 }; prices = { 18.95, 14.89, 13.49 }; cds = struct( ‘genre’, genres, … 'artist', artists, … 'year', years, … 'stars', stars, … 'price', prices); A.A. 2016/'17 prof. Luigi Maria Ingrosso

prof. Luigi Maria Ingrosso Building Structure Arrays with makeCD(…) cds(1) = makeCD('Blues', 'Clapton, Eric', ... 'Sessions For Robert J', 2004, 2, 18.95 ) cds(2) = makeCD('Classical', ... 'Bocelli, Andrea', 'Andrea', 2004, 4.6, 14.89 ) cds(3) = makeCD( 'Country', 'Twain, Shania', ... 'Greatest Hits', 2004, 3.9, 13.49 ) cds(4) = makeCD( 'Latin', 'Trevi, Gloria', ... 'Como Nace El Universo', 2004, 5, 12.15 ) cds(5) = makeCD( 'Rock/Pop', 'Ludacris', ... 'The Red Light District', 2004, 4, 13.49 ) cds(6) = makeCD( 'R & B', '2Pac', ... 'Loyal To The Game', 2004, 3.9, 13.49 ) cds(7) = makeCD( 'Rap', 'Eminem', ... 'Encore', 2004, 3.5, 15.75 ) cds(8) = makeCD( 'Heavy Metal', 'Rammstein', ... 'Reise, Reise', 2004, 4.2, 12.65 ) A.A. 2016/'17 prof. Luigi Maria Ingrosso