2

Create a random array of numbers from 1 – 10. Then, move all of the 7s to the front of the array. The order of the other numbers is not important as long as all numbers follow the group of Lucky 7s. Looking at the code for insertion sort and selection sort might be very helpful for this assignment. Arrays.toString() will also be useful. For instance, if you are given the list :: 4 5 6 7 1 2 3 7 The list could become – all 7s must be first :: 7 7 6 4 1 2 3 5

My code:

public class NumberShifter
{

public int[] go(int[] arrayToBeShifted, int index)
{
  int originalArray[] = new int[index];
  int valueBeingMoved = originalArray[index];

  for (int i = index; i > 0; i--) 
  {
    arrayToBeShifted[i] = arrayToBeShifted[i-1];
  }

  arrayToBeShifted[0] = valueBeingMoved;

  return arrayToBeShifted;
}
}

The runner:

class Main
{
  public static void main(String[] args) 
  {
    int random[] = new int[10];
    for(int i=0; i<random.length; i++)
    {
      random[i] = (int)(Math.random()*6)+1;
    }
    NumberShifter rt = new NumberShifter();   
    System.out.println(rt.go(random,7));
    System.out.println(rt.go(random,7));
    System.out.println(rt.go(random,7));
    System.out.println(rt.go(random,7));
  }
}

This program give me the errors Please anyone tell me that i use a correct method to solve this question. If i use a wrong method for this question please solve this with correct method.

3 Answers3

0

The problem is here in NumberShifter:

int originalArray[] = new int[index];
int valueBeingMoved = originalArray[index];

You are creating an array with a size of [index]. Suppose index = 7, so it has index values of 0, 1, 2...6 But in the next line you are accessing originalArray[index] (originalArray[7]) which does not exist. So you are getting the exception.

Apart from that, your code has other issues as well.

I would like to suggest this very simple approach:

public class Main {

    public static void main(String[] args) {
        int random[] = {4, 7, 6, 5, 7, 8, 0, 7, 1, 7};

        printArray(random);
        move7sToFirst(random);
        printArray(random);
    }

    public static void move7sToFirst(int[] random) {
        for (int i = 0; i < random.length; i++) //traverse array to find 7s
            if (random[i] == 7)                 //if 7 found
                for (int j = 0; j < i; j++)     //traverse from beginning to index before 7 to find non-7
                    if (random[j] != 7)         //if non-7 found
                        swap(random, i, j);     //swap 7 with non-7
    }

    public static void swap(int[] random, int index1, int index2) {
        int temp = random[index1];
        random[index1] = random[index2];
        random[index2] = temp;
    }

    public static void printArray(int[] arr) {
        for (int i = 0; i < arr.length; i++)
            System.out.print(arr[i] + " ");
        System.out.println("");
    }
}

Output:

4 7 6 5 7 8 0 7 1 7 
7 7 7 7 4 6 5 8 0 1 

Of course, this is the simplest approach with time complexity of O(n^2). This can be improved by memorizing the indexes for next iterations and time complexity can be reduced significantly to improve it almost as O(n).

Tameem Khan
  • 577
  • 5
  • 14
  • As I mentioned, you code has other issues too. The way you are traversing and manually trying to point out the 7s will not achieve the expectations. I have updated my answer with a simpler method. – Tameem Khan Jan 29 '20 at 04:23
0

There are multiple inaccuracies in your logic, I have fixed the code and following should solve your problem

public class NumberShifter{
   public static void main(String[] args){
      int[] array = {7,7,1,1,2,3,4,7,7,7,5,7};
      for(int i=0;i<array.length;i++){
         System.out.print(" "+array[i]);
      }
      shift7s(array);
      System.out.println("\n");
      for(int i=0;i<array.length;i++){
         System.out.print(" "+array[i]);
      }
   }

   public static void shift7s(int[] array) {
      int i = 0;
      int j = array.length -1;
      while(i < j){
         i = getIndexOfNextNonSeven(array, i);
         j = getIndexOfNextSeven(array, j);
         if(i == array.length || j < 0) break;
         if(i < j){
            swap(array, i, j);
         }
      }
   }

   private static int getIndexOfNextNonSeven(int[] array, int currentIndex){
      while(currentIndex < array.length &&
              array[currentIndex] == 7 ){
         currentIndex++;
      }
      return currentIndex;
   }

   private static int getIndexOfNextSeven(int[] array, int currentIndex){
      while(currentIndex < array.length &&
              array[currentIndex] != 7 ){
         currentIndex--;
      }
      return currentIndex;
   }

   private static void swap(int[] array, int p1, int p2){
      int temp = array[p1];
      array[p1] = array[p2];
      array[p2] = temp;
   }
}
mightyWOZ
  • 7,946
  • 3
  • 29
  • 46
0

This is the way i solve the question i make two separate classes runner and main for code.

My code:

public class NumberShifter
{

public int[] go(int[] arrayToBeShifted, int index)
{
  for (int i = 0, e = 0; i < arrayToBeShifted.length; i++) 
  {
    if (arrayToBeShifted[i] == 7)
    {
    arrayToBeShifted[i] = arrayToBeShifted[e];
    arrayToBeShifted[e] = 7;
    e++;
    }
  }
  return arrayToBeShifted;
}
}

The runner:

class Runner
{
  public static void main(String[] args) 
  {
    NumberShifter rt = new NumberShifter();  
  //demo to see  if code works
    System.out.println(java.util.Arrays.toString(rt.go(new int[]{1, 10, 9, 2, 8, 2, 5, 6, 10, 7, 9, 8, 6, 7, 2, 7, 6, 10, 5, 3},7)));

  //random arrays according to question
  int random[] = new int[20];
    for(int i=0; i<random.length; i++)
    {
      random[i] = (int)(Math.random()*10)+1;
    }
     System.out.println(java.util.Arrays.toString(rt.go(random,7)));
    System.out.println(java.util.Arrays.toString(rt.go(random,7)));
    System.out.println(java.util.Arrays.toString(rt.go(random,7)));

  }

}
Faheem
  • 42
  • 9