Georgia Institute of Technology College of Computing CS 1171 Computing for Engineers Final Exam - Fall Semester 2010 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 A A A A A A A A A B B B B B B B B B C C C C C C C C C D D D D D D D D D E E E E E E E E E Part Poss. Pts 20 25 50 40 20 20 20 20 215 Print your Prism ID (not 900 number!) in the spaces provided, and shade the boxes of the corresponding numbers and/or letters. I hereby signify that this examination paper contains my own work exclusively, and I have neither given nor received inappropriate help during the taking of this examination, in compliance with the letter and spirit of the Academic Honor Code of Georgia Tech. Name (print): F F F F F F F F F G G G G G G G G G H H H H H H H H H I I I I I I I I I J J J J J J J J J K K K K K K K K K L L L L L L L L L M M M M M M M M M N N N N N N N N N (cover) 1 2 3 4 5 6 7 8 O O O O O O O O O TOTAL P P P P P P P P P Q Q Q Q Q Q Q Q Q R R R R R R R R R S S S S S S S S S T T T T T T T T T U U U U U U U U U V V V V V V V V V W W W W W W W W W X X X X X X X X X Y Y Y Y Y Y Y Y Y Z Z Z Z Z Z Z Z Z Earned Pts - Lost Pts Grader Please Note: Failure to complete this front sheet correctly will cost you 5% of your grade. Please turn off (or silence) and put away any cell phones, beepers/pagers, personal radios or music players that you have in your possession. Page 1 of 17 Academic misconduct (including - but not limited to - examples on the list below) could result in a zero score on this examination, an “F” final grade in the course, and/or other disciplinary action: Failure to cooperate with or follow directions given by a proctor. Failure to stop writing when the allotted time is up (as reported by a proctor). Communication with anyone other than a proctor for ANY reason in ANY language in ANY manner. Sharing of ANYTHING (e.g. pencils, erasers, paper). Writing on paper that is not given to you by a proctor. Using cell phones, beepers, personal radios or music players, etc. during the exam. Using books or other reference material during the exam. Disruption of the exam setting. Reference Section: all(x) – checks if all of the elements of x are true any(x) – checks if any of the elements of x are true char(a) – returns the value of a as a string class(a) – returns the class of a csvread(filename) – reads in file of comma separated values csvwrite(filename, A) – writes A to .csv file cumtrapz(x, y) – performs a trapezoidal integration of y(x) diag(m) – returns the diagonal elements of the matrix m diff(x) – returns a vector of column differences of the vector x dlmread(filename, D) – reads in file with delimiter D dlmwrite(filename, A, D) – writes A to file with delimiter D double(a) – converts a to class double (numeric) factorial(n) – returns n! fieldnames(X) – returns a cell array of fieldnames find(m) – returns the indices of the true elements of m fopen(F, P) – opens file F with permission P getfield(X, F) – gets value in field F of structure X image(x) - display the image from the matrix x imread(filename) - returns a matrix representation of an image imwrite(x, filename) – writes the image x to the file: filename newy = interp1(x,y,newx) - interpolates to find newy, the values at the points in newx. isa(a, b) – checks if a is of class b ischar(a) – checks if a is of class char (a string) isnumeric(a) – checks if a is of a numeric data type(a number) isempty(here) – checks if here is null (usually represented by [], the empty vector length(a) – largest dimension of a lightangle(az, el) – shines a light on the plot from the specified direction (degrees) lower(str) – replaces all upper case letters in str with their lower case equivalent magic(n) – builds a n * n magic square [value where] = max(a) – value and index of the max value in a mean(v) – returns the average value of v [xx, yy] = meshgrid(x, y) – compute the plaid from the x and y vectors [value where] = min(a) – value and index of the minimum value in a mod(a, b) – the remainder when a is divided by b ones(rows, cols) – generate a matrix filled with 1 plot(x, y, S) – plots y versus x; S specifies line style plot3(x, y, z, S) – plots a 3-D line with S specified line style polyfit(x,y,o) – performs regression on x and y points to a given order o polyval(c,x) – returns dependent values of a polynomial with coefficients c at independent values x prod(v) – compute the product of all the elements in a vector v rand(n, m) – produces an n by m array of random numbers between 0 and 1 rem(a, b) – the remainder when a is divided by b Page 2 of 17 rmfield(S, X) – returns a structure with field X removed from S setfield(S, F, X) – returns a structure with field F added to S with value X sin(th) – sin of the angle in radians size(a) – all the dimensions of a sort(v) – arranges v in ascending numerical order sound(x, fs) – plays the sound x at a sampling frequency fs newy = spline(x, y, newx) - Performs cubic spline interpolation to find newy, the values at the points in newx. sprintf(format, <vars>) - Write formatted data to string strcmp(a, b) – Compare strings a and b strcmpi(a, b) – Compare strings a and b without regard to case struct(F, V, ….) – creates a structure with field F and values V subplot(rows, cols, index) – creates a grid of plots and fills in the indexed plot sum(a) – total all the elements one dimension of the array a title(str) -- places a text title on a plot upper(str) – replaces all lower case letters in str with their upper case equivalent [x, fs] = wavread(file) – gives the waveform and sampling frequency for a .wav file wavwrite(x, fs, filename) – write the vector x as a wav file: filename at a sampling frequency fs x/y/zlabel(S) – labels x/y/z axis with string S [nums txt raw] = xlsread(filename) – reads in .xls file xlswrite(filename, A) – write array A to .xls file zeros(rows, cols) – generate a matrix filled with 0 Page 3 of 17 CS1171 – Computing in Matlab Final Exam Fall, 2010 Problem 1 – Sorting [20 points] A. Sort the following vector using Quick Sort. You must show the vectors generated at each step, not the code involved. [10, 3, 7, 15, 2, Page 4 of 17 1, 13, 6, 42] CS1171 – Computing in Matlab Final Exam Fall, 2010 B. Consider the code fragments below. They are the “working” part of four sorting algorithms. Circle the names of each algorithm and the corresponding Big O. Careful – it’s just possible that some of the names might be deceptive. Working Code Sorting Algorithm function b = sort1(b) N = length(b); right = N-1; for in = 1:(N-1) for jn = 1:right if b(jn) > b(jn+1) b = merge(b, jn, jn+1); end end right = right - 1; end end function a = sort2(a, from, to) if (from < to) [a p] = insert(a, from, to); a = sort2(a, from, p); a = sort2(a, p + 1, to); end end function b = sort3(a) b = []; ndx = 1; sz = length(a); while ndx <= sz b = partition(b, a(ndx) ); ndx = ndx + 1; end end function b = sort4(a) b = a; sz = length(a); if sz > 1 pivot = floor(sz / 2); first = sort4(a(1:pivot)); second = sort4(a(pivot+1:sz)); b = swap(first, second); end end Page 5 of 17 Insertion Sort Bubble Sort Quick Sort Merge Sort Insertion Sort Bubble Sort Quick Sort Merge Sort Insertion Sort Bubble Sort Quick Sort Merge Sort Insertion Sort Bubble Sort Quick Sort Merge Sort Big O O(log N) O(N) O(N log N) O(N2) O(log N) O(N) O(N log N) O(N2) O(log N) O(N) O(N log N) O(N2) O(log N) O(N) O(N log N) O(N2) CS1171 – Computing in Matlab Final Exam Fall, 2010 Problem 2 – Problem Solving [25 points] Match the description in the left column to the formal name from the right column. The formal names will be used only once each. Answer Description Processing a spread sheet to construct a structure array Code Formal Name A Modularity B Abstraction C Data Abstraction Grouping data items together in a single, named collection D Procedural Abstraction Finding the lowest global temperature ever recorded E Insert F Build G Traverse H Map Packaging code that solve a specific problem and making that package available for re-use I Filter Doubling the odd values in an array of numbers J Fold Expressing a quality apart from a particular implementation K Search L Sort Taking a dollar bill out of your billfold Dropping a coin into a piggy bank Alphabetizing a pile of student test papers Ensuring that functions have one singular purpose Backing up all the files on your hard drive Taking all the dollar bills out of your billfold Page 6 of 17 CS1171 – Computing in Matlab Final Exam Fall, 2010 Problem 3 – Multiple Choice [50 Points] 1. What is the effect of the function imager given the image matrix img? function [ret] = imager(img) img = double(img); val = uint8((img(:,:,1)+img(:,:,2)+img(:,:,3))/3); ret(:,:,1) = val; ret(:,:,2) = val; ret(:,:,3) = val; A. B. C. D. E. ret is a black and white image ret is a grayscale image ret is twice as large as img ret is the same as img None of the above 2. Given the following lines of code, which of the following operations would produce the same sound? [x, fs] = wavread('mysound.wav') sound(x, fs) A. B. C. D. E. x = x(round(linspace(1, length(x), 2*length(x)))) sound(x, fs/2) x = 2*x sound(x, fs/2) x = sound(x, fs*2) sound(x, fs/2) x = x(round(linspace(1, length(x), 2*length(x)))) sound(x, fs*2) x = x/2 sound(x, fs/2) 3. Given the following Matlab code: A = true; B = false; C = (A || B) && ~(B && B) if C output = 'almost there' else output = 0 end What is the class of output? A. char B. double C. logical D. cell E. Error Page 7 of 17 CS1171 – Computing in Matlab Final Exam 4. Given the following: x = [0 1 2 3 4] y = [0 2 4 6 8] plot(x, y, 'ro') What would the plot look like? A. A solid red line connecting the given x and y values B. A plot of red circular points with the given x and y values C. A curved fit for the given x and y values D. Error E. None of the above 5. Convert the following while loop to a for loop. vec = [1 2 3 4]; sum = 0; while length(vec)>0 sum = sum + vec(1); vec(1) = []; end A. for vec sum = sum + vec(i); end B. for i = vec sum = sum + vec(i); end C. for i < length(vec) sum = sum + vec(i); i = i + 1; end D. for i = vec sum = sum + i; end E. for i = length(vec) sum = sum+vec(i); end 6. Which of the following is a valid function header? (mark all that apply) A. function = fx(input) B. function ret1, ret2 = fx(input) C . function ret = fx(34) D. function fx E. [ret1, ret2] = fx(input1, input2) Page 8 of 17 Fall, 2010 CS1171 – Computing in Matlab Final Exam 7. Given: vec = [5 9 6 3 6 8] Which of the following will NOT return the vector v2 = [5 6 3 6 8]? A. v2 = vec(vec < 9) B. v2 = vec([1 3:6]) C. v2 = vec; D. E. v2(vec == max(vec))=[] v2 = vec(vec ~= 9) v2 = vec; v2(vec > 9)=[] 8. Given : x = linspace(1,10,100) y= x.^3 y1 = diff(y)./diff(x) Which of the following will plot the second derivative? A. plot(x(2:end), diff(y1)./diff(x(2:end))) B. plot(x(3:end), diff(y1)./diff(x(2:end))) C. plot(x(2:end), diff(y1)./diff(x)) D. plot(x(2:end), diff(y1(2:end))./diff(x(2:end))) E. plot(x, diff(y1)./diff(x(2:end))) 9. Which choice below is NOT true about the following line of code? f1 = fopen('index.txt', 'w') A. The file handle for ‘index.txt’ is stored in the variable f1 B. The text from ‘index.txt’ is stored in the cell array f1 C. The file has been opened with read only permission D. Both A and C E. Both B and C 10. Given: cell = {'cool', 1:4, false} cell{2} = [] What is the final value of cell? A. {'cool', [], false} B. {'cool', false} C. {'cool', {}, false} D. {'cool', [1 3 4], false} E. Error Page 9 of 17 Fall, 2010 CS1171 – Computing in Matlab Final Exam Fall, 2010 Problem 4 – Coding Problems [40 Points] 1. You are given a sound of a piano playing a C-note – 'cNote.wav'. Determine the number of seconds the sound plays for and store in the variable tnote. Then, plot the amplitude of the sound versus time. 2. Write a MATLAB function called makeGTEmail that consumes two parameters: A character array representing the name of the person A domain representing the department the person is associated with. The rest of the email will always be gatech.edu. Any spaces in the name of the person should become a “.” in the email address. The function should return the character array of the newly formed email address. Assume any characters in the email address will always be lowercase. Examples: makeGTEmail(‘david smith’, ‘cc’); should return: ‘david.smith@cc.gatech.edu’ makeGTEmail(‘george p burdell’, ‘ece’); should return: ‘george.p.burdell@ece.gatech.edu’ makeGTEmail(‘clough’, ‘pres’); should return: ‘clough@pres.gatech.edu’ Page 10 of 17 CS1171 – Computing in Matlab Final Exam Fall, 2010 3. Write a script to solve the following equations: 4x – 13y + 2z = 8 5y = z x – 5z = 42 4. Given vectors x and y of equal length containing x and y data values, please do the following: - Compute the coefficients for a third order fit of the data and store in C - Create a new y vector called newy using this fit and the original x values - Compute the cumulative integral of newy using the trapezoidal rule - Plot the integral versus the x values Page 11 of 17 CS1171 – Computing in Matlab Final Exam Fall, 2010 Problem 5 – Images [20 Points] I. There are four images to the right, labeled A-D. Below is a list of transformations you can perform on the image A. 1. Flip columns 2. Flip rows 3. Transpose In order to create each of the images B-D, write which transformations you will use starting with A, in the appropriate order. Your answer might be 3, 2, 1, for example. Image Transformation(s) B C D II. Consider the following "image" (assume it is a perfect square), saved under the file 'myplane.jpg': And the following code: b = imread('myplane.jpg'); [n,m,l] = size(b); a = b(1:end, 1:n/2, :); c = b(1:end, (n/2 + 1):end, :); b = [c; a]; image(b); Which of these will the resulting picture most resemble? A B C D Page 12 of 17 E CS1171 – Computing in Matlab Final Exam Fall, 2010 Problem 6 – Numerical Methods [20 Points] I. The contents of the Excel spreadsheet myData.xls are shown below. [out1 out2 out3] = xlsread('myData.xls'); A = out1(:, 1)'; B = out1(:, 2)'; C = out1(:, 3)'; D = out1(:, 4)'; [r1 c1] = size(out1); [r2 c2] = size(out2); [r3 c3] = size(out3); S1 = r1*c1; S2 = r2*c2; S3 = r3*c3; As you can see, data for July (month 7) is missing from the excel file. Using the variables A, B, C, and D from above (write 1-2 lines of code ONLY): 1. Find the value of Quantity A for July using linear interpolation and store in Q1: 2. Find the value of Quantity B for July using a quadratic fit of the data. Store in Q2: 3. Find the value of Quantity C for July using spline interpolation and store in Q3: 4. What would be the value of x1 = diff(out1(4, :))? 5. Fill in the blanks with the appropriate symbol (>, <, or =) S1________ S2; S2_________S3; S1________S3; Page 13 of 17 CS1171 – Computing in Matlab Final Exam Problem 7 – Structure Arrays [20 Points] You are given the following code: teams = {'Duke','Georgia Tech','Miami', ... 'North Carolina','Virginia','Virginia Tech'}; conference_wins = {0,4,2,3,6,7}; ACC = struct('team',teams,'wins',conference_wins); for index = 1:length(ACC); ACC(index).losses = 8-ACC(index).wins; end array = [ACC.losses]; [x y] = min(array); rmfield(ACC,'losses'); ACC(5).coach = 'Al Groh'; A = ACC(y).team; B = getfield(ACC(3), 'losses'); C = fieldnames(ACC(1)); D = setfield(ACC(2), 'mascot', 'Buzz'); What are the values of the following variables after the code is run? A = __________________________________________ B = __________________________________________ C = __________________________________________ D = __________________________________________ Page 14 of 17 Fall, 2010 CS1171 – Computing in Matlab Final Exam Fall, 2010 Problem 8 – Plotting [20 Points] Consider the graphs and code, then answer the following questions. NOTE: On the body of rotation, the faces are white and the lines are different colors. Given the two vectors u and v, some of the code for these two plots was as follows: th = linspace(0, 4*pi, 100); [uu tth] = meshgrid(u, th); [vv tth] = meshgrid(v, th); 1. The function used to plot the body of rotation was: A. meshgrid() B. surf() C. mesh() D. plot3() 2. Which variable was chosen as the radius parameter, rr? A. u B. v 3. Which of these variables was NOT defined by either rr.*cos(tth) or rr.*sin(tth)? A. xx B. yy C. zz D. None of the above 4. What was the value of that other variable? A. uu B. vv Page 15 of 17 C. rr D. None of the above CS1171 – Computing in Matlab Final Exam Fall, 2010 5. Using u, v, th, uu, vv, and/or tth, complete the code to rotate the data about the z-axis. However, you must plot this body of rotation as a colored surface with smooth color gradients and a light shining from 45o in azimuth and elevation. You should not need to write more than 10 lines of code. Page 16 of 17 CS1171 – Computing in Matlab Final Exam This page deliberately left blank Anything you do write here may be used in evidence against you Page 17 of 17 Fall, 2010