Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Try Snip &: Assignment 6: Arrays This assignment asks you to write a collection of little functions that all operate on an array of integers.
Try Snip &: Assignment 6: Arrays This assignment asks you to write a collection of little functions that all operate on an array of integers. All the functions take an array as argument. 1. Create a function called copyArray() which creates a deep copy and copies every element from the first array to the second array and returns the second array 2. Create a function called void printLiteral() that prints the array to the console in a form that looks exactly like the literal strings use above for initialization. The literal starts with a "f", then lists the numbers separated by commas but no spaces, and ending with "}". The output must even work if the length of the array is 0 ("{}") or 1 (no commas). 3. Create a function called sumOfArray that returns the sum of the values in the array, 4. Create a function called maxinArray that returns the maximum value in the array, 5. Create a function called mininArray that returns the minimum value in the array When looking for min or max, compare each new value to the min or max value that you have seen so far. Do not initialize that value to an arbitrary extreme (like 0). Observe that when you check the first value of the array, that value should become the min or max you have seen so far, since you haven't seen anything else. Assuming that you are using a variable called minSeen, the correct solution is to initialize minSeen to the first value right away, before starting the loop. That way you don't have to know anything about the range of possible values (which could be all negative, making 0 a bad choice for the initial value.) 6. Create a function called rangelnArray that returns the range of the values in the array. In math, the range is the magnitude of the difference between the minimum and maximum (inclusive). You compute it by subtracting the minimum from the maximum. You already have methods that return the min and max. Use them and do not include any loops in this method. 7. Create a function called mean() that returns the average value in the array. Even though the array contains ints, return the average as a double. Note that you have a method for the sum, and you can get the length by using the array property of the array itself. So this method only has to do a divide. Test that you can get a result that includes a fractional part (not just whole numbers). You may need to look up "cast" to see how to make the division not do an integer divide. In the average and range functions, use the sumOfArray, mininArray, and maxinArray functions to compute those parts, rather than repeating the work of computing the sum, or looking for min and max. 8. Create a function called clip that takes a maximum value as an argument besides the array and changes any value in the array that is higher than the specified maximum value to be the same as the maximum value. This function could also be called "haircut", in that it takes values that are too high, and cuts them down to the maximum allowable height. (Think of a scissor going through your hair and trimming the ones that are too long.) If the array is {2, 4, 6, 7, 3}, after clip(5) it should be (2, 4, 5, 5, 3}. When calling the clip() function from main(), use copy Array() to make a copy of the array and use the new array for clipping so that the original array is not touched. Call the print() function before and after to show the effect of clipping. Do not print values inside the clip method. 9. Create a function called median which calculates the median of the array and returns the median value. Median is the middle number in the array after sorting the array. If array has odd number of elements, median is the exact middle number. If the array has even number of elements, median is the average of the two middle elements. For example if array has 9 elements, median is the 5th element or element corresponding to the 4th index. If array has 10 elements it's the average of the 5th and 6th element or same as average of the elements in the 4th and 5th index. To calculate the median we need to sort the array. A sorting algorithm is given below. 10. Create a function called mode which returns the mode value which is the most repeated value in the array. If there are more than one mode, just pick one of them. a. For example consider the array {1,2,3,2,3,2,4,2,5,2}, mode is 2 as 2 is the most repeated number. b. Consider the array {1,2,3,2,3,2,3,4,5). In this array there are 3 twos and 3 Threes. So 10. Create a function called mode which returns the mode value which is the most repeated value in the array. If there are more than one mode, just pick one of them. a. For example consider the array {1,2,3,2,3,2,4,2,5,2), mode is 2 as 2 is the most repeated number. b. Consider the array {1,2,3,2,3,2,3,4,5). In this array there are 3 twos and 3 Threes. So mode is 2 and 3. But you can return either 2 or 3 (One of the modes). C. Consider the array {10,9,8,7,6,5). In this array every value appears only once. So all values are modes. Just return one of them. If Array is empty, The min and max methods should just return O. For median and mode you need the sorted array (especially for the median) you can sort the array using the command. java.util.Array.sort( ) Please do not hard code the array or length of the array. All these functions should work for any array as input When you are finished (or as far as you got before getting stuck) submit the zipped package with class, as always. This is how your main will look like. You will have to complete the testArrayFunction that tests the functions. public static void main(String[] args) { int[] arro = { -2,-1 }; int [] arr1= new int[10]; I/arr1 is filled in with 10 random numbers between 100 and 500 int [] arr2 = {2, 4, 6, 8, 10, 12, 14, 16, 7, 6, 22, 8, 9, 16, 5, 2, 7, 8, 12, 2, 0, 14, 17, 19, 22 }; testArrayFunctions (arro); testArrayFunctions (arr1 )
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started