You are on page 1of 6

3/22/18

Recap
•  Last Class
–  Polynomials
COL100 INTRODUCTION TO COMPUTING
Indian Ins(tute of Technology Delhi

Indian Ins(tute of Technology Delhi


•  Today
& –  Cell Arrays
COMPUTER SCIENCE –  Simula:ons
Programming

Array vs. Cell Array Other Examples


•  Vectors and matrices •  Cell array: individual
•  Simple array 0 0 B
store values of the components may
–  Each component stores one scalar. same type in all contain different types
Indian Ins(tute of Technology Delhi

Indian Ins(tute of Technology Delhi

•  E.g., one char, one double, or one uint8 value components of data
•  All components have the same type


•  Cell array
–  Each cell can store something “bigger” than one
scalar, e.g., a vector, a matrix, a char vector
–  The cells may store items of different types

1
3/22/18

Versus Arrays Cell Arrays


•  Create •  Use braces { } for
•  Cell array of char vectors
m= [ 5, 4 ; … creating and
C= { ‘Kolkata’,’New Delhi’,’Chennai’} addressing cell arrays
Indian Ins(tute of Technology Delhi

Indian Ins(tute of Technology Delhi


1, 2 ; …
Kolkata New Delhi Chennai 0, 8 ] •  Create
•  Cell Array •  Addressing C= { ones(2,2), 4 ; …
C= { ‘Kolkata’;’New Delhi’;’Chennai’} –  m(1,2)=pi; ‘abc’ , ones(3,1); …
9 , ‘a cell’ }

Kolkata Contrast with 2-d array of characters •  Addressing
New Delhi C{2,1}= ‘ABC’
Kolkata C{3,2}= pi
Chennai
New Delhi disp(C{3,2})
Chennai

Crea:ng cell arrays… Deck of Cards


C= {‘Oct’, 30, ones(3,2)}; •  Represent a deck of cards
D{1} = ‘A Hearts’;
is the same as
Indian Ins(tute of Technology Delhi

Indian Ins(tute of Technology Delhi

D{2} = ‘2 Hearts’;
C= cell(1,3); % not necessary :
C{1}= ‘Oct’; D{13} = ‘K Hearts’;
D{14} = ‘A Clubs’;
C{2}= 30; :
C{3}= ones(3,2); D{52} = ‘K Diamonds’;
You can assign the empty cell array: D = {}
•  But this means that have to type all combina:ons
of suits and ranks in crea:ng the deck…
– How do we proceed?

2
3/22/18

Use Cell Arrays Populate the Deck Cell Array


•  Use Nested Loops
•  Make use of a suit array and a rank array … function D = CardDeck()
–  suit = suit= {’Hearts’,’Clubs’,’Spades’,’Diamonds’};
Indian Ins(tute of Technology Delhi

Indian Ins(tute of Technology Delhi


•  {‘Hearts’, ‘Clubs’, ‘Spades’, ‘Diamonds’}; rank=
–  rank = {’A’,’2’,’3’,’4’,’5’,’6’,’7’,’8’,’9’,’10’,’J’,’Q’,’K’};
•  {‘A’,‘2’,’3’,’4’,’5’,’6’,’7’,’8’,’9’,’10’,’J’,’Q’,’K’}; i= 1; % index of next card
•  Then concatenate to get a card. for k= 1:4 % Set up the cards in suit k
–  str = [rank{3} ‘ ’ suit{2} ]; for j= 1:13
–  D{16} = str; D{i}= [ rank{j} ' ' suit{k} ];
i= i + 1;
•  So D{16} stores ‘3 Clubs’ end
end

Shuffle a Card Deck Shuffle


function T = Shuffle(S)
% S is 1-by-n cell array and n is even.
•  The “perfect shuffle” of a 12-card deck % T is 1-by-n cell array that is a perfect shuffle of S.
Indian Ins(tute of Technology Delhi

Indian Ins(tute of Technology Delhi

n = length(S);
mid = n/2;
T = cell(1,n); Top=cell(1,mid); Bot= cell(1,mid);
•  Cut the Deck % Cut the deck
for k= 1:mid
Top{k}= S{k};
Bot{k}= S{mid+k};
end
•  Alternate
% Alternate
for k= 1:mid
T{2*k-1} = Top{k};
T{2*k} = Bot{k};
end

3
3/22/18

Levels of AbstracAon Recap


•  Packaging procedures (program •  Structures with array fields
– xc: x-coordinate of center
instructions) into a function
Indian Ins(tute of Technology Delhi

Indian Ins(tute of Technology Delhi


– yc: y-coordinate of center
–  A program is a set of functions executed in the – r: radius
specified order – c: rgb color vector
–  Data is passed to (and from) each function •  D1 = struct(‘xc’,1,’yc’,2,’r’,3,’c’,[1 0 1]);
•  D2 = struct(‘xc’,4,’yc’,0,’r’,1,’c’,[.2 .5 .3]);
•  Packaging data into a structure •  Compute “average” of two disks
–  Elevates thinking – r = (D1.r + D2.r) /2;
–  Reduces the number of variables being – xc = (D1.xc + D2.xc)/2;
passed to and from functions – yc = (D1.yc + D2.yc)/2;
– c = (D1.c + D2.c) /2;
•  D = struct(‘xc’,xc,’yc’,yc,’r’,r,’c’,c)

Levels of AbstracAon A Card Game


•  Iden:fy “objects” in the game
•  Packaging procedures (program and define each:
•  Develop the algorithm
instructions) into a function •  Card
—the logic— of the –  Proper:es: suit, rank
–  A program is a set of functions executed in the
card game:
Indian Ins(tute of Technology Delhi

Indian Ins(tute of Technology Delhi

–  Ac:ons: compare, show


specified order
–  Set up a deck as an array •  Deck
–  Data is passed to (and from) each function
–  Property: array of Cards
of cards. (First, choose
•  Packaging data into a structure –  Ac:ons: shuffle, deal, get #cards
–  representa:on of cards.) lek
–  Elevates thinking
–  Shuffle the cards •  Hand …
–  Reduces the number of variables being passed to and •  Player …
from functions –  Deal cards to players
•  Then write the game—the
•  Packaging data, and the instructions that –  Evaluate each player’s algorithm—using objects of the
hand to determine above “classes”
work on those data, into an object winner
–  A program is the interaction among objects Object-oriented programming:
Procedural programming: focus on focus on the design of the objects
–  Object-oriented programming (OOP) focuses on the the algorithm, i.e., the procedures, (data+acNons) necessary for
design of data-instructions groupings necessaryfor solving a problem solving a problem

4
3/22/18

OOP Example
•  Define the classes (of the objects) •  class: Rectangle
–  Iden:fy the proper:es (data) and •  Proper:es:
Indian Ins(tute of Technology Delhi

Indian Ins(tute of Technology Delhi


ac:ons(methods, i.e., func:ons) of each class –  xLL, yLL, width, height
•  Create the objects (from the classes) that are •  Methods (ac:ons):
then used—that interact with one another –  Calculate area
•  Defining a class ≠ crea:ng an object –  Calculate perimeter
–  A class is a specifica:on –  Draw
–  An object is a concrete instance of the class –  Intersect
•  intersec:on between two rectangles is a rectangle

Defining a class Example


•  A class is instan:ated with the classdef •  Class Interval
keyword •  An interval has two proper:es:
Indian Ins(tute of Technology Delhi

Indian Ins(tute of Technology Delhi

–  lek, right
•  Ac:ons—methods—of an interval include
–  Scale, i.e., expand
–  Shik
–  Add one interval to another
–  Check if one interval is in another
–  Check if one interval overlaps with another
•  The source code must go inside a file with the
same name

5
3/22/18

Classdef Intervals :: Use


classdef Interval < handle end % Play with the Interval class disp(q)
% An Interval has a left endpoint and a right endpoint. clear all pause
function tf = isIn(self, other)
clc disp('See the properties of the interval
properties % tf is true if self is in the other Interval referenced by p')
Indian Ins(tute of Technology Delhi

Indian Ins(tute of Technology Delhi


left tf= self.left>=other.left && self.right<=other.right; disp(p)
p= Interval(3,7) % p references a newly
right end created Interval object disp('p and q reference the SAME object!')
end
function Inter = overlap(self, other) pause pause
methods % If self and the other Interval overlap, then Inter is
the overlapped Interval; otherwise Inter is empty array
disp('Make the interval twice as wide and fprintf('\nCreate a second and third interval
function Inter = Interval(lt, rt) then shift it left by one unit') objects\n')
% Constructor: construct an Interval object of ttype Interval.
p.scale(2) r= Interval(4,9)
Inter.left= lt; disp(p) s= Interval(0,1)
Inter= Interval.empty();
Inter.right= rt;
left= max(self.left, other.left); p.shift(-1)
end
right= min(self.right, other.right); disp(p) pause
if right-left > 0 fprintf('\nOverlap intervals p and r\n')
function scale(self, f)
Inter= Interval(left, right); pause a= p.overlap(r)
% Scale self by a factor f
w= self.right - self.left; end fprintf('\nLet q reference the same interval pause
end as p\n') fprintf('\nOverlap intervals p and s\n')
self.right= self.left + w*f;
q= p; % q and p both reference (point to) theb= p.overlap(s)
end same interval.
end %methods
% Note that so far we have only ONE
function shift(self, s) interval object, referenced
% Shift self by s end %classdef
% by both p and q.
self.left= self.left + s; disp('Shift the interval referenced by q to the
self.right= self.right + s; right 3 units')
q.shift(3)

You might also like