Only need the method not program 1. Write a method called range that returns the
ID: 3559404 • Letter: O
Question
Only need the method not program
1. Write a method called range that returns the range of values in an array of integers. The range is defined as 1 more than the difference between the maximum and minimum values in the array. For example, if an array called List contains the values [36, 12, 25, 19, 46, 31, 22], the call of range (list) should return 35 (46 - 12 + 1 ). You may assume that the array has at least one element.
2. Write a method called isSorted that accepts an array of real numbers as a parameter and returns true if the list is in sorted (increasing order), and false otherwise.
3. Write a method called collapse that accepts an array of integers as a parameter and returns a new array containing the result of replacing each pair of integers with the sum of that pair. For example, if an array called list, stores the values [7,2,8,9,4,13,7,1,9,10], then the call of collapse(list) should return a new array containing [9,17,17,8,19]. The first pair from the original list is collapsed into 9 (7 + 2), the second pair is collapsed into 17 (8 + 9), and so on. If the list has an odd number of elements, the final element is not collapsed, it's just ignored instead.
4. Change number 1, to work for a 3 dimensional array of integers.
5. Write a method that computes the average length of words in an array that is passed to it. Assume that the array contains all Strings. The average should be returned.
Explanation / Answer
1. Write a method called range that returns the range of values in an array of integers.
The range is defined as 1 more than the difference between the maximum and minimum values in the array.
For example, if an array called List contains the values [36, 12, 25, 19, 46, 31, 22],
the call of range (list) should return 35 (46 - 12 + 1 ). You may assume that the array has at least one element.
public static int range(int[] array)
{
int min = array[0];
int max = array[0];
for(int i=0; i<array.length; i++)
{
if(array[i] < min) min = array[i];
if(array[i] > max) max = array[i];
}
return max - min + 1;
}
2. Write a method called isSorted that accepts an array of real numbers as a parameter and returns true if the list is in sorted (increasing order),
and false otherwise.
public static boolean isSorted(int[] array)
{
for(int i=0; i<array.length-1; i++)
{
if(array[i] > array[i+1])
return false;
}
return true;
}
3. Write a method called collapse that accepts an array of integers as a parameter and returns a new array containing the result
of replacing each pair of integers with the sum of that pair. For example,
if an array called list, stores the values [7,2,8,9,4,13,7,1,9,10], then the call of collapse(list)
should return a new array containing [9,17,17,8,19].
The first pair from the original list is collapsed into 9 (7 + 2),
the second pair is collapsed into 17 (8 + 9), and so on.
If the list has an odd number of elements, the final element is not collapsed, it's just ignored instead.
public static int[] collapse(int[] array)
{
int[] new_array = new int[array.length/2];
for(int i=0; i<array.length-1; i+=2)
{
new_array[i/2] = array[i] + array[i+1];
}
return new_array;
}
4. Change number 1, to work for a 3 dimensional array of integers.
public static int range(int[][][] array)
{
int min = array[0][0][0];
int max = array[0][0][0];
for(int i=0; i<array.length; i++)
{
for(int j=0; j<array[i].length; j++)
{
for(int k=0; k<array[i][j].length; k++)
{
if(array[i][j][k] < min) min = array[i][j][k];
if(array[i][j][k] > max) max = array[i][j][k];
}
}
}
return max - min + 1;
}
5. Write a method that computes the average length of words in an array that is passed to it.
Assume that the array contains all Strings. The average should be returned.
public static double avg_length(String[] words)
{
double sum = 0;
for(int i=0; i<words.length; i++)
sum = sum + words[i].length();
return sum/words.length;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.