{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Recitation 6 Problems- Solutions

Recitation 6 Problems- Solutions - 14:440:127 Introduction...

Info icon This preview shows pages 1–3. 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 6- Solutions 1) Write a for loop that calculates 20! (20 factorial). You may not use the factorial( ) function. total=1; for x=1:20 % or 20:-1:1 total=total*x; end disp(total) 2) Write a while loop that continually asks the user to input a number. Each time he/she enters a positive number, you should display that number to the screen. Once the enter a number that’s not positive, the loop should stop, and you should display “Loop Stopped” to the screen. x=input( 'Enter a number' ); while (x>0) disp(x) x=input( 'Enter a number' ); end disp( 'Loop stopped' ) 3) Write a for loop that calculates the sum of the prime numbers from 1 to 1000. total=0; for x=1:1000 if (isprime(x)) total=total+x; end end disp(total) % without loops: sum(primes(1000)) 4) Write a while loop that finds the smallest 8 digit prime number. Then, instead write that loop as a for loop (using the break command). n=10000000; % smallest 8 digit number while (~isprime(n)) % loop until you find a prime n=n+1; end disp(n) for n=10000000:99999999 if (isprime(n)) break end end disp(n) 5) Use the following command to create a 10x10 matrix: C = round(500*rand(10)); Write a nested for loop that finds the maximum value in that matrix. C = round(500*rand(10)) z=size(C);
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
maxval=-inf; for r=1:z(1) % z(1) is the number of rows in C for c=1:z(2) if (C(r,c)>maxval) maxval=C(r,c); % set maxval = to current number end end end disp(maxval) % to write this without a nested loop
Image of page 2
Image of page 3
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