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!

7
  • 1
    What happens if math.rand doesn't generate a 7? Commented Dec 11, 2019 at 15:44
  • @C2H50H Eventually it will.. Commented Dec 11, 2019 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 Commented Dec 11, 2019 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 stackoverflow.com/questions/13766209/… Commented Dec 11, 2019 at 15:47
  • 2
    @FedericoklezCulloca Indeed, I was being sarcastic :) Commented Dec 11, 2019 at 15:47

2 Answers 2

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++;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

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 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.
This is the method that I tried! I just couldn't figure it out. Thanks.
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.

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.