, we created a method named search4 which was designed to: search an array (nums
ID: 3820412 • Letter: #
Question
, we created a method named search4 which was designed to:
search an array (nums) for a given value (key)
store all of the positions of where the key is found in another array (positions)
return the number of items placed into the positions array (since positions is a partially-filled array)
The example (nums) array that was shown in class was:
If the search4 method was executed with the above nums array, when it finishes, the positions array would look like this:
For this assignment you are asked to finish writing the main method in the starter file. You need to add code to the main method so that it uses the search4 method to find all of the 8s in the nums array and change them all to 5s.
The starter file is below
0 1 2 3 4 5 56 8 2 8 96 8Explanation / Answer
Code:
positionsSize=search4(nums,8,numsSize,positions);
System.out.println("No of positions where 8 is present is "+positionsSize);
for(int i=0;i<positionsSize;i++){
nums[positions[i]]=5;//using the positions array which contain index TO REPLACE VALUES
}
Totl program:
class SearchHomework {
public static final int MAXSIZE = 6;
public static void main(String[] args) {
int [] nums = new int[MAXSIZE];
int numsSize = fillArray(nums);
int [] positions = new int[nums.length];
int positionsSize;
// write code here which will use the search4 method to
// find all positions of where the number 8 is,
// and then change all of the 8s to 5s
positionsSize=search4(nums,8,numsSize,positions);
System.out.println("No of positions where 8 is present is "+positionsSize);
for(int i=0;i<positionsSize;i++){
nums[positions[i]]=5;//using the positions array which contain index TO REPLACE VALUES
}
//print the array elements
for(int i=0;i<numsSize;i++)
System.out.print(nums[i]+" ");
}
/**
* Fills an array with a preset list of values
* @param nums The array to be filled
* @return The number of items added to the array
*/
public static int fillArray(int [] nums) {
nums[0] = 56;
nums[2] = 2;
nums[4] = 96;
nums[1] = nums[3] = nums[5] = 8;
return 6;
}
/**
* Searches an array for a key value. Creates a list of all the position numbers
* of where the key value is found in the array.
* @param nums The array to be searched
* @param key The value to search for in the array
* @param size The number of items store in the nums array
* @param positions An empty array with at least size number of positions.
* At the end of the method, this array will be holding all of the position numbers
* of where the the key was found in the nums array.
* @return The number of items placed into the positions array
*/
public static int search4(int [] nums, int key, int size, int[] positions) {
int usedSize = 0;
for( int pos = 0; pos < size; pos++ ) {
if ( key == nums[pos] ) {
positions[usedSize] = pos;
usedSize++;
}
}
return usedSize;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.