Home > OS >  How can I match the original array and the shuffled array?
How can I match the original array and the shuffled array?

Time:07-04

This is the Prompt: enter image description here

This is what I wrote: enter image description here

import java.util.Scanner;
public class ContagionControl {
    public static void main(String[] args) {
        System.out.println("Please Enter the Number of Citizens: ");
        Scanner input= new Scanner(System.in);
        int a = input.nextInt();


        int[] A = new int [a];

        System.out.printf("       Id");
        for (int i=0; i< A.length; i  ) {

            System.out.printf("M", i);

        }
        System.out.println();
        System.out.printf("Cantactee");
        for (int i=0; i< A.length; i  ) {
            int b= (int) (Math.random() * A.length);
            System.out.printf ("M", b);
        }
        System.out.println();
        System.out.println("Please Enter the Number of Citizens");
        int c = input.nextInt();

        for (int j=0; )

    }
}

How can I match the infected id with his/her contactee ?

CodePudding user response:

you need to create another ArrayList to hold quarantine_citizens;

step 1 Then get the contactee from to the citizen array base (index of citizen, map index to contactee array)

create variable to hold contactee and put into quarantine_citizens ArrayList

and it needs to map to the citizen array again ( do step 1 again )

enter image description here

CodePudding user response:

@Jay explained the required algorithm in his answer. Below is the code that implements the algorithm.

The following appears in the assignment instructions that you posted in your question:

use the shuffling algorithm

And also

Be aware that all the elements are distinct

Your shuffling algorithm does not ensure that all elements are distinct. In other words, the algorithm that appears in the code in your question may produce the same number more than once.

The below code uses code taken from this Web page:
Shuffle Array in Java

The below code also checks for valid user input. More explanation appears after the code.

import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class CC {

    private static int[] getCitizens(int numberOfCitizens) {
        int[] citizens = new int[numberOfCitizens];
        for (int i = 0; i < numberOfCitizens; i  ) {
            citizens[i] = i;
        }
        return citizens;
    }

    private static int[] getContactees(int[] array, int a) {
        int[] contactees = new int[array.length];
        System.arraycopy(array, 0, contactees, 0, array.length);
        Random rd = new Random();

        // Starting from the last element and swapping one by one.
        for (int i = a - 1; i > 0; i--) {

            // Pick a random index from 0 to i
            int j = rd.nextInt(i   1);

            // Swap array[i] with the element at random index
            int temp = contactees[i];
            contactees[i] = contactees[j];
            contactees[j] = temp;
        }
        return contactees;
    }

    private static void printArray(int[] array, String format, String prompt) {
        System.out.print(prompt);
        for (int i : array) {
            System.out.printf(format, i);
        }
        System.out.println();
    }

    private static void printResult(List<Integer> list) {
        System.out.println("These citizens are to be self-isolated in the following 14 days:");
        for (Integer id : list) {
            System.out.print(id   " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {

        @SuppressWarnings("resource")
        Scanner input = new Scanner(System.in); // Don't close 'Scanner' that wraps stdin.
                                                // (Even though IDE warns that you should.)

        int numberOfCitizens = 0;
        do {
            System.out.print("Number of citizens: ");
            try {
                numberOfCitizens = input.nextInt();
                if (numberOfCitizens <= 0) {
                    System.out.println("Must be positive integer. Try again.");
                }
            }
            catch (InputMismatchException xInputMismatch) {
                System.out.println("Not an integer. Try again.");
                input.nextLine();
            }            
        } while (numberOfCitizens <= 0);
        int[] citizens = getCitizens(numberOfCitizens);
        String format = "%"   Integer.toString(numberOfCitizens).length()   "d ";
        printArray(citizens, format, "       ID ");
        int[] contactees = getContactees(citizens, numberOfCitizens);
        printArray(contactees, format, "Contactee ");
        int infectedCitizen = -1;
        int limit = numberOfCitizens - 1;
        do {
            System.out.printf("ID of infected citizen [0-%d]: ", limit);
            try {
                infectedCitizen = input.nextInt();
                if (infectedCitizen < 0 || infectedCitizen > limit) {
                    System.out.println("Not within range. Try again.");
                }
            }
            catch (InputMismatchException xInputMismatch) {
                System.out.println("Not an integer. Try again.");
                input.nextLine();
            }
        } while (infectedCitizen < 0 || infectedCitizen > limit);
        List<Integer> infected = new ArrayList<>(numberOfCitizens);
        while (true) {
            if (!infected.contains(infectedCitizen)) {
                infected.add(infectedCitizen);
                infectedCitizen = contactees[infectedCitizen];
            }
            else {
                break;
            }
        }
        Collections.sort(infected);
        printResult(infected);
    }
}

The last while loop, in method main is the actual algorithm. All preceding code is just preparation. I refer to the following code:

List<Integer> infected = new ArrayList<>(numberOfCitizens);
while (true) {
    if (!infected.contains(infectedCitizen)) {
        infected.add(infectedCitizen);
        infectedCitizen = contactees[infectedCitizen];
    }
    else {
        break;
    }
}

I now use the sample data from your question to explain the above algorithm. Here is the sample data:

       ID  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
Contactee  2  0  7 11 15  4 14  1  8 13  3  5 12  9  6 10
  1. We create an empty List.
  2. The user-entered ID is 0 (zero).
  3. We add 0 to the List.
  4. We get the element in Contactee array at index 0 – which is 2
  5. We add 2 to the List.
  6. We get the element in Contactee array at index 2 – which is 7
  7. We add 7 to the List.
  8. We get the element in Contactee array at index 7 – which is 1
  9. We add 1 to the List.
  10. The element in Contactee at index 1 is 0. Since 0 already appears in the List, we are done.

List now contains all the infected citizens, namely 0, 1, 2 and 7.
Note that sorting the list is only to display the list in ascending order. It is not required.

Here is output from a sample run of the above code:

Number of citizens: 16
       ID  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 
Contactee  5  8  3  0 11 12 14  9  7  1 15  2  4 13  6 10 
ID of infected citizen [0-15]: 0
These citizens are to be self-isolated in the following 14 days:
0 2 3 4 5 11 12
  • Related