REC07+Problems+Set+Solutions

# REC07+Problems+Set+Solutions - 14:440:127- Introduction to...

This preview shows pages 1–4. Sign up to view the full content.

14:440:127- Introduction to Computers for Engineers Problems for students to attempt in Recitation 7 1. Write a for loop that prints out the decimal equivalent of the reciprocals 1/2, 1/3, 1/4, . .. , 1/19, 1/20. Then, rewrite this without using a loop, and use tic/toc to see which is faster. tic for x = 1:20 disp(1/x) end toc tic x = 1./(1:20) toc 2. Find the sum of the geometric series: 1 + 2^1 + 2^2 +. ..+ 2^10 using a for loop. Then, rewrite this without using a loop, and use tic/toc to see which is faster. tic total = 0; for x = 0:10 total = total+2^x; end disp(total) toc tic x = 2.^(0:10); disp(sum(x)) toc 3. Display the values of x^3 for all positive integer values x such that x^2 is less than 2000 x = 1; while(x^2<2000) disp(x^3) x = x+1; end

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4. Take a user input - Please enter your gender. Use input( ʻ ʼ , ʼ s ʼ ), which expects a string input. If the entered character is M or F, print 'Thanks'. Otherwise, say 'Incorrect' and use a while loop to keep on asking for input until correct. g = input('Please enter your gender', 's'); disp('Incorrect') g = input('Please enter your gender', 's'); end 5. Sequential Search Algorithm: Given a vector A of arbitrary length, write a simple code to search for the 1st occurrence of a particular number. x = input('Which number do you want to fnd?'); ±or k = 1:length(A) i±(A(k)==x) ±print±('%.0± is at element %.0±\n',x,k) break end end 6. What sequence of numbers will the following for loop print? n = 10; for k = 1:n n = n-1; k end Explain the behavior of this program. k = 1 k = 2 k = 3 k = 4 k = 5 k = 6 k = 7 k = 8 k = 9 k = 10
7 . Write a loop to test if a given number is a prime. Use the naive primality test method, i.e. to test N, check if any of the number that are in [2, sqrt(N)] divides N evenly. % assume X already contains a positive integer

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/24/2011 for the course PHYSICS 123 taught by Professor Madey during the Spring '08 term at Rutgers.

### Page1 / 9

REC07+Problems+Set+Solutions - 14:440:127- Introduction to...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online