Spring 2001 CS100M Exercise E11: A Picture is Worth a Thousand Words Due: online 8am, Thursday 3/15 This exercise is to give you some practice with 2-dimensional array indexing ("indexing" = "accessing elements/positions in an array"). =============================================================================== task =============================================================================== Complete the "skeleton" code provided at the end of this write-up to make function $e11(m)$ take a positive integer $m$ in the range 10..30 and return an $m$-by-$2m$ string matrix $s$ with: $*$s on the border (left, right, top, and bottom edges) $\$s on the diagonal, except for on the border (the *diagonal* of a matrix = all valid positions (i,i) in the matrix) $-$s below the diagonal, except for on the border $+$s on the edge, $@$s in the interior, and $c$ at the center of a disk of radius $round(m*.35)$ and center $(round(m*1.5),round(m/2))$ Note on mathematical terminology: + a *circle* is just the "edge"; it does not include the points inside. + a *disk* = the points inside a circle, possibly including the circle itself. Hints + if pressed for time (e.g. studying for Prelim T2), you should still do this exercise, but you might want to skip dealing with the disk. + examples for $m=10$, $m=20$, and $m=30$ are shown below + you can overwrite values: e.g. place $\$s along the entire diagonal, including positions $(1,1)$ and $(m,m)$, and then afterwards place $*$s on the border. + the distance between positions $(x1,y1)$ and $(x2,y2)$ is $sqrt((x1-x2)^2 + (y1-y2)^2)$. + draw a $+$ at each position $(i,j)$ whose distance from $(xc,yc)$ is in the range $]r-.5,r+.5[$. Bonus: include the word "bonus" in a comment in your code and do either/both: + [1 bonus pt] Include coordinate labels as part of $s$, as shown in the examples below. Hint: tranposition ($'$) might be useful. + [1 bonus pt] Use logical arrays and other vectorization techniques to write concise code: each comment in the skeleton can be implemented without loops in 1 or 2 lines. Hint: $x$, $y$ should be very useful. =============================================================================== examples =============================================================================== note: coordinates are shown for clarity -- you are NOT required to include coordinates as part of $s$! e11(10): 11111111112 12345678901234567890 1 ************+++++*** 2 *\ooooooooo++@@@++o* 3 *-\ooooooo++@@@@@++* 4 *--\oooooo+@@@@@@@+* 5 *---\ooooo+@@@c@@@+* 6 *----\oooo+@@@@@@@+* 7 *-----\ooo++@@@@@++* 8 *------\ooo++@@@++o* 9 *-------\ooo+++++oo* 10 ******************** e11(20): 1111111111222222222233333333334 1234567890123456789012345678901234567890 1 **************************************** 2 *\ooooooooooooooooooooooooooooooooooooo* 3 *-\oooooooooooooooooooooooo+++++ooooooo* 4 *--\ooooooooooooooooooooo++@@@@@++ooooo* 5 *---\ooooooooooooooooooo+@@@@@@@@@+oooo* 6 *----\ooooooooooooooooo+@@@@@@@@@@@+ooo* 7 *-----\oooooooooooooooo+@@@@@@@@@@@+ooo* 8 *------\oooooooooooooo+@@@@@@@@@@@@@+oo* 9 *-------\ooooooooooooo+@@@@@@@@@@@@@+oo* 10 *--------\oooooooooooo+@@@@@@c@@@@@@+oo* 11 *---------\ooooooooooo+@@@@@@@@@@@@@+oo* 12 *----------\oooooooooo+@@@@@@@@@@@@@+oo* 13 *-----------\oooooooooo+@@@@@@@@@@@+ooo* 14 *------------\ooooooooo+@@@@@@@@@@@+ooo* 15 *-------------\ooooooooo+@@@@@@@@@+oooo* 16 *--------------\ooooooooo++@@@@@++ooooo* 17 *---------------\oooooooooo+++++ooooooo* 18 *----------------\ooooooooooooooooooooo* 19 *-----------------\oooooooooooooooooooo* 20 **************************************** e11(30): 111111111122222222223333333333444444444455555555556 123456789012345678901234567890123456789012345678901234567890 1 ************************************************************ 2 *\ooooooooooooooooooooooooooooooooooooooooooooooooooooooooo* 3 *-\oooooooooooooooooooooooooooooooooooooooooooooooooooooooo* 4 *--\ooooooooooooooooooooooooooooooooooooo+++++++ooooooooooo* 5 *---\oooooooooooooooooooooooooooooooooo++@@@@@@@++ooooooooo* 6 *----\ooooooooooooooooooooooooooooooo++@@@@@@@@@@@++ooooooo* 7 *-----\ooooooooooooooooooooooooooooo++@@@@@@@@@@@@@++oooooo* 8 *------\ooooooooooooooooooooooooooo++@@@@@@@@@@@@@@@++ooooo* 9 *-------\oooooooooooooooooooooooooo+@@@@@@@@@@@@@@@@@+ooooo* 10 *--------\oooooooooooooooooooooooo+@@@@@@@@@@@@@@@@@@@+oooo* 11 *---------\ooooooooooooooooooooooo+@@@@@@@@@@@@@@@@@@@+oooo* 12 *----------\ooooooooooooooooooooo+@@@@@@@@@@@@@@@@@@@@@+ooo* 13 *-----------\oooooooooooooooooooo+@@@@@@@@@@@@@@@@@@@@@+ooo* 14 *------------\ooooooooooooooooooo+@@@@@@@@@@@@@@@@@@@@@+ooo* 15 *-------------\oooooooooooooooooo+@@@@@@@@@@c@@@@@@@@@@+ooo* 16 *--------------\ooooooooooooooooo+@@@@@@@@@@@@@@@@@@@@@+ooo* 17 *---------------\oooooooooooooooo+@@@@@@@@@@@@@@@@@@@@@+ooo* 18 *----------------\ooooooooooooooo+@@@@@@@@@@@@@@@@@@@@@+ooo* 19 *-----------------\ooooooooooooooo+@@@@@@@@@@@@@@@@@@@+oooo* 20 *------------------\oooooooooooooo+@@@@@@@@@@@@@@@@@@@+oooo* 21 *-------------------\oooooooooooooo+@@@@@@@@@@@@@@@@@+ooooo* 22 *--------------------\ooooooooooooo++@@@@@@@@@@@@@@@++ooooo* 23 *---------------------\ooooooooooooo++@@@@@@@@@@@@@++oooooo* 24 *----------------------\ooooooooooooo++@@@@@@@@@@@++ooooooo* 25 *-----------------------\oooooooooooooo++@@@@@@@++ooooooooo* 26 *------------------------\ooooooooooooooo+++++++ooooooooooo* 27 *-------------------------\oooooooooooooooooooooooooooooooo* 28 *--------------------------\ooooooooooooooooooooooooooooooo* 29 *---------------------------\oooooooooooooooooooooooooooooo* 30 ************************************************************ =============================================================================== skeleton code for function $e11$ =============================================================================== function s = e11(m) % s = e11(m): return a $m$-by-$2m$ string matrix $s$ with % + $*$s on the border (left, right, top, and bottom edges) % + $\$s on the diagonal, except for on the border % (the *diagonal* of a matrix = all valid positions (i,i) in the matrix) % + $-$s below the diagonal, except for on the border % + $+$s on the boundary, $@$s in the interior, and $c$ at the center of % a disk of radius $round(m*.35)$ and center $(round(m*1.5),round(m/2))$ n = 2*m; % width of $s$ % [3/14] fixed typo below, where $xc<-->yc$ xc = round(m*1.5); yc = round(m/2); % (yc,xc) = center of disk r = round(m*.35); % radius of disk % $x$, $y$: very useful for vectorization bonus, but probably no help otherwise y = repmat((1:m)', 1, n); % y(i,j) = i = the row position (i,j) is in x = repmat(1:n, m, 1); % x(i,j) = j = the col position (i,j) is in s(m,n) = ' '; % make $s$ large enough s(:,:) = 'o'; % fill each element with $'o'$ % place $-$s below diagonal % ******** your code goes here ******** % place $\$s on diagonal % ******** your code goes here ******** % place $*$s on border % ******** your code goes here ******** % place $@$s inside disk % ******** your code goes here ******** % place $+$s on disk % ******** your code goes here ******** % place $c$ at center of disk (done for you:) s(yc, xc) = 'c'; =============================================================================== submission =============================================================================== Submit online at the Exercise Submission site by 8am of the due date.