Question

In: Computer Science

swapArrayEnds(int[] nums): swaps the first and last elements of its array parameter. Ex: {10, 20, 30,...

  • swapArrayEnds(int[] nums): swaps the first and last elements of its array parameter. Ex: {10, 20, 30, 40} becomes {40, 20, 30, 10}.
  • removeTen(int[] nums): returns a version of the given array where all the 10's have been removed. The remaining elements should shift left towards the start of the array as needed, and the empty spaces at the end of the array should be set to 0. Ex: {1, 10, 10, 2} yields {1, 2, 0, 0}. You can make a new array and return the new array.

Note that swapArrayEnds() does not need to return the result, as it makes changes to the array referenced by the parameter, which is the same array reference by the argument. On the other hand, removeTen() does not direct make changes to the array, but creates a new array, which requires to be made.

  • Java passes parameter by value, a method cannot change the actual argument. However, a method can make changes to the object that is referenced by the argument.
  • If a method changes the array's contents, the method usually has a void return type.
  • If the method does not change the array's contents, but create a new array, then the method needs to return the newly created array.

public class RemoveTen {
public static void main(String[] args) {
int[] nums = {1, 10, 10, 2};
  
swapArrayEnds(nums);

for(int i = 0; i < nums.length; i++)
System.out.print(nums[i] + " ");

int[] result = removeTen(nums);
  
for(int i = 0; i < result.length; i++)
System.out.print(result[i] + " ");
System.out.println();

for(int i = 0; i < nums.length; i++)
System.out.print(nums[i] + " ");
System.out.println();
}

public static void swapArrayEnds(int[] nums) {
/*FIXME Complete the implementation of the swapArrayEnds method*/
  

}

public static int[] removeTen(int[] nums) {
/*FIXME Complete the implementation of the removeTen method*/
  
}

  
}

Solutions

Expert Solution

public class RemoveTen {

public static void main(String[] args) {

int[] nums = {1, 10, 10, 2};

swapArrayEnds(nums);

for(int i = 0; i < nums.length; i++)

System.out.print(nums[i] + " ");

System.out.println();

int[] result = removeTen(nums);

for(int i = 0; i < result.length; i++)

System.out.print(result[i] + " ");

System.out.println();

for(int i = 0; i < nums.length; i++)

System.out.print(nums[i] + " ");

System.out.println();

}

public static void swapArrayEnds(int[] nums) {

int temp=nums[0];

nums[0]=nums[nums.length-1];

nums[nums.length-1]=temp;

}

public static int[] removeTen(int[] nums) {

int new_array[]=new int[nums.length];

int k=0;

for(int i=0;i<nums.length;i++)

{

if(nums[i]!=10)

new_array[k++]=nums[i];

}

if(k<nums.length)

for(int i=k;i<nums.length;i++)

new_array[i]=0;

return new_array;

}

}


Related Solutions

FOR JAVA (ZYBOOK) Write a method swapArrayEnds() that swaps the first and last elements of its...
FOR JAVA (ZYBOOK) Write a method swapArrayEnds() that swaps the first and last elements of its array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}. I can't modify/change any of the code. I can only add code to implement this where it says /* Your solution goes here */. This is the code: import java.util.Scanner; public class ModifyArray { /* Your solution goes here */ public static void main (String [] args) { Scanner scnr...
Given a 2D array a, sum up ALL the edges of the array. Ex. int a[...
Given a 2D array a, sum up ALL the edges of the array. Ex. int a[ ][ ] = { {1, 2, 3, 4},                        {5, 6, 7, 8},                        {9, 10, 11, 12} }; OUTPUT: Sum of the edges = 65
A.) myNums is an array of 50 elements of type int and k is an int...
A.) myNums is an array of 50 elements of type int and k is an int variable. For which one we get the index of out of bounds? a. for (k = 0; k <= 49; k++)     cout << myNums[k] << " "; b. for (k = 1; k < 50; k++)     cout << myNums[k] << " "; c. for (k = 0; k <= 50; k++)     cout << myNums[k] << " "; d. for (k =...
// Given an int array of size elements, determine if there are k elements that add...
// Given an int array of size elements, determine if there are k elements that add up to sum. // The array holds integers, both positive and negative and zero. // It is not possible to add zero elements (that's when k==0) to any sum, not even zero. // It is not possible to add any elements from an empty array. // Must be recursive and not iterative //bool K_element_sum(size_t k, int sum, int arr[], size_t size){}
Write a C function to swap the first and last elements of an integer array. Call...
Write a C function to swap the first and last elements of an integer array. Call the function from main() with an int array of size 4. Print the results before and after swap (print all elements of the array in one line). The signature of the arrItemSwap() function is: void arrItemSwap(int *, int, int); /* array ptr, indices i, j to be swapped */ Submit the .c file. No marks will be given if your pgm does not compile...
Multiples(): Takes an int (n) as parameter and prints first 10 multiples n in a single...
Multiples(): Takes an int (n) as parameter and prints first 10 multiples n in a single line using for loop. Code in Java Ex output: 5 5,10,15,20,25,30,35,40,45,50
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int sum = 0; int i; for(i = 0; i < arr.length; i++){ sum += arr[1]; } return sum; } public int getAverageValueInArray(int[] arr){ // return the average value of array elements int value = 0; for(int i = 0; i < arr.length; i++){ double average = value/ arr.length; } return value; } public int getNumberOfEvens(int[] arr){ //return the number of even values found in...
Implement a method that meets the following requirements: (a) Takes as parameter 1) an int array...
Implement a method that meets the following requirements: (a) Takes as parameter 1) an int array A 2) an int number x to search for (b) determines whether x exists in A, and prints a message indicating the result (c) has the best worst case Big Oh complexity you can manage, using only your own thinking and the materials (the worst case growth rate for the number of items searched should be as low as possible, given that A contains...
[Point: 10] The instance of a list ADT using array is L = (10, 20, 30,...
[Point: 10] The instance of a list ADT using array is L = (10, 20, 30, 40, 50, 60). Find the output of following code segment or what is returned by each statement. remove(30);          find(50):                insert(7, 3):           findKth(4)             [Point: 5] The complexity of remove operation from a LIST ADT using array implementation is O(N). Explain why? [Point: 10] Show that the running time for the following segment of code is O(N3) without using the rule for loop. Make sure to...
array • First, create a function called addNumber, which has a formal parameter for an array...
array • First, create a function called addNumber, which has a formal parameter for an array of integers and increase the value of each array element by a random integer number between 1 to 10. o Add any other formal parameters that are needed. • Second, create another function called printReverse that prints this array in reverse order. • Then, you need to write a C++ program to test the use of these two functions.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT