This preview shows page 1. Sign up to view the full content.
Unformatted text preview: ENGR 12600 Fall 2008 Laboratory 11 Task 1 Introduction to WHILE Loops (Estimated Time = 30 minutes) Task 1 You will learn to analyze flowcharts that contain loops for situations in which the number of executions is uncertain. You will then learn to construct simple while loops from the flowcharts. A. Consider this flowchart: Input x Is x more than 65? True Reduce x by 5. False Display x 1. What happens if the user inputs 78 as the initial value of x? On a separate sheet of paper, track the loop using the table format shown. Iteration Value of x 2. What value of x is displayed? ____________ 3. How many times does the loop operate? _______________ B. Open your MATLAB template file and rename it lab11task1a.m. Write the script that performs the logic in shown in the flowchart. 1. In the input section, use the input command to ask the user to enter a value for x. 2. In the calculation section, write the while statement shown in the flowchart. 3. In the output section, display the value of x using the fprintf command. C. Test your code using 78 as the initial value of x. What value is displayed? ENGR 12600 Fall 2008 Laboratory 11 D. Below is a more complex flowchart to evaluate. Task 1 Input x Is x over 50? False Is x below 45? True Increase x by 5 False True False Is x over 65? True Display x Decrease x by 5 1. What value of x is displayed when x is initially set to 78? 2. What value of x is displayed when x is initially set to 33? 3. What is the range of x values that will be printed? E. Open your MATLAB template file and rename it lab11task1b.m. Write the script that performs the logic in shown in the flowchart. 1. In the input section, use the input command to ask the user to enter a value for x. 2. In the calculation section, write the code for conditional logic and loops shown in the flowchart. 3. In the output section, display the value of x using the fprintf command. F. Test your code using 78 and 33 as the initial values of x. Record the for the following values, as comments, under the Output Section: Displayed Value: x = 78 x = 33 ENGR 12600 Fall 2008 Laboratory 11 Task 2 Introduction to FOR Loops (Estimated Time = 30 minutes) Task 2 You will construct an appropriate flowchart diagram for a for loop process and implement a simple for loop in MATLAB. A. Draw a flowchart of a for loop that (1) displays the current value of the loop index each time the loop executes. (2) displays the final value of the loop index after the loop has terminated. The flowchart should clearly depict the initialization of the starting, ending, and incrementing values of the for loop, as well as the appropriate flowaltering (decision) process(es) that take place. The for loop parameters (i.e. variables) to be assigned are: Start_Value, End_Value, Increment, and Index. These correspond to the starting value, ending value, incrementing value, and index, respectively, that are used to control the number of times the for loop is repeated. B. Test your flowchart, by hand, using the test cases shown below (track the loop). For each test case, track the number of times the for loop is executed and the value of the index during each execution of the loop. Record these values on a sheet of paper. Start_Value End_Value Increment # of times loop executes Final value of index C. Convert the flowchart you developed in step B to a MATLAB script file. 1. Open your MATLAB template file and save it as lab11task2.m. (remember to include appropriate comments) 2. Use the MATLAB input command to prompt the user to input values for Start_Value, End_Value, and Increment. 3. Run the five test cases from step B and compare your results to your hand solutions. Case 1 1 5 1 Case 2 9 3.5 1 Case 3 12 4 2 Case 4 17 28 2.6 ENGR 12600 Fall 2008 Laboratory 11 Task 3 FOR Loops that Perform Iterative Mathematical Operations (Estimated Time = 30 minutes) Task 3 You will construct an appropriate flowchart to complete iterative mathematical operations and then implement a for loop in MATLAB to perform the same iterative mathematical operations. n i A. In the mathematical expression: sum = , i =1 (1) (2) the summation operator (S) implies sum = 1 + 2 + 3 + .... + n . 1. Compute the value of sum given by eq. (1) by hand for the case when n is equal to 10. Sum = 2. Draw a flowchart to compute the value of sum given by eq. (1) when n is equal to 10. 3. Write a MATLAB script named lab11task3.m to perform the summation given by eq. (1). a. Use input values Start_Value = 1, End_Value = 10, and Increment = 1. b. Record your result below. How does it compare to your hand calculation? Sum = 1 1 B. Consider the summation of instead of simply summing i, such that sum = . (3) i i =1 i 1. Compute the value of sum given by eq. (3) by hand for the case when n is equal to 10. 2. Add the for loop to compute the value of sum given by eq. (3) after the loop from step A. a. Use input values Start_Value = 1, End_Value = 10, and Increment = 1. b. Record your result below. How does it compare to your hand calculation? n Sum = ...
View
Full
Document
This note was uploaded on 02/12/2012 for the course ENGR 126 taught by Professor Oakes during the Fall '08 term at Purdue University.
 Fall '08
 Oakes

Click to edit the document details