Top Banner
fprintf and other examples
26

Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

fprintf and other examples

Page 2: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

fprint function

>> a=2;b=3;c=5;

>> fprintf('a = %d , b = %d, c = %d', a, b, c);

a = 2 , b = 3, c = 5

>> fprintf('a=%d , b=%d', a, b);fprintf('a+b=%d ', a+b);

a=2 , b=3a+b=5

>> fprintf('a=%d , b=%d \n', a, b);fprintf('a+b=%d ', a+b);

a=2 , b=3

a+b=5

Page 3: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

fprintf

>> a = 3.7;>> fprintf('a=%d , b=%d \n', a, b);fprintf('a+b=%d ', a+b);a=3.700000e+000 , b=3 a+b=6.700000e+000

>> fprintf('a=%f , b=%d \n', a, b);fprintf('a+b=%d ', a+b);a=3.700000 , b=3 a+b=6.700000e+000

Page 4: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

Drawing a Rectangle Block

>> drawRectangleBlock (3, 4, 7, 4)

####### ####### ####### #######

Page 5: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

Algorithm

Given x, y, width, height :

go down y-1 lines

for the next height lines do

put x-1 space signs

print width ‘#’ characters

Page 6: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

Matlab program

function drawRectangleBlock(x, y, width, height)

for i=1:y-1 fprintf('\n');end

for i = y : y + height - 1 for j = 1 : x-1 fprintf(' '); end for j = x : x + width - 1 fprintf('#'); end fprintf('\n');end

Page 7: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

Draw a Pyramid

>> drawPyramid(3, 5, 5)

# ### ##### ################

Page 8: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

Algorithm

Given x, y, height :

go down y-1 lines

for line 1 to height do

put x - line space signs

print 2 * line - 1 ‘#’ characters

Page 9: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

Matlab Program

function drawPyramid(x, y, height)

for i=1:y-1 fprintf('\n');end

for line = 1:height for j = 1 : x - line fprintf(' '); end for j = 1 : 2 * line - 1 fprintf('#'); end fprintf('\n');end

Page 10: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

Another Wayfunction drawPyramid(x, y, height)

for i=1:y-1 fprintf('\n');end

noPieces = 1;noSpaces = x - 1;for line = 1:height for j = 1 : noSpaces fprintf(' '); end for j = 1 : noPieces fprintf('#'); end fprintf('\n'); noSpaces = noSpaces - 1; noPieces = noPieces + 2;end

Page 11: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

banner problem

• design a program that is much like unix command banner:

• each character is a 8x8 combination of ‘#’

Page 12: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

Drawing Rectangles

• Given a matrix where each row has the parameters of one rectangle, draw all the rectangles on screen

• A matrix with n rectangles looks like :[ x1 y1 width1 height1 x2 y2 width2 hegiht2… xn yn widthn heightn]

Page 13: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

Example

For m =

3 5 5 8 17 1 7 31 12 8 17 8 1 20 32 6

Page 14: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

Drawing Pyramids

• Given a matrix where each row has the parameters of one pyramid, draw all the pyramids on screen

• A matrix with n pyramids looks like :

[ x1 y1 height1

x2 y2 hegiht2

xn yn heightn]

Page 15: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

Pyramid Example

p =

5 5 4 11 3 10 20 7 9

Page 16: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

Pyramids Design

• For j = 1 to max y location do

– for i = 1 to max x location do

• isFull = false

• for all pyramids p do

– if p contains point (i, j)

» isFull = true

• if (isFull)

– print character ‘#’

• else

– print character ‘ ‘

– move to next line

Page 17: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

Rectangles Design ?

• the logic is very similar, except we ask whether current rectangle contains (i, j) we could have :

• for all shapes s do– if s is a pyramid

• if pyramid s contains (i, j) …

– else if s is a rectangle• if rectangle s contains (i, j) …

– …

Page 18: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

Max x location

• Given a matrix m of pyramids or rectangles

• For rectangles the max x value of a rectangle is x + width – 1– Create a vector of x + width -1 return the max

• max( m(:, 1) + m(:, 3) – 1)

• For pyramids: x + height – 2– Create a vector of x + height – 2, return max

• max( m(:, 1) + m(:, 3) – 2)

Page 19: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

Max y location

• Given a matrix m of pyramids or rectangles

• For rectangles the max y value of a rectangle is y + height – 1– Create a vector of y + height -1 return the max

• max( m(:, 2) + m(:, 4) – 1)

• For pyramids: y + height – 1– Create a vector of y + height – 1, return max

• max( m(:, 2) + m(:, 3) – 1)

Page 20: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

Max Location finding

• In all cases, given a matrix, we add up certain columns and find the maximum value of the addition

• Let’s implement a function to do that

Page 21: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

findMaxLocation

Given a matrix m and wo columns c1 & c2:

function maxLocation = findMaxLocation(m, c1, c2)

vals = m(:, c1);

deltas = m(:, c2);

totals = vals + deltas - 1;

maxLocation = max(totals);

Page 22: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

Test findmaxlocation function

p =

5 5 4 11 3 10 20 7 9

>> findMaxLocation(p, 1, 3)ans = 28

>> findMaxLocation(p, 1, 2)ans = 26

Page 23: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

function isInRectangle

function isinside = isInRectangle(i, j, x, y, width, height)

if (x <= i && i < x + width) && ...

(y <= j && j < y + height)

isinside = true;

else

isinside = false;

end

Page 24: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

function isInRectangle

• Since our rectangles are stored in vectors:

function isinside = isInRectangle2(i, j, rect)

isinside = isInRectangle(i, j, rect(1), rect(2), rect(3), rect(4));

Page 25: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

function isinside = isInPyramid(i, j, x, y, height)

% First check the bounding rectanglesx = x - height + 1;sy = y;width = 2 * height - 1;if ~(isInRectangle(i, j, sx, sy, width, height)) isinside = false; return;end

lineno = j - sy + 1;nospaces = height - lineno;nopieces = 2 * lineno - 1;

if ( i < sx + nospaces || i >= sx + nospaces + nopieces) isinside = false;else isinside = true;end

Page 26: Fprintf and other examples. fprint function >> a=2;b=3;c=5; >> fprintf('a = %d, b = %d, c = %d', a, b, c); a = 2, b = 3, c = 5 >> fprintf('a=%d, b=%d',

function printPyramids(pyramids, ch)

maxx = findMaxLocation(pyramids, 1, 3);maxy = findMaxLocation(pyramids, 2, 3);

for j = 1 : maxy for i = 1 : maxx isFilled = false; for pno = 1 : size(pyramids, 1) pyr = pyramids(pno, :); if (isInPyramid2(i, j, pyr)) isFilled = true; break; end end if (isFilled) fprintf(ch); else fprintf(' '); end end fprintf('\n');end