0

I need help with a code where I have to put the number "7" at the front of the array using a random array of numbers. I use Math.random to generate the random numbers in each index. In any of these indexes, a number 7 may be generated.

import java.util.Arrays;

public class NumberShifter {

    public static int[] Array(){
        int[] array = new int[20];
        int max = 10; 
        int min = 1; 
        int rand = 0;
        int range = max - min +1;

        for(int t = 0;t<=array.length-1;t++) {
              rand = (int)(Math.random() * range) + min; 
            array[t] = rand;
        }



    return array;

    }
}

Here's an example output for the code I have displayed (its all random numbers)

[9, 6, 3, 4, 4, 10, 7, 5, 2, 10, 3, 1, 8, 7, 4, 4, 10, 5, 9, 1]

There are two sevens in this array that have been generated randomly.

I would like to have the output where the sevens are at the front.

[7, 7, 3, 4, 4, 10, 9, 5, 2, 10, 3, 1, 8, 6, 4, 4, 10, 5, 9, 1]

How would I write the rest of my code so that I can achieve this?

P.S. I'm also in high school, so I'm sorry if I get something wrong!

Razmon
  • 61
  • 6
  • 1
    What happens if math.rand doesn't generate a 7? – Beez Dec 11 '19 at 15:44
  • @C2H50H Eventually it will.. – NiVeR Dec 11 '19 at 15:44
  • @NiVeR that's beside the point. That code can very well generate sequences where a 7 doesn't appear, so C2H50H's question is legitimate – Federico klez Culloca Dec 11 '19 at 15:45
  • You can run through the array and mark the spot where all 7's are located then swap the 7's to the front of the array. Check out https://stackoverflow.com/questions/13766209/effective-swapping-of-elements-of-an-array-in-java – Beez Dec 11 '19 at 15:47
  • 2
    @FedericoklezCulloca Indeed, I was being sarcastic :) – NiVeR Dec 11 '19 at 15:47
  • Is an Array required, or can you use an ArrayList? If you can use an ArrayList, you can use the fancy swap method, otherwise it's less of a swap, and more of assigning values to the array buckets. – Compass Dec 11 '19 at 15:48
  • Stackoverflow is not here for you to do your homework. However, I think the task is to switch the 7's with the front numbers. – S.Babovic Dec 11 '19 at 15:49

2 Answers2

0

Iterate the elements of the array and if they are 7 swap them with the next element at the beginning of the array, using another variable to keep track of the next index for swapping.

int index = 0;
for (int i = 0; i < array.length; i++) {
    if (array[i] == 7) {
        int tmp = array[i]; // actually not needed, we know it's 7
        array[i] = array[index];
        array[index] = tmp; // or just 7
        index++;
    }
}
tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • 1
    There are actually unnecessary copy actions if the first elements are sevens. Would it be a good idea to do swap actions if `array[index] != 7` but increment index anyway? – Steyrix Dec 11 '19 at 15:53
  • @Steyrix Sure, you can do that. I did not want to make it more complex than necesary, though. And the effect on running time will be minimal. – tobias_k Dec 11 '19 at 16:10
  • This is the method that I tried! I just couldn't figure it out. Thanks. – Razmon Dec 12 '19 at 15:30
0

I would actually start at the end and reserve the front for the sevens.

   public static int[] Array() {
      int[] array = new int[20];
      int max = 10;
      int min = 1;
      int rand = 0;
      int range = max - min + 1;
      int sevensGoHere = 0;

      for (int t = array.length - 1; t >= sevensGoHere;) {
         rand = (int) (Math.random() * range) + min;
         if (rand == 7) {
            array[sevensGoHere++] = rand;
         }
         else {
            array[t--] = rand;
         }
      }
      return array;

   }

By doing it on the fly you don't need to rearrange the array.

WJS
  • 36,363
  • 4
  • 24
  • 39