This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 14:440:127– Introduction to Computers for Engineers Recitation 05 Problems Solutions Pertaining to LEC05 Notes Rutgers University, Spring 2011 Instructor  Brenda V. Cortez INSTRUCTIONS: • You can create a separate Mfile for each of the problems in your recitation problem sets, so that you can reference back to your answers later when you study. • Save all your work to your MATLAB Files directory in the J: Drive. • An alternative to separate MFiles, is to create a single Mfile for all the problems, and use %% to create a cell for each problem. Refer to my Video Tutorial on the topic of cells found in Video Tutorials > LEC04 Video Tutorials . • For all problems, always think about using good variable names, even if the problem doesn’t explicitly specify variable them; get used to defining your own. • Make sure you go through all the problems before the exam and ask any questions if you don’t understand why something works the way it does. • I suggest that you write simple pseudocode stating the algorithm you will use to solve each problem that requires a program. Then start typing your code. It may save from errors. PROBLEMS: 1. Rewrite the following if statement as a switch statement if(x==5  x==6) disp(‘one’) elseif(x==7) disp(‘two’) else disp(‘three’) end Answer: switch x case {5,6} disp(‘one’) case 7 disp(‘two’) otherwise disp(‘three’) end 2 2. Write Matlab code that finds the sum of all prime numbers from 211 to 300 , inclusive. ( inclusive means it includes 211 and 300 ). Display your answer, using fprintf , in the following format (inserting the correct answer from your calculations into the underlined por tion): “The sum of all primes from 211 to 300 is 4048 .” Answer: fprintf(‘The sum of all primes from 211 to 300 is %.0f\n’, sum(primes(300))  sum(primes(210))) %%%%% OR x = primes(300); fprintf(‘The sum of all primes from 211 to 300 is %.0f\n’, sum(x(x>=211))) %%%%% OR x = 211:300; fprintf(‘The sum of all primes from 211 to 300 is %.0f\n’, sum(x(isprime(x)))) 3. Assume that there already exists a matrix stored in the variable A . (To test your solution, you should make up your own matrix and store it as A ). Write MatLab code that, for any matrix stored in A , calculates and displays the sum of all composite numbers in that matrix. Note that composite numbers are those that are not prime (Hint: if you test if they are prime ( isprime ), Matlab returns “false”). Answer: sum(A(~isprime(A))) 4. Ask the user to input a number. Do the following: • If the number is a positive, prime integer, display “Prime!” • If the number is a positive, composite integer, display “Composite!” • If the number is a negative integer, display “Negative” • If the number is zero, display “Zero” • In all other cases, display “Not an integer” Answer: x = input(‘Enter a number:’); if(x~=fix(x)) disp(‘Not an integer’); elseif(x<0) disp(‘Negative’) elseif(x==0)...
View
Full
Document
This note was uploaded on 03/30/2011 for the course ENGINEERIN 127 taught by Professor Finch during the Spring '08 term at Rutgers.
 Spring '08
 Finch

Click to edit the document details