Question

Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

Assignment 6, Merge Arrays (java)

Instructions

In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries.

Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print both of these arrays. Finally, the program will create and print an array using all the elements which are in the two arrays but not including any duplicates. The merged array may have space for more elements than needed: extra elements at the end of the array should be set to 0 and not printed by the program.

The numbers should be added to the merged array in an alternating pattern: first from list 1, then from list 2, then list 1 again, etc. If a number in one of the arrays already appears in the merged array, then it should be ignored, and the program should alternate to the other list again. For example, if the first list begins 1 2 3 10, and the second begins 3 4 5 8, then the merged list would begin 1 3 2 4 5 10 8.

Because the number of elements in the merged array is unknown, its size should be set to the maximum possible number of elements it should contain, and after all elements which should form the merged array appear, any remaining unfilled spaces in the array should be 0. The first 0 encountered in the array should signal the end of the “actual” elements of the array, and therefore the 0s at the end of the array should not be printed by your program.

Be sure that your output is in the format shown in the sample run, with one array on each line and each one named correctly: the Code Runner needs to be able to read the arrays that are printed in order to correctly grade your work.

Sample Run 1

Enter an array length (must be 10 or greater):
5

Enter an array length (must be 10 or greater):
20

First Array: 89 96 23 91 2 20 89 4 50 72 88 24 88 45 58 55 37 32 79 7

Second Array: 58 63 35 39 21 17 54 53 83 19 12 67 30 67 96 76 95 26 77 41

Merged Array: 89 58 96 63 23 35 91 39 2 21 20 17 54 4 53 50 83 72 19 88 12 24 67 30 45 55 76 37 95 32 26 79 77 7 41

Sample Run 2

Enter an array length (must be 10 or greater):
10

First Array: 99 30 29 77 29 52 36 5 59 59 

Second Array: 13 10 13 82 59 58 96 30 15 73 

Merged Array: 99 13 30 10 29 77 82 59 52 58 36 96 5 15 73

Sample Run 3

Enter an array length (must be 10 or greater):
50

First Array: 32 81 75 98 97 64 76 32 99 57 29 11 58 57 5 48 49 46 17 84 85 78 44 12 11 7 35 44 73 11 28 64 84 97 59 92 99 6 49 54 69 42 93 47 42 99 80 22 31 67

Second Array: 91 64 58 91 36 15 79 83 65 41 47 98 89 26 37 65 26 93 59 86 69 52 31 77 9 33 20 83 11 52 14 64 74 83 19 95 51 11 7 5 87 88 57 62 4 38 64 77 55 55

Merged Array: 32 91 81 64 75 58 98 97 36 15 76 79 83 99 65 57 41 29 47 11 89 26 5 37 48 49 46 93 17 59 84 86 85 69 78 52 44 31 12 77 9 7 33 35 20 73 28 14 74 19 92 95 51 6 54 87 42 88 62 4 38 80 22 55 67

Milestones

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.

This is what I have so far, I'm not sure how to merge the arrays and make sure no numbers repeat.

I commented out my array merge attempt.

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("\nFirst Array: ");

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

{

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

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

}

System.out.print("\n\nSecond Array: ");

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

{

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

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

}

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

for (int i = 0; i<merge.length;)

{

merge[i]=firstArray[i];

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

i++;

merge[i]=secondArray[i];

i++;

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

}

*/

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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("\nFirst Array: ");

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

       {

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

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

       }

       System.out.print("\n\nSecond Array: ");

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

       {

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

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

       }
       System.out.println("\n");
      
      
/*
* Created a boolean array of length 100 to track list of number we have already added to merge list
*/
       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("\n");
      

   }

}

Add a comment
Know the answer?
Add Answer to:
Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program to merge two sorted arrays into a third array. Ask the user to...

    Write a program to merge two sorted arrays into a third array. Ask the user to enter the size of the two arrays. The length of array1 could be greater than, equal to or less than the length of array2. Fill both with random numbers 0-99. Sort both arrays as explained below. Write a method merge that takes the two arrays as arguments. The method returns a merged array with size equal to the size of both arrays combined. Note:...

  • Consider the below matrixA, which you can copy and paste directly into Matlab.

    Problem #1: Consider the below matrix A, which you can copy and paste directly into Matlab. The matrix contains 3 columns. The first column consists of Test #1 marks, the second column is Test # 2 marks, and the third column is final exam marks for a large linear algebra course. Each row represents a particular student.A = [36 45 75 81 59 73 77 73 73 65 72 78 65 55 83 73 57 78 84 31 60 83...

  • Question 9-15 are based on the random sample below which is obtained to test the following hypoth...

    Question 9-15 are based on the random sample below which is obtained to test the following hypothesis about the population mean. Test the hypothesis that the mean is less than 80. 80 100 81 93 80 57 98 90 71 56 58 78 59 55 55 77 72 78 56 94 98 59 93 86 89 62 60 66 59 71 96 97 94 69 64 77 87 77 64 90 90 95 98 99 56 69 72 81 95...

  • write a c++ program that merges two arrays a and b. You may use the following...

    write a c++ program that merges two arrays a and b. You may use the following array contents A=5,7,8,6,3,3 and B=4,5,6,1,2,3.The merged array should exclude all repeating numbers

  • == Programming Assignment == For this assignment you will write a program that controls a set...

    == Programming Assignment == For this assignment you will write a program that controls a set of rovers and sends them commands to navigate on the Martian surface where they take samples. Each rover performs several missions and each mission follows the same sequence: deploy, perform one or more moves and scans, then return to base and report the results. While on a mission each rover needs to remember the scan results, in the same order as they were taken,...

  • 1. Forecast demand for Year 4. a. Explain what technique you utilized to forecast your demand....

    1. Forecast demand for Year 4. a. Explain what technique you utilized to forecast your demand. b. Explain why you chose this technique over others. Year 3 Year 1 Year 2 Actual Actual Actual Forecast Forecast Forecast Demand Demand Demand Week 1 52 57 63 55 66 77 Week 2 49 58 68 69 75 65 Week 3 47 50 58 65 80 74 Week 4 60 53 58 55 78 67 57 Week 5 49 57 64 76 77...

  • Write a program to calculate students’ average test scores and their grades. You may assume the...

    Write a program to calculate students’ average test scores and their grades. You may assume the following data: Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 83 30 69 87 Blair 23 45 96 38 59 Clark 60 85 45 39 67 Kennedy 77 31 52 74 83 Bronson 93 94 89 77 97 Sunny 79 85 28 93 82 Smith 85 72 49 75 63 Use three...

  • Write a program to calculate students’ average test scores and their grades. You may assume the...

    Write a program to calculate students’ average test scores and their grades. You may assume the following data: Johnson 85 83 77 91 76 Aniston 80 90 95 93 48 Cooper 78 81 11 90 73 Gupta 92 83 30 69 87 Blair 23 45 96 38 59 Clark 60 85 45 39 67 Kennedy 77 31 52 74 83 Bronson 93 94 89 77 97 Sunny 79 85 28 93 82 Smith 85 72 49 75 63 Use three...

  • I need to develop the 7 functions below into the main program that's given on top...

    I need to develop the 7 functions below into the main program that's given on top Write a C program to create array, with random numbers and perform operations show below. Il Project 3 (53-20). 1 Projects CS5,00 This file contains the function Programcution Dagine and one include <timo // Defining some constants definer_SIZE 20 define LOVE LIMIT 1 eine UPR UNIT define TALSE eine Tut 1 wold randomizery (int[], int, Int, int) wold pinay in. St, Int); En find...

  • In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 pro...

    In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 processes. First half of the array will be sorted by thread 1 and the second half by thread 2. When the threads complete their tasks, the main program will merge the half arrays. You should not waste your time on merge-sort algorithm. Use the merge sort algorithm given below void mergesort(int a[],int i,int j) { int mid; if(i<j) { mid=(i+j)/2; mergesort(a,i,mid); //left recursion mergesort(a,mid+1,j); //right...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT