%% Homework Week 3 %% If and for statements % 1. Write a MATLAB program that will display the smaller of two numbers. The two numbers % should be read in from the user and the program should display the value of the smaller % number. If the two numbers are equal, either value can be displayed (i.e. don?t worry about % if the two numbers are equal). %% % 2. Using the program from exercise 1 as a starting point, write a MATLAB program that will % display the smallest of three numbers. The three numbers should be read in from the user % and the program should display the value of the smallest number. If the two or three % numbers are equal and are the smallest value, either value can be displayed. Hint: one way % of solving this problem is to first determine smaller of two of the numbers and then % compare that result to the third number. %% % 3. A worker is paid according to his hourly wage up to 40 hours, and 50% more for overtime. % Write a program in a script file that calculates the pay to a worker. % The program asks the user to enter the number of hours and the hourly wage. The program then displays the pay. %% % 4. Create a 4 × 8 matrix of uniformly distributed random numbers. % Loop through all rows and columns and test whether each element is greater than 0.5. % Save a variable that contains the indices of these numbers %% % 5. Write a MATLAB program that will display the numbers from 5 to 25 with a step of 5. The % program should display: 5, 10, 15, 20, and 25. Use iteration (for loop) to solve this problem. % Run your program and verify that it operates correctly. %% % 6. Modify the program of Exercise 5 so that the numbers from 25 to 5 with a step of -5 are % displayed. The program should display: 25, 20, 15, 10, and 5. Use iteration (for loop) to solve % this problem. Run your program and verify that it operates correctly. %% 7. Can you make an infinite loop using a for-loop? How about using an if-then construction? %% 8. What are the differences between loops A and B below? % Which will print out more messages in the Command window? Or will their outputs be the same? %% loop A i=0; while i<5 i=i+1; fprintf('%g ',i) end fprintf('\n') %% loop B i=0; while i<5 fprintf('%g ',i) i=i+1; end fprintf('\n') %% 9. Write a loop that generates a column vector A with ten random numbers. % Then create a 10 × 4 matrix having A as its first column, with the remaining columns % the product of the first column and the column number % a) Using for loops % b) Using while % c) what is the difference? %% 10. The MATLAB code below ensures that a number read in from the user will be % greater than zero. Modify the code below to ensure a number between 1 and 100 is % read in. % read in number number = input('Enter number greater than zero: '); % ensure number is greater than 0 while (number <= 0) number = input('Enter number greater than zero: '); end %% 11. Write a MATLAB program that will display the name of the month given an integer number % between 1 and 12 (1 for January, 2 for February, etc.). The number should be read in from % the user and the program should display the name of the month or an error message if the % number is not in the range of 1 to 12. %% 12. Write a MATLAB program that will display a menu with the salgados choices: coxinha, espeto de frango, % esfirra, e risolis; and display the cost of the salgado selected from the menu. The salgado costs % are: coxinha R$ 3.50, espeto de frango R$ 3.50, esfirra R$ 3.65, and risolis R$ 3.80. The menu can be generated % using the MATLAB statement choice=menu('Salgado Choices','coxinha','espeto de frango','esfirra','risolis'); % The menu generated is shown in Figure 1. The menu function will return an integer % corresponding to the number of the button selected. The variable choice will be assigned % the value corresponding to the menu button pressed.