Homework #5: Simple Loops and Ifs CSE 7, Winter 2015 Before beginning this homework, create a new Notepad++, H5_LastName.txt, file in your cs7wXX home directory. If you are completing Homework#5 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 "H5_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: SIMPLE FUNCTIONS AND LOOPS 1. Let’s do some work with for-loops. For-loops are a form of iteration that allow you to perform the same instructions a fixed number of times. The instructions that get performed are those in the loop-body. In MATLAB, the loop body is the part that is (often) indented and between the keywords for and end. 2. For example, enter the following code into the command prompt: k=1; for i = 1:20 k = k * i %this is the loop body end 3. The above code will multiply up the numbers 1 through 20 and store it in a variable k. During each iteration the variable k is being updated so that its new value is equal to its old value multiply i. k = k*i %NOTICE THERE IS NO SEMICOLON HERE It follows the following pattern: new_k_value = old_k_value * i 4. Notice that we don’t put a semicolon at the end of the line in the loop body. This is NOT normal and is only for demonstration purposes. Usually, you DO NOT want to print meaningless information out during every iteration of the loop. QUESTION #1. Why might you not want to print our information in a loop body? HINT: Think about loops that go from 1 to 1000 or 1 million. 5. Let’s go ahead and turn this into a more general function. Create new function called multiply_up_numbers, and have it take in two parameters, finish and start which should both be integers. 6. Your function should output a single value which is the total number you get when you multiply up the integers between finish and start. 7. For example, if I passed in 5 and 1 in to my function. I want my function to return 120 since 1 * 2 * 3 * 4 * 5 = 120 8. Add comments explaining how your function works and what all the input parameters mean 9. QUESTION #2. Call your function so that it calculates the product of all the integers between 1 and 100. Write the answer as well as the function call. PART TWO: FUNCTIONS, LOOPS AND SEARCHING FOR VALUES AND LOOPS 1. Now let’s write a function that loops over all the rows in a matrix and checks to see if the value in the first column is equal to some specified value. 2. Why might we do this? Let’s say we have a matrix with two columns. The first contains a list of students’ Gradesource secret number, the second contains their score on the first exam and the third column contains their score on the second exam. Suppose, I only want the scores for student with ID 1234. 3. Then I should be able to call my function (let’s call it find_score) with the parameters 1234 and the matrix of scores as the input and the function should return their first and second exam scores. 4. Here’s the start of the function header. Go ahead and fill in the missing parameters with names of your choosing. function [score1, score2] = find_score(studentid , score_mat) 5. Before implementing the function (writing the function body), let’s consider some test cases. This is common practice in software engineering, and thinking about the tests before writing the function allows us to really understand the function’s behavior. 6. First we have to make up a small list of test scores score_mat = [1252, 80, 74; 1234, 86, 49 ; 2347, 72, 71 ; 3450, 98, 64 ; 4569, 90, 77]; 7. QUESTION #3. What should our function return if we call it with the following parameters: (1234, score_mat) (4569, score_mat) 8. QUESTION #4. What do you think our function should return if we call it with (5678, score_mat)? Should we return a result if the specified student didn’t take the test? What should it be? 9. Now let’s go ahead and implement the function. Start by creating a simple for-loop outline that starts at 1 and goes until the number of rows of the second input parameter. function header for index = 1: <HOW MANY TIMES DO WE LOOP?> %INSERT BODY HERE end end 10. QUESTION #5. What is the built-in matlab function that gives us the number of rows in a matrix? HINT: It’s NOT length. If you are unsure type in help size for help. 11. Now in the function body, we need to use our loop variable which in my example is called index. Since the loop variable is being increased by one at each iteration of the loop, we can use it to move through each row of our score matrix and check if the value in the first column is equal to student ID we’re looking for. Score Mat. First Time through the Loop. index = 1 (index,1) COLUMN 1 (ID) 1252 1234 2347 3450 4569 COLUMN 2 (SCORE 1) 80 86 72 98 90 COLUMN 3 (SCORE 2) 74 49 71 64 77 Score Mat. Third Time through the loop. index = 3 COLUMN 1 (ID) 1252 1234 2347 3450 4569 COLUMN 2 (SCORE 1) 80 86 72 98 90 COLUMN 3 (SCORE 2) 74 49 71 64 77 (index,3) 12. QUESTION #6. What is the equality comparison operator in MATLAB? 13. Using an if statement, check to see if the value at (index, 1) is equal to the student ID we’re looking for. 14. If it is, return score1 and score2. In other words, if the student ID matches row 1, you should return score1 = 80, score2 = 74. PART THREE: APPENDING TO A MATRIX 1. For next week’s lab we are going to need to know how to build an matrix up by iterating through a loop, so let’s go ahead and learn that now. We don’t need to write a function for this, so go ahead and put the following commands into a script called build_mat.m. 2. Building a matrix implies adding either a new row or a new column to it. 3. Let’s start with the matrix my_mat = [1, 2, 3; 2:4]; 4. QUESTION #7. What are the dimensions of my_mat? 5. Now let’s add 10 new rows to my_mat using a for loop that follows the pattern we have created before. Each new row must have the same number of columns as the original my_mat matrix. Type the following lines of code into your script. for i = 3:13 my_mat(i,:) = [i, i+1, i+2] end 6. Notice that each time through the loop, we are growing the my_mat matrix (there are ways to improve Matlab’s performance with this later, but for now this works just fine). 7. The right hand side of the assignment creates a 1x3 vector that gets appended to the bottom of our my_mat matrix. 8. Your Turn. Clear your workspace and comment out the above loop. 9. Create a loop in your script that works just like the above loop ONLY this time it appends a new column to the my_mat matrix. Change the loop so that it adds 20 new columns. 10. QUESTION #8. What does your final output look like? Copy and paste your loop body into your Notepad++ report AND save your m file. PART FOUR: SAVING and SUBMITTING 1. SUBMIT *FOUR* THINGS: Save your Notepad++ file. If you worked with a partner, you must both have the file. Save a copy of your build_mat.m file, your multiply_up_numbers.m and find_score.m file into your cs7wXX directory. You are welcome to stay and work on the assignment portion with your partner. HOMEWORK #5 The Homework#5 assignment is due next week in YOUR lab section at the BEGINNING of Lab. You will be graded individually. You will need to show the Tutor/TA that you are able to use the MATLAB commands from the homework assignment. He/She will ask questions to determine how knowledgeable you are. You may work with a partner or individually for the homework assignment. You should have one file named “Hw5_LastName”, saved in your cs7wXX home directory. (See Homework # 1 details) Note: If partner programming, name your file: “Hw5_Partner1LastName_Partner2LastName: Make sure each of you has a copy of this file in your home directory to access for individual grading in next week’s lab. See “sftp” (Secure File Transfer Protocol) steps in Homework#1. You are free to go, or you are welcome to stay and work on the assignment (posted on the website) portion in the remainder of your enrolled lab section.
© Copyright 2024