Question

In: Computer Science

Write an array method to carry out each of the following tasks for an array of...

Write an array method to carry out each of the following tasks for an array of integers. Note: you can think of each as a separate problem independent of the others.

  1. a) Swap the first and last elements in the array.

  2. b) Shi< all elements by one to the right and move the last element into the first

    posi>on. For example, 1 4 9 16 25 would be transformed into 25 1 4 9 16.

  3. c) Replace all even elements with 0.

  4. d) Replace each element except the first and last by the larger of its two neigh-

    bors.

  5. e) Return true if the array contains duplicate elements (which need not be adja-

    cent).

  6. JAVA

Solutions

Expert Solution

Source Code:

class ex
{
   static void swap_first_last(int arr[])
   {
       int temp=arr[0];
       arr[0]=arr[arr.length - 1];
       arr[arr.length - 1]=temp;
   }
   static void move_circular_right(int arr[])
   {
       int last=arr[arr.length - 1];
       int i=arr.length -1 ,temp=0;
       while(i>0)
       {
           arr[i]=arr[i-1];
           --i;
       }
       arr[0]=last;
   }
   static void replace_even_with_zero(int arr[])
   {
       for(int i=0;i<arr.length;i++)
       {
           if(arr[i]%2==0)
               arr[i]=0;
       }
   }
   static void replace_with_adj_neighbour(int arr[])
   {
       for(int i=1;i<arr.length-1;i++)
       {
           if(arr[i-1]>arr[i+1])
               arr[i]=arr[i-1];
           else
               arr[i]=arr[i+1];
       }
   }
   static boolean have_duplicates(int arr[])
   {
       boolean duplicates=false;
       for(int i=0;i<arr.length-1;i++)
       {
           for(int j=i+1;j<arr.length;j++)
           {
               if(arr[i]==arr[j])
                   duplicates=true;
           }
       }
       return duplicates;
   }
   public static void main(String[] args) {
       int arr[]=new int[]{1,4,9,16,25};
       swap_first_last(arr);
       move_circular_right(arr);
       replace_even_with_zero(arr);
       replace_with_adj_neighbour(arr);
       System.out.println(have_duplicates(arr));
       for(int i=0;i<arr.length;i++)
       {
           System.out.println(arr[i]);
       }
   }
}


Related Solutions

Write array methods that carry out the following tasks for an array of integers by creating...
Write array methods that carry out the following tasks for an array of integers by creating and completing the “ArrayMethods” class below. Add documentation comments for each method. Provide a test program called ‘Lab5_yourID.java” that test methods of ArrayMethods class. In your test program, use random class to generate array values. public class ArrayMethods { private int[ ] values; //declare instant variables public ArrayMethods (int[ ] initialValues) {values = initialValues;} //constructor public void shiftRight( ) { … } public Boolean...
Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5. Create a...
Write out the null and alternative hypotheses for the following hypothetical proposal. Carry out a one-sample...
Write out the null and alternative hypotheses for the following hypothetical proposal. Carry out a one-sample Z test to determine significance at the α=0.05 level. PROPOSAL In the field of cancer epidemiology, many researchers are interested in developing risk measurement assays that are intended to influence the screening process for prevention of late stage and metastatic cancers. Parity, or the number of children that a woman has over her lifetime, is associated with the overall risk of breast cancer in...
For each of the properties reflexive, symmetric, antisymmetric, and transitive, carry out the following. Assume that...
For each of the properties reflexive, symmetric, antisymmetric, and transitive, carry out the following. Assume that R and S are nonempty relations on a set A that both have the property. For each of R complement (Rc), R∪S, R∩Sand R−1. determine whether the new relation must also have that property; might have that property, but might not; or cannot have that property. Any time you answer Statement i or Statement iii, outline a proof. Any time you answer Statement ii,...
For each of the properties reflexive, symmetric, antisymmetric, and transitive, carry out the following. Assume that...
For each of the properties reflexive, symmetric, antisymmetric, and transitive, carry out the following. Assume that R and S are nonempty relations on a set A that both have the property. For each of R complement, R∪S, R∩S, and R−1, determine whether the new relation must also have that property; might have that property, but might not; or cannot have that property. A ny time you answer Statement i or Statement iii, outline a proof. Any time you answer Statement...
Each of the following scenarios requires the use of accounting information to carry out one or...
Each of the following scenarios requires the use of accounting information to carry out one or more of the following managerial activities: (1) planning, (2) control and evaluation, (3) continuous improvement, or (4) decision making. a. MANAGER: At the last board meeting, we established an objective of earning an after-tax profit equal to 20 percent of sales. I need to know the revenue that we need to earn to meet this objective, given that we have $250,000 to spend on...
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
In java write a method that will take an array and change it into a linkedlist...
In java write a method that will take an array and change it into a linkedlist and then display it in the main method
The following scenarios each contain enough information to carry out a hypothesis test for means. For...
The following scenarios each contain enough information to carry out a hypothesis test for means. For each one, (1) give the formula for the test statistic (in terms of the variables, not the numbers in the problem!), (2) indicate which distribution you would use to determine the critical value, and (3) give the number of degrees of freedom, either as a formula or as calculated for the problem (if relevant). You don’t have to explain your answers, and you don’t...
4-1- Let array locations A[0], ..., A[7] hold numbers A = {9,8,7,6,5,4,3,2}. Carry out by hand...
4-1- Let array locations A[0], ..., A[7] hold numbers A = {9,8,7,6,5,4,3,2}. Carry out by hand one call of the partition function of Quick Sort on the array A above. You must trace the actions of partition on this array carefully. After this single call to partition, answer the questions: 1. What is the initial index of the pivot element? What is its value? 2. Where does the pivot end up? (What is its index at the end?) 3. How...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT