13.6k views
4 votes
Milestone 1: Write code which asks for a length input until it gets an integer 10 or greater, then creates 2 arrays of this length. Milestone 2: Write code which fills each of the arrays with random integers which are between 1 and 100 inclusive and displays the arrays. Milestone 3: Set up code to loop through each element of the original arrays which are to be checked and added. Milestone 4: Make program check through each previously filled element of the merge array to see if it contains the next value to be added and add this value if it does not already appear. Prints all values of merge array, without including the 0s at the end of the array.

User Hwaxxer
by
5.8k points

1 Answer

5 votes

Answer:

import java.util.Scanner;

import java.lang.Math;

class Main {

public static void main(String[] args) {

int length = 0;

boolean lengthCheck = true;

Scanner scan = new Scanner(System.in);

while (lengthCheck == true)

{

System.out.println("Enter an array length (must be 10 or greater):");

length = scan.nextInt();

if (length >= 10)

{

lengthCheck = false;

}

}

int[] firstArray = new int[length];

int[] secondArray = new int[length];

System.out.print("\\First Array: ");

for (int i = 0; i < length; i++)

{

firstArray[i] = (int) (Math.random() * 100) + 1;

System.out.print(firstArray[i] + " ");

}

System.out.print("\\\\Second Array: ");

for (int i = 0; i < length; i++)

{

secondArray[i] = (int) (Math.random() * 100) + 1;

System.out.print(secondArray[i] + " ");

}

System.out.println("\\");

boolean[] isAdded = new boolean[100];

int[] merge = new int[(firstArray.length + secondArray.length)];

int j=0;

for (int i = 0; i < length; i++)

{

if(!isAdded[firstArray[i] - 1]) {

merge[j] = firstArray[i];

j++;

isAdded[firstArray[i] - 1] = true;

}

if(!isAdded[secondArray[i] - 1]) {

merge[j] = secondArray[i];

j++;

isAdded[secondArray[i] - 1] = true;

}

}

System.out.print("Merged Array: ");

for (int i = 0; i < 2*length && merge[i] != 0; i++)

{

System.out.print(merge[i] + " ");

}

System.out.println("\\");

}

}

User Igor Ivancha
by
5.6k points