Edon Freiner
Edon Freiner

Reputation: 526

Making a nested for loop into a single for loop

I had an assignment to create an array of random number from 10-100. Then I need to sout all the numbers not listed in the array. I did the assignment with a nested for loops, to cross reference the arrays, then I changed all the found numbers in the array into -1. Finally I printed out the elements in the array that were not -1. My professor told me that is it possible for me to do this assignment with only one for loop and there is no need to do a nested for loop. and make the computer run 10,000 times instead of just 100. Is that possible? If so how?

Thank you.

    package assignment.pkg1;

    import java.util.Random;
    public class Assignment1 {

       static Random ran = new Random();

        public static void main(String[] args) {

            int[] arr = new int[100];

            for (int i = 0; i < 100; i++) {
                arr[i] = (ran.nextInt(90)) + 10;
            }

            InversingArray(arr);


        }

        public static void InversingArray(int[] randomArray) {

            int[] fullArray = new int[100];

           for (int i = 0; i < 100; i++) {
                fullArray[i] = i;
            }

            for (int i = 0; i < 100; i++) {
                for (int j = 1; j < 100; j++) {

                    if (randomArray[j] == fullArray[i]) {
                        fullArray[i] = -1;

                    }
                }
           }
            System.out.println("These numbers are not in randomArray: ");
             for (int i = 0; i < 100; i++) {
                if (fullArray[i] != -1) {
                    System.out.println(fullArray[i]);
                }

            }
        }

Upvotes: 1

Views: 1296

Answers (2)

Nathaniel Johnson
Nathaniel Johnson

Reputation: 4839

In your code you create an array to hold the possible values. If you think about it, the array index will always be equal to the number stored in the array.

fullArray[i] = i;

This is redundant.

What you are being asked to do is determine which numbers have been used: a boolean test. This means that you should have an array of boolean that is initially false (the default value of booleans in java) and is flipped to true when an equal integer is flipped to true.

Something like

int[] arr = new int[100];

for (int i = 0; i < 100; i++) {
        arr[i] = (ran.nextInt(90)) + 10;
}

// ba starts with all false values 
boolean ba[] == new boolean[90];  // note that the instructor said 10-100
for(int i=0; i<90; i++) {
  ba[arr[i]] = true;
  // lets assume arr[0] == 45
  // ba[arr[0]] is the same as ba[45]
  // ba[45] = true;  will set that bucket of the boolean array to true 
}

System.out.println("These numbers are not in randomArray: ");
for (int k = 0; k < 10; k++) {
    System.out.println(k);
}
for (int j = 0; j < 90; j++) {
    if (!ba[j]) { // shorthand for ba[j]==false
        System.out.println(j+10); // The array starts at a base of 10
    }
}

Be aware (probably the point of the exercise) that you are working with an array [0..90] that represents the numbers [10..100].

Upvotes: 1

slartidan
slartidan

Reputation: 21576

The nested loop currently looks like this:

for (int i = 0; i < 100; i++) {
    for (int j = 1; j < 100; j++) {
        if (randomArray[j] == fullArray[i]) {
            fullArray[i] = -1;
        }
    }
}

But we know, that fullArray[i] is always the same as i.

So you can rewrite it to:

for (int j = 1; j < 100; j++) {
    int i = randomArray[j];
    fullArray[i] = -1;
}

Or even shorter:

for (int j = 1; j < 100; j++) {
    fullArray[randomArray[j]] = -1;
}

Upvotes: 0

Related Questions