Recitation 9 Problems- Solutions

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

Info iconThis 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 9. SOLUTIONS 1) Write a function called “dice” that simulates the throw of two dice by returning a vector containing 2 random integers between 1 and 6. function v = dice( ) v = ceil(6*rand(1,2)); 2) Write a function that outputs the 9th prime number in a vector. The vector is 121:1:4343. You cannot use the primes( ) function, although you certainly may use the isprime( ) function. Save the following as output9th.m function ninth = output9th(v) ninth=-inf; count=0; x=0; while x=x+1; if (isprime(v(x))) count=count+1; end end if (count==9) ninth=v(x); end Then type the following in the workspace: v = 121:1:4343; output9th(v) 3) Write down the basic structure of 'for' and 'while' loop. (You don't need specify variable names). What are the major differences between the two methods? for VARIABLE = VECTOR STATEMENTS end while(CONDITION) STATEMENTS end For specifies exactly how many times it will loop, whereas while loops while the condition continues to be true. 4) Use for loop and the following formula to calculate π /4: s=0; for n=1:10000 s = s+1/(4*n-3) - 1/(4*n-1); end disp(s)
Background image of page 1

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

View Full DocumentRight Arrow Icon
5) Write Matlab code that performs the basic tasks of an ATM machine. The starting account balance is $500. Ask the user for a PIN, and accept any four digit number. Ask the user whether he/she would like to deposit funds, withdraw funds, or display the account balance (using the menu command). Include a fourth menu item, “Return Card.” If the user is depositing, ask how much to deposit and add it to the account balance. If the user is withdrawing, allow withdrawing if there is enough money in the account and subtract it from the account balance. If there are insufficient funds do not allow withdrawal and display an error message. Keep displaying the menu after each transaction (this allowing additional transactions) until the user chooses “Return Card.” At that point, use fprintf to display the final account balance. balance=500; p = input( 'Enter your pin, please: ' ); if while (1) m = menu( 'Please choose one:' , 'Deposit Money' , 'Withdraw Money' , 'Check Account Balance' , 'Return Card' ); switch m case 1 d = input( 'How much do you want to deposit? ' ); if (d>0) balance = balance+d; else disp( 'Invalid' ) end case 2 d = input( 'How much do you want to withdraw? ' ); if balance = balance-d; else disp( 'Invalid Number of Insufficient Funds' ) end case 3 fprintf( 'Your balance is %.2f\n' ,balance); case 4 break end end fprintf( 'Your balance is %.2f. If you''re lucky, this bank will still be in business tomorrow\n' ,balance); else disp(
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/29/2012 for the course 440 127 taught by Professor Blase during the Fall '09 term at Rutgers.

Page1 / 7

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online