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

1) Solve the following problem by sorting the array first. How do you need to mo

ID: 3812174 • Letter: 1

Question

1) Solve the following problem by sorting the array first.

How do you need to modify the algorithm for computing the total?

You are given the quiz scores of a student.You are to compute the final quiz score, which is the sum of all scores after dropping the lowest one.For example, if the scores are

8 7 8.5 9.5 7 4 10

then the final score is 50.

2) Solve the following problem using an algorithm that removes and inserts elements instead of switching them.

You are given an array whose size is an even number, and you are to switch the first and the second half.

For example, if the array contains the eight numbers

9 13 21 4 11 7 1 3

then you should change it to

11 7 1 3 9 13 21 4

Write the pseudocode for the algorithm, assuming that methods for removal and insertion exist.

Trace the algorithm and explain why it is less efficient than the swapping algorithm shown below.

i=0

j = size/2

While (i < size / 2):

Swap elements at positions i and j

i++

j++

End While

3) It turns out that we must be careful about updating the index value when we remove elements from an array list, since the size of the list changes inside the loop.

Show how we can avoid this problem by traversing the array list backwards.

4) Are each of the following statements about arrays true or false? If false, why?

a.All elements of an array are of the same type.

b. Arrays cannot contain strings as elements.

c. A method cannot change the length of an array argument.

5) Implement each of the operations on partially filled arrays below as a method.

a.Sort the elements in decreasing order.

b.Print all elements, separated by a given string.

c.Count how many elements are less than a given value.

d.Remove all elements that are less than a given value.

e. Place all elements that are less than a given value in another array.

6) A run is a sequence of adjacent repeated values.

Implement an int method that computes and returns the length of the longest run in an array.

For example, the longest run in the array with elements

1 2 5 5 3 1 2 4 3 2 2 2 2 3 6 5 5 6 3 1

Explanation / Answer


package com.test.ds;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;


public class ArrayTest {
   public static double calculateSum(){
       double result =0.0;
       double arr1[]={8 ,7 ,8.5 ,9.5, 7 ,4 ,10};
       Arrays.sort(arr1);
System.out.println(Arrays.toString(arr1));
for(int i=1;i<arr1.length;i++){
   result+= arr1[i];
}
  
return result;
   }

   static void swapArrayValues(){
      
       int[] arr1={9, 13, 21, 4 ,11 ,7 ,1, 3};
       int mid =arr1.length/2;
       Integer[] array = {1,2,3,4,5,6};

       Integer[] array2 =Arrays.copyOfRange(array, 0, mid-1); // returns {10, 20}
       Integer[] array3 =Arrays.copyOfRange(array,mid-1, array.length);

       Integer[] array4 =array2+array3;
      
   }
  
  
   static int findSequenceOfRepeatedPosition(){
       int[] arr = {1 ,2, 5, 5, 3, 1, 2, 4, 3 ,2, 2, 2, 2, 3 ,6, 5 ,5, 6, 3 ,1}; // 2, 3 repeats at position 2.
          int result =0;
           int element =0;
           int prev = 0;
           int size = 0;
           int max_size = 0;
           for (int i=0;i<=arr.length;i++){
               if (element == prev)
                   size ++;
                   if( size > max_size){
                   result = element;
                   max_size = size;
                   }
                   else{
                       size = 0;
                       prev = element;
                   }
                  
           }
           System.out.println(result);
             
       return 0;
   }
  
   public static void main(String[] args) {
       System.out.println(calculateSum());
       swapArrayValues();
       int[] arr = {1 ,2, 5, 5, 3, 1, 2, 4, 3 ,2, 2, 2, 2, 3 ,6, 5 ,5, 6, 3 ,1};
       System.out.println("longest run is :"+longestSequence(arr,arr.length));
       sortArray(arr);
   }
  
   static int longestSequence(int array[], int size)
   {
   int currentNumber = array[0], currentMax = 1, longestMax = 1;
   for(int i = 1; i < size; i++)
   {
   if(array[i] == currentNumber)
   currentMax++;
   else
   {
   currentNumber = array[i];
   if(currentMax > longestMax)
   longestMax = currentMax;
   currentMax = 1;
   }
   }
   return longestMax;
   }
  
   static Array sortArray(int[] arr){
       List<Integer> integersList = Ints.asList(arr);
       Collections.sort(integersList, Collections.reverseOrder());
      
       for (Integer i: arr) {   
   //Do your stuff here
   System.out.println(i);
   }
   }

}


All elements of an array are of the same type.
   True
Arrays cannot contain strings as elements:False
   Arrays can store strings
  
A method cannot change the length of an array argument: True
It gives only reference of the array , it doesn't change the size