Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a method named search4 was created which was designed to: 1.search an array (num

ID: 3820744 • Letter: A

Question

a method named search4 was created which was designed to:

1.search an array (nums) for a given value (key)

2.store all of the positions of where the key is found in another array (positions)

3.return the number of items placed into the positions array (since positions is a partially-filled array)

The example (nums) array is

If the search4 method was executed with the above nums array example, when it finishes, the positionsarray would look like this:

For this assignment you are asked to finish writing the main method in the starter file provided bellow. 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.

0 1 2 3 4 5 56 8 2 8 96 8

Explanation / Answer

/**
* Modified java program that sets all values in the array contains 8 changed to 6*/
//SearchHomework.java
public 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;

       /* find all positions of where the number 8 is,
           and then change all of the 8s to 5s */      
       int usedSize=search4(nums, 8, numsSize, positions);

       System.out.println("nums array ");
       for (int i = 0; i < nums.length; i++) {
           System.out.printf("%-5d",nums[i]);
       }
       System.out.println();
       System.out.println("positions array");
       for (int i = 0; i < positions.length; i++) {
           System.out.printf("%-5d",positions[i]);

       }
   }

   /**
   * Fills an array with a preset list of values  
   */
   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.*
   */
   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;
               //set value 6 to the postion of key vlaue
               nums[pos]=6;
               usedSize++;
           }
       }

       return usedSize;
   }
}

-------------------------------------------------------

Result:

nums array
56   6    2    6    96   6  
positions array
1    3    5    0    0    0   

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote