Write a function that takes as an input parameter an array


Lab Assignment

For this lab you may work with a partner, or you may choose to work alone. Your partner should be in the same lab as you.. If you choose to work with a partner, you are still responsible for the lab in its entirety, even if you partner is abducted by aliens and mind melded with a two-toed sloth. One partner should turn in the lab via canvas, with both names on it, and the other should simply write on Canvas that they submitted with their partner.

Your file should start with comments including your name(s), etc., followed by your included files, and then your function declarations in the order in which the functions are written in the lab.

Your main should follow, calling each of the functions and, if needed or helpful, initializing variables before function calls and printing out values and arrays.

Following the main should be each of your functions in the order in which they are specified below.

Make sure your code is formatted and commented. The comments should include 3 test cases specifying, given these input parameters, you expect this output. For each function, you should also have main call the function with your 3 test cases.

In the comments at the top, write the name, email address, and office hours of your TA)

Arrays

About parameters: Unless I specify, you may choose whether your parameter is passed by value, pointer or reference. If a parameter doesn't change and shouldn't change within a function, you should pass by value.

1. Write a function that takes an array of integers as an input parameter (the address of the first value of the array). It returns nothing. It prints out the array as a single line, with commas between each number, and when the array is finished being printed, it prints an endl; so that we flush the buffer and move to a new line.

Note: you'll be using this function after running other functions to check out your results.

1. Write a function that takes as input parameters (using call by pointer) 3 integers. It generates a random number between 25 and 50 (not including 50). It then creates an array on the memory heap of that length. It generates a random high number (mine was between 5 and 10) and a random low number (between -5 and -10) and fills in the array iteratively with random numbers between the high and the low numbers*, and it returns that array. The input parameters should be modified so that it holds the length of the array, the high value, and the low value. In main, call the function 1 to print out the array.

*not including the high - in general when we specify a range, we include the first value but not the last. If I forget to say that in the future, you can assume that's what I intended.

2. Write a function that is almost exactly the same as the function above, only it takes an input parameter an integer (and you can pass in a number between 25 and 50 - your choice). Inside the function create an array on the stack instead of the heap. Fill it with random numbers as above. Return the address of the first value of the array, and then in the main use function 1 to print it out. This should NOT work. In comments explain why.

3. Write a function that takes as an input parameter an array of integers (and the size of the array). The function should print out the address of every value in the array.

4. Write a function that takes as an input parameter an array of doubles. The function should print out the address of every value in the array. See how this works?

For this next part, you will be writing a low pass filter using a very basic hanning window. The idea behind a low pass filter is to get rid of high frequencies, or, in essence, smoothing out the massive outliers in an array. The way to do this is to take a window size, usually an odd number in length, in which you take the average of the values before and after a value in an array and replace that value with the average. So, for instance, if you had an array as follows:

3,8,2,5,1,4,6,0,2

And you had a window size of 3, you would go through the array in window sizes of 3 and replace each center value with the average. For those values at the beginning and the end, in which you can't have a window on both sides, you'd replace the values with a 0. Otherwise each value gets replaced with its window average.

The resulting array would be:

0 4 5 2 3 3 3 2 0

Which is a much smoother array with fewer outliers.

Now, an even better way of smoothing out the outliers is to weight the window. So, in the process of averaging, the values farthest from the center value in the window are weighted the lowest, whereas the values closest to the center of the window are weighted the highest. This is known as a Hanning window.

For instance, if you have a window size of 5, you might weight the values by multiplying the values at location 1 and 5 with 1, the values at location 2 and 4 with 2 and the values at location 3 (the center) with 3, and then dividing the sum by 9 to get the weighted average. So in this case, given the following array and a window size of 5,

3,8,2,5,1,4,6,0,2

Using a hanning window for a filter, you'd get:

0, 0, 4, 3, 3, 3, 3, 0, 0,

(Note that this is even smoother than the really simple filter we used above).

So you can see where this is going:

2. Write a function for the hanning window. For this function, you should take as input parameters a part of the array (remember - whenever you pass in an array, no matter what you pass in, it is always interpreted as the address of the first value of the array. So if I passed in &arr[3], when that address enters the function, it will assume that the 4th address is the first address in the array. So DO NOT make a copy of each window and send that into the function -that is just wasteful and makes no sense). Assume the window size is an odd number in size, Weight the values appropriately. Return the weighted and averaged value.

3. Write a function for filtering the array. This function should return a new array. You can't write over the old array (why not?). You will have to create a new array on the heap, and return that new array. This function should take as input parameters the original array and the size of the original array, and should create a new array that has been filtered using the hanning window function.

Print out both arrays to see and check the difference.

4. Write a function that takes as input parameters an array, the length of the array, the highest value in the array, and the lowest value in the array. The function should print out a graph of the array by printing out a * for each value in the appropriate place (below is an example of my printout, which is probably easier than explaining it:

Random Array generated:

6, -2, -4, 5, -3, -4, -3, -1, 5, 2, -2, 0, -7, 2, -3, -4, -3, -1, -5, -3, 1, 7, 3, -7, -7, 3, -8, 1, -5, -4, -2, -5, -8, 0, -4,

Graph of the above array (printed using the function in 6):

Filtered Array (using the hanning window):

0, 0, 0, 0, -1, -2, -1, 0, 1, 1, 0, -1, -2, -2, -2, -2, -3, -2, -2, -1, 1, 2, 0, -2, -3, -3, -3, -2, -3, -3, -4, -4, -4, 0, 0,

Graph of the filtered array (printed using function 6):

5. Write a function that takes as input parameters two integer addresses (both call by pointer). It returns the address of a 2-dimensional array.

In the function, generate a random number between 5 and 10. That will be the size of the array of addresses (call it x). Then generate a second random number between 4 and 8. That will be the size of each array of integers (call it y). Adjust the input parameters to hold x and y.

Create the 2-dimensional array (of x, y in size), making sure the 2-d array is on the heap.

Fill this array with 0's. Then generate 5 random number pairs (between 0 and x, and between 0 and y) and place a 1 in each of those random number locations.

Make sure that there isn't already a 1 in that location (and if there is generate a new x and y random number set).

Return the 2 d array.

In the main, use a for loop (looping x times) to call the print function (function 1) to print out the matrix.

Attachment:- Lab-Arrays.rar

Request for Solution File

Ask an Expert for Answer!!
Data Structure & Algorithms: Write a function that takes as an input parameter an array
Reference No:- TGS02680150

Expected delivery within 24 Hours