I don\'t know how to solve this problem.... ?Write a function to generate a rand
ID: 440998 • Letter: I
Question
I don't know how to solve this problem.... ?Write a function to generate a random data array, ?write a function to compare two different arrays, ?write a function to reverse an array. The first function is outlined for you. Follow the instructions below to complete them. The second and third function are described, but you will need to write your own code to complete it. Note: MATLAB has a lot of functionality built into it to sort and manipulate arrays of data. Do not use built-in MATLAB functions as shortcuts to completing this lab. Remember, the goal of this lab is to practice writing functions and practice using arrays, and it is this practice that is important. Caution: Be careful when selecting variable names. Do not choose variable names that are already defined in MATLAB. If you create a variable named length, then you are hiding the built-in length function. (You will not be able to use MATLAB functions if you have a variable with the same name as the function name.) Part 1: Generating a Random Walk In this part of the lab, you will write a function to create random data. You will use this random data when you test your solutions to the other parts of the lab. We could just use the rand function to fill up an array, but this random data would have no structure. Instead, let's create arrays that contain random walks. This will allow you to examine the data and its structure more easily. Also, random walks play an important role in many simulations and computations of stochastic activity, and you may see them again later. (See Wikipedia: Random Walk for more details if you are interested.) The simplest random walk occurs on the number line, starts at 0, and each step of the random walk goes randomly up or down one unit. Here is an example of an array formed from such a random walk: [ 0 1 2 1 2 3 4 3 4 5 4 3 4 3 2 ] The rules for creating a similar random walk are very easy: 1.The first value is 0. 2.Flip a coin. 3.If the coin is heads, the next value is the current value + 1. 4.If the coin is tails, the next value is the current value - 1. 5.Repeat from step 2 until all the needed values are generated. Let's write a function to generate an array containing a random walk. Create your lab04 directory and move into it. Next, create a new function named RandomWalk (in a file named RandomWalk.m) and cut-and-paste the following function contract above your function: % This function generates an array containing numbers generated % by a random walk. The first number in the random walk is 0, % and each subsequent value is either +1 or -1 from the previous % value. (The step is chosen randomly with equal probability.) % The random walk is stored in an array and returned to the caller. % % Parameters: % walk_length -- the desired number of values in the random walk % % Returns: % walk -- the array containing the random walk of the specified length % Delete the default comments inside of the function. Change the input parameter name and the output variable name to match the names in the function contract. Also make sure the function name is "RandomWalk" and that it matches the filename "RandomWalk.m". Your function header and body should now look like this: function [ walk ] = RandomWalk( walk_length ) end Before you write any statements, it is important to outline the work that your function will do. Let's carefully go through the needed steps. In your function, write a comment for each required step: 1.Create an array of the specified length and store it in the output variable. 2.Make sure the first array value is 0. 3.Create a variable to keep track of how many steps have been taken. 4.Repeatedly take steps until the walk array is full: 5.Choose a random value. 6.If the value is < 0.5, the next value = the current value - 1 7.Otherwise, the next value = the current value + 1 8.Increment the step counter. Here are how the comments look in my function: % Create an array of the specified length and store it in the output % variable. Make sure the first array value is 0. % Create a variable to keep track of how many steps have been taken. % Repeatedly take steps until the walk array is full: % Choose a random value. % If the value is < 0.5, the next value = the current value - 1 % Otherwise, the next value = the current value + 1 % Increment the step counter. It can be tough to write code without a plan. Now that the algorithm is in place, it is easier to create the needed statements. Write the single statement or two that are needed for each step, and make sure your statements actually do the required work. You should try to write the statements on your own. If you get stuck, come back here and follow my hints below: To create the walk array, just use the zeros function to create an array of 1 row and the correct number of columns, then store the result in the walk output variable: walk = zeros(1, walk_length); Note that this also guarantees us that the first value is 0. Creating the variable to keep track of the number of steps should be easy for you, but here is what I did: step_count = 1; Note: I'm counting the first zero as a step. Repeatedly taking steps is equivalent to a while loop. Note that we need to take one less step than the size of the array (to fill an array of 3 values, we need to take only 2 more steps). The while loop condition must be true until we have taken the required number of steps: while step_count < walk_length Your while loop needs an end statement. Enclose all of the loop comments by placing the end statement after them, but before the end of the function. Inside of the while loop, you need to make the random step. First, choose the random value. I used the variable name coin, but you can choose a different name if you like: coin = rand(1); Next, your program needs to decide if the random value was 'heads' or 'tails'. Since the random number is in the range [0..1), just use half the range for each possibility. Since this is a decision your code makes, use an if/else statement. I'll just list the condition part here, you'll need to construct the else and end on your own. if coin < 0.5 If the random value is less than 0.5, you need to make the next value in the array one smaller than the current value. Get the current value, subtract one from it, and store it in the next position in the array. We'll use our step_count to keep track of the storage positions in the array: walk(step_count+1) = walk(step_count) - 1; You will place similar code in the else section of the if statement, except that your random walk will go the other way. Be very careful -- adjust the direction of the step, but remember that the result will still be stored in the next position of the walk array. Finally, in the loop, make sure you count the step you just took. Increment the variable that is keeping track of the number of steps: step_count = step_count + 1; Once you get the code written, try testing it. Go back to the workbench window, and try executing your function. Have it create an array containing a random walk of length 20: w = RandomWalk(20) The variable w should now contain an array of 20 numbers, and these should represent a random walk. Try executing the function again and create a random walk of 300 steps: w = RandomWalk(300) To see the structure of the data, try plotting the random walk: plot(w); Note: When you plot a single array, MATLAB treats it as y-coordinates. It uses default x coordinates. Make sure your function correctly generates a random walk. The plot should slowly zig-zag up and down across the graph, and the trend of the random walk should be clearly visible. Part 2: Reverse an Array Write a function that takes an array as an input parameter, and returns a copy of the array in the output variable. The returned array should have the same elements as the original array, but in reversed order. Call your function "ReverseArray" and put it in a file with the correct name. Make sure your function has a contract. You are to write this one on your own, but you can use the following hints: ? Your function should have one input parameter and one output variable. Give them reasonable names. ? You will need to create an array of the same size as the original array and store it in your output variable. ? Write a loop that copies each element, one at a time, from the original array into the output array (in reverse order). When you get the function written, you can reverse a random walk (and store it), then plot the original and the reverse to see if your function worked. Part 3: Two Arrays Write a function that takes in two different arrays as input parameters, and returns a new array in the output variable. The two input arrays should be of the same size. If they are not use the error() function to quit out of your function. The output will be an array the same size as the input arrays, but for each index in the array, it will contain the largest value between the same index in the two input arrays. Call your function "FindBiggestElements" and put it in a file with the correct name. Make sure your function has a contract. An example input might be: a = [ 1 9 1 5 4 1] b = [ 10 20 0 0 4 14] c = FindBiggestElements( a, b ) At this point, c is now [ 10 20 1 5 4 14 ]. The first value, 10, is because, 10 (from the 1st position in the b array) is larger then the 1 (from the 1st position in the a array). Likewise, 20 is bigger then 9, etc. The basic use of the error() function takes in a single parameter of type string. The string is the error message you wish to display to the user. When MATLAB encounters the error() function, it immediately ceases operation of your script/function. Use help error to learn more about the error function.Explanation / Answer
This is how you put random data in an array :) #include #include #include using namespace std; int main() { const int LOTT = 5; const int USR = 5; int lottery[LOTT]; int user[USR]; for (int index = 0; indexRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.