{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

REC07+Problems+Set+Solutions

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

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
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'); while(~strcmp(g,'M') & ~strcmp(g,'F')) 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 find?'); for k = 1:length(A) if(A(k)==x) fprintf('%.0f is at element %.0f\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
Image of page 2
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.
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern