Homework #6: CSE 7, Winter 2015 Before beginning this homework, create a new Notepad++, H6_LastName.txt, file in your cs7wxx home directory If you are completing Homework#6 with a partner, following the sftp instructions from HW#1. To determine your cs7wxx account name, see UCSD ACMS account lookup: https://sdacs.ucsd.edu/~icc/index.php PARTNER PROGRAMMING (Refer to Homework#1 – password privacy and saving Partner2’sfiles) Please name your document "H6_Partner1Lastname_Partner2Lastname.txt". Be sure you have both partners' names, cs7wxx names, and PIDs at the top of your document. Please record your answers in Notepad++ txt files only. You must save Partner2’s file in their ieng6 home directory as well. You must use different partners for EACH lab and homework assignment. PART ONE: While-loops 1. Let’s practice a little bit with while loops. While loops are another form of iteration that allow the same set of instructions to be repeated multiple times. Just like for-loop, while loops execute instructions that are in the loop body multiple times. UNLIKE for-loops, while loops do not execute a fixed number of times. Instead, as the name implies, while loops continue while some condition is true. 2. For example, enter the following code into a script file called whiles.m : 3. The above code first creates a variable called my_mat that contains some numbers and another variable called i that is set to 8. Then, the while loop begins! The first thing that happens is MATLAB checks the condition to see if it is true. 4. So in this case, it checks to see if i > 0. Since 8 > 0, MATLAB starts executing the code in the body of the loop. The body of the loop in this case contains two statement: my_mat[i] and i = i – 1 meaning that the variable i is decremented, and after the first time through the loop, the value of i will be 7. 5. When the end of the while loop body is reached, MATLAB will go back up to the top of the while loop and check the condition statement. If the condition is still true, the body will execute again. 6. So at iteration 2, MATLAB checks to see if i > 0. i is equal to 7, and since 7 > 0, the loop body executes again. Iteration Number 1 2 3 4 5 6 7 8 Starting i value 8 7 6 my_mat[i] Condition Value Ending i Value 15 13 True True 7 6 7. QUESTION #1 In Notepad++, fill in the table, by separate values with TAB character. So for iterations 1 and 2, you would type 1 8 15 True 7 2 7 13 True 6 8. QUESTION #2 At the completion of the loop, what is the value of i? 9. You don’t have to write this part in Notepad++, but can you guess what the loop is doing without looking at the explanation below? 10. Why is that useful you might ask? Imagine a scenario where you need to go through a list from the end instead of from the start. For example, a list of names is listed in alphabetical order. I would waste a lot of time if the name that I want to find starts with a W like William. Therefore searching from the end of the list will drastically save a lot of time. 11. Suppose now I want to go through the list from the beginning… 12. QUESTION #3 What do you think you need to change in the loop now? Go ahead and modify the code in the script so that my_mat reads from the beginning. 13. So now what if we write a new buggy loop where we want to simply print out the numbers from 1 to 50. Add the following lines of code to your script, and just execute the selected lines by highlighting them, right-clicking and selecting Evaluate Selection. 14. This is appropriately called an infinite loop, because you guessed it, the loop body continues to execute forever (infinitely). Press Ctrl and C together to stop the infinite loop. 15. QUESTION #4 Modify the loop condition so that the loop only prints out the numbers from 1 to 50. Write the new condition down as the answer to this question. PART TWO: Diving AND LOOPS 1. On the course webpage, we have a csv file titled dive.csv. CSV stands for comma separated values and it is a format of data that works really well with MATLAB. Make sure to download the file and store it into your MATLAB folder. We will NOT be using it in the function definition, but we will need it later to test out our function 2. Let’s get started by creating a function in MATLAB, called find_winner that takes in a single input parameter called score_data. The function is going to find and return the candidate number of the person who has the highest score. 3. score_data has two columns. The first column is going to contain the candidate number for each athlete, and the second column is going to contain their dive scores. 4. Our function header is going to have the following structure function [index , candidate_num ] = find_winner(score_data) 5. Recall that the orange part represents the output arguments, the purple represents the function name and the green represents the input argument 6. So what goes in the function body? Well we are going to use a while loop to find the index of the maximum score. 7. Tricky tricky. We have a while loop AND condition. Let’s see if we can figure out what the while loop is doing. 8. QUESTION #5 For this assignment, what should i be initialized to? Write it in English, do NOT write the code. 9. Now what is happening with the if statement. The if statement returns true if the score for the current athlete is greater than the maxScore. During the first iteration, maxScore is set to -999, so the condition evaluates to true. Then, inside the if-body, the variable maxScore is reassigned to whatever the first score was, and the variable index is set to 1. 10. This process is repeated until the all rows have been looped over, when maxScore reaches its largest value, the if condition will never evaluate to true again, so the value of index will stay fixed at the value associated with the maxScore. 11. After all rows have been considered, whatever was inside of index should contain the index of the athlete with the highest score. The last line of the function assigns the output variable called candidate_num to the correct index. 12. A bit confused? Imagine a simpler case, where you have a matrix that looks like: 1 2 3 100 1 1 13. The first time through the loop, maxScore will get set to 100 (remember scores are in the second column shown in brown), and after that, the if condition will never evaluate to true again. PART THREE: TESTING OUR FUNCTION AND REUSE 1. So how do we test it? Just like all the other functions we’ve written in this class, we have to call it to get it to work. 2. Our function expects a single input that it calls score_data. You should have already downloaded the dive data from the class website. Make sure to put it in the MATLAB folder. In order to use it, we have to load it into MATLAB. Fortunately MATLAB has a simple command called load that allows us to load the data. 3. QUESTION #6 What is the candidate number of the gold medalist? 4. Cool, we wrote a useful function using a while loop. But what if we wanted to find the silver and bronze medalist. Another really GREAT idea in programming is to reuse functions that we’ve already written. Our function returns the best performer of the dataset AND the index. Can you think of a way to reuse the function to get the second best performer, the silver medalist. 5. Well, here’s the cool part. If we just remove the gold medalist from the dataset, we can simply call our same function again to get the silver medalist (because the silver medalist will now be the best performer in the new dataset). Give it a try: 6. QUESTION #7 Who won the silver medalist for the diving event? Using the same procedure as above but by cutting out the silver and gold medalist, can you use our function to find the bronze medalist and candidate number? Write the code you’d use in this part. PART FOUR: SAVING and SUBMITTING 1. SUBMIT *THREE* THINGS: Save your Notepad++ H6_LastName.txt file. If you worked with a partner, you must both have the file. Save a copy of your whiles.m file, and your find_winner.m function file into your cs7wxx directory. You are welcome to stay and work on the assignment portion with your partner.
© Copyright 2024