Docsity

Structures - Computing for Engineers - Lecture Slides | CS 1371, Assignments of Computer Science

Partial preview of the text

Download Structures - Computing for Engineers - Lecture Slides | CS 1371 and more Assignments Computer Science in PDF only on Docsity!

CS

Introduction to Computing

for Engineers

Structures

Structures

LearningObjectives Learn aboutstructures TopicsStructures (Chapter 9)Summary

Creating a Structure…

Let’s create a simple structure:

person.firstname = ‘George’;

person.lastname = ‘Burdell’; - person.address1 = ‘803 Shallowford Lane’; - person.city = ‘Peachtree City’; - person.state = ‘GA’; - person.zip =

Structures are Universal Containers

Any type of data can be loaded into a structure array

Different data types can be loaded and will overwritewhatever is already there - Structure addressing is simpler that Cell Arrays - fieldnames( ) will return the field names in a structure - You should get used to using the dot notation because itis widely used in programming, especially with objects(later)

Problem Solving

CD Album – create a cell array containing informationabout your CDs.

Each entry in the cell array will be a structure describingthe CD:

Genre

Artist - Title - Tracks - Year - Then write a function to search this cell array for all CDsnewer than a given year and return their titles in a newcell array.

The script:

clc clear % want to build a cell array of CD structures album{1} = makeCD( 'rap', 'P Diddy', 'Doofus', 16, 1996); album{2} = makeCD( 'rock', 'Lincoln Park', 'Metea', 16, 2003); album{3} = makeCD( 'classic', 'Yanni', 'Dreaming', 16, 1999); album{4} = makeCD( 'country', 'Gart Brooks', 'Sevens', 16, 1995); album{5} = makeCD( 'rock', 'SKA', 'Rush', 16, 1998); album{6} = makeCD( 'classic', 'Yasha Haifiz', 'Mozart # 1', 16, 1815); album{7} = makeCD( 'pop', 'Britny Spears ', 'Doofus', 16, 2002); album{8} = makeCD( 'jazz', 'Louis Armstrong', 'Wonderful World', 16, 1949); album{9} = makeCD( 'soul', 'James Brown', 'Drug Addict', 16, 1989); album{10} = makeCD( 'disco', 'BeeGees', 'With It', 16, 1979); album{11} = makeCD( 'rock', 'Elvis Presley', 'Jailhouse Rock', 16, 1948);

The makeCD function

function CD = makeCD(g, a, ti, tr, y) % make a CD structure with the given data CD.genre = g; CD.artist = a; CD.title = ti; CD.tracks = tr; CD.year = y;

The filterCD function

function c = filterCDs( a, yr ) % extract the titles of CDs recorded since this year c = [ ]; index = 1; for ix = 1:length(a) thisCD = a{ix}; if thisCD.year >= yr c{index} = thisCD.title; index = index + 1; end end

Further Study

There are only 6 different operations you can possiblyperform on a collection of data items.

Each operation requires a function to specify how thatoperation should be performed - The operations are:

Build – create the collection from some source

Traverse – touch each item and summarize - Map – change the values in each item - Filter – remove some items from the collection - Search – determine whether a specific item exists - Sort – re-order the collection

Summary

LearningMatlab can storeand manipulatemuch more thansimply numbers

Action Items Review the lecture Review the material on the

websites?