0

I am trying to develop a program to delete all the median values from an array (the middle value if it has an odd number of elements, the two middle values if it has an even number of elements) until there are only two elements left, elements [0] and [1]. For example, if the user inputs 1, 2, 3, 4, 5, the program will return [1, 5]. I put down what I thought logically might help, but my array x isn't copying myArray in the for loops. I am not looking for someone to completely do the code for me, just to point out where I am wrong. Here is my code:

import java.util.*;
public class Deletion
{
   public static void main(String[]args)
   {
      Scanner kb = new Scanner(System.in);
      System.out.println("Please enter the array length:");
      int [] myArray = new int[kb.nextInt()];
      int [] x = new int[myArray.length - 1];
      int index1 = 0;
      int index2 = 0;
      int index3 = 0;
      if(myArray.length < 3)
      {
         System.out.println("Please make sure array length is greater than two. Run again.");
         System.exit(0);
      }
      else
      {
         for(int i = 0; i < myArray.length; i++)
         {
            System.out.println("Please enter a number for position " + i + ":");
            myArray[i] = kb.nextInt();
         }
         for(int i = 0; i < myArray.length; i++)
         {
            while(myArray.length > 2)
            {
               if(myArray.length%2 != 0)
               {
                  index1 = (myArray.length/2);
                  for(int j = 0, r = 0; j < myArray.length; j++)
                  {
                     if(j != index1)
                     {
                        x[r++] = myArray[j];
                        myArray = x;
                     }
                  }
                  x = new int[myArray.length - 1];
               }
               else
               {
                  index2 = (myArray.length/2);
                  index3 = (myArray.length/2 - 1);
                  for(int j = 0, r = 0; j < myArray.length; j++)
                  {
                     if(j != index2 && j != index3)
                     {
                        x[r++] = myArray[j];
                        myArray = x;
                     }
                  }
                  x = new int[myArray.length - 1];
               }
            }
         }
      }
      System.out.println(Arrays.toString(myArray));
   }
}
9
  • What is your understanding of what the statement myArray = x; does? Commented Mar 6, 2021 at 19:24
  • x = new int[myArray.length - 1] that will create a new array and set all the values to 0, so you're clobbering the results you just populated. Try moving that before the loops. Commented Mar 6, 2021 at 19:25
  • 1
    By the way what we usually call the median is the middle element of a sorted sequence. Commented Mar 6, 2021 at 19:28
  • The final array should contain the lowest value and the largest value? Commented Mar 6, 2021 at 19:30
  • @user2740650 - I was purposely trying to do that so that it would be one smaller than myArray every single loop since there isnt an easy way to delete elements. Commented Mar 6, 2021 at 19:31

3 Answers 3

1

You must create the array and populate it, else it's using the same memory address, hence won't work. Use the following:

myArray = ArrayUtils.clone(x);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your tip, I did try it but it keeps saying it can not find the symbol.
0

When you are doing do “myArray = x”, your are actually merely assigning a reference to the array. Hence, if you make any change to one array, it would be reflected in other arrays as well because both myArray and x are referring to the same location in memory.

Thus, what you need is

myArray = x.clone();

Comments

0

I cleaned up your code a bit. According to what you described, what really matters is pulling in the minimum and maximum values in the array - everything else will be deleted, so you simply need a single traversal through the array to find those two values.

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    boolean isValid = false;
    int validLength = 0;
    System.out.println("Please enter the array length:");
    while (!isValid) {
        int length = scanner.nextInt();
        if (length < 3) {
            System.out.println("Please make sure array length is greater than two. Try again.");
        }
        else {
            isValid = true;
            validLength = length;
        }
    }
    int minimumValue = Integer.MAX_VALUE, maximumValue = Integer.MIN_VALUE;
    for (int i = 0; i < validLength; i++) {
        System.out.println("Please enter a number for position " + i + ":");
        int nextInt = scanner.nextInt();
        if (nextInt < minimumValue) minimumValue = nextInt;
        else if (nextInt > maximumValue) maximumValue = nextInt;
    }
    System.out.println(Arrays.toString(new int[] {minimumValue, maximumValue}));
}

Edit: made another revision as using an array is unnecessary. Just keep track of the minimum and maximum values as they are being entered.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.