Partial preview of the text
Download Exam 1 with Solved Problems - Computing for Engineers | CS 1371 and more Exams Computer Science in PDF only on Docsity!
CS1371 - Computing for Engineers Test 1.Version G February 11", 2005 Problem 1 — Functions The United States Postal Service has given you the task of calculating the "check digit" for its five-digit zip codes. The check digit is calculated by adding up the digits of the five-digit zip code and seeing what would have to be added to the sum to get a multiple of 10. For example, the check digit for the zip code 51220 would be 0 (as 5+ 1+2+2+0= 10), while the check digit for 82125 would be 2 (as8+2414+2+5=18). Write a Matlab function called checkDigit that takes in a vector of zip code digits that range from 0 to 9 and returns the check digit. You also need to check that the inputted zip code is a valid zip code — i. e., that it is of length 5. If it is not a valid zip code, return -1. Examples: checkDigit([9 8 0 3 4]) should return 6. checkDigit([7 2 1 4]) should return -1. vee som 2p = checkbigit(a bio, Ash itrd, e) Fe ¥* sun Cee ) Tene . bees mad. (yer, te) ¢ mod CY) z= 6 if lorgh ved 5 £ mod (yey, to) le +7 Zipe O el mad . (+ mod Cys Biither o Se € mod Cyt, wz: PURO, TD); Be = B ee | ele Aye ie wrod (yee to) z.9 a Zech end else ct wed Cyd ade zo Zier ge ele Rod (yie tg) 22 Lie =H else 4 aaa ty *.19) = <0 AB CD E’°F GHitI id -3 +3 Page 3 of 9 $1371 - Computing for Engineers Test 1 Version G February 11", 2005 Problem 2 — Multiple Choice A) In WebWork, suppose you upload and submit 4 files for your homework, named as follows: - circlem - square.m - triangle.m - zebram You then realize you meant to turn in trapezoid.m instead of zebra.m. What is the correct process for fixing this? a) Upload and submit only trapezoid.m, and do wwhedve any files or they will be erased. db) Upload and submit only frapezoid.m; then retrieve the file(s) to be sure you submitted properly. ¢) Upload and submit the 4 correct files, and do not retrieve any files or they will be erased. Upload and submit the 4 correct files again; then retrieve all the files to be sure you submitted properly. B) Consider the following vector in Matlab: vec = [10000], Which of the following will produce an error? JS a) vec(6) = 1; b) vee(1) = [h c¢) vec([3:-1:1]) = 1; Q=0 =1; @) vec = [[6 [5] vec]; C) The two lines below are entered at the command prompt: Ns 2bs to x=[930638] y = mod((sqrt(length(((x+2),*[1 2 3 45 6}))*6)),3) What is the value of y? \y 10 G0 . b)3 c)2 d)5 A_B C DEF GHIdIJ Page 4 of 8 C$1371 - Computing for Engineers Test 1 Version G February 11", 2005 Problem 3 - Scripts The Taylor Polynomial is a powerful mathematical tool, which helps approximate the value of various functions within a reasonable range of approximation. For example, it helps in the calculation of a trigonometric function like tanh! (x), known as the inverse hyperbolic tangent. The Taylor Polynomial of degree n for tanh (x), for any x, is given by: 3 Soy? x? en tanh? (x) 2 xt 4445 4.+ Maxt yt st yt n=l Using n = 10, write the Matlab commands below to calculate the value of tanh (5) (x=5, in the formula). Your final answer should be stored in the variable itanhSapprox. You MAY NOT use iteration. Hints: 1, OfIs1 2. You MAY use the built-in function sim 3. You could start by creating a vector of the exponents. X=S nz1o AD ; y 7 los (Pe-V) Lo At x." y. wa b: aly ABODE FG HI pW) Page 6 of 9 CS1371 - Computing for Engineers Test 1 Version G Problem 4- Booleans and Conditionals The code for the function magicCarpetRide is shown below. function answer = magicCarpetRide(x,y,z) ifx&&z if @ll-y) answer = ‘Last night I held Aladdins lamp’; elseif ~y answer = ‘Let the sound take you away’; else answer = ‘Any place it goes is right’; end elseif y|[z ifz answer = "You dont know’; else answer = ‘Right between the sound machine’; end else answer = "Why dont you tell your dreams to me?’ end What are the values of A,B,C, and D? A = magicCarpetRide(1,0,1); A= Lash otal % Vel) Bled) ing lent B = magicCarpetRide(1,0,0); B=_Wwhy dank jm Act yanc BP eetms bo meh C = magicCarpetRide(0,0, 1); CH! Mon do} one D = magicCatpetRide(1,1,0); ¢ D=_ Rink bebvece 44, found madeing ee February 11", 2005 AB C D EF GH J Page 7 of 9 CS1371 - Computing for Engineers Test 1 Version G February 11°, 2005 This page deliberately blank for extra work A BC DEFGHasId Page 9 of 9