Question

In: Computer Science

Write a program in java which has two arrays of size 4 and 5; merge them...

Write a program in java which has two arrays of size 4 and 5; merge them and sort.

Solutions

Expert Solution

I have implemented the required code in Java.

And also provided the output of the respective code.I hope this will help.

Source code:

Main.java

import java.util.Arrays;
public class Main {
public static void main(String[] args) {
//array of length 4
int[] array1 = {33,88,11,55};
//array of length 5
int[] array2 = {22,44,66,99,77};
  
//Calculating sum of length of two arrays
int length = array1.length + array2.length;
  
System.out.println("First Array is: ");
for (int i = 0; i < array1.length; i++) {
System.out.print(" " + array1[i]);
}
System.out.println(" ");
System.out.println("Second Array is: ");
for (int i = 0; i < array2.length; i++) {
System.out.print(" " + array2[i]);
}
//Creating a resulting array of the calculated length
int[] result = new int[length];
int position = 0;
//for each loop to add array1 elements to the resulting array
for (int element: array1) {
result[position] = element;
position++;
}
//for each loop to add array2 elements to the resulting array
for (int element: array2) {
result[position] = element;
position++;
}
Arrays.sort(result);
System.out.println("\nThe resulting array after merging and sorting two arrays is: ");
for (int i = 0; i < length; i++) {
System.out.print(" " + result[i]);
}
}
}

Output:


Related Solutions

Write and test a merge function that uses a recursive algorithm to merge two sorted arrays...
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays of integers. Neither list contains duplicates, and the resulting list should not contain duplicates either. Hint: You may want to call a helper function from merge. PROGRAM: C
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Write a program that takes two integer arrays a and b of size n from the...
Write a program that takes two integer arrays a and b of size n from the user, the use a method product to find the product of a and b and return the results after storing them in an array c, then prints the returned results to the screen. (Note: c[i] = a[i] * b[i], for i = 0, ..., n-1) Sample Output: Enter the size of your arrays: 5 Enter the integer values of the first array a: 1...
in Java, write a program that takes an input of 4 numbers and splits them into...
in Java, write a program that takes an input of 4 numbers and splits them into 4 separate lines. EXAMPLE: so if input is 1994 the output should be 1 9 9 4
Write a Java program to create an array of a specific size (which is an input...
Write a Java program to create an array of a specific size (which is an input from the user) and fill it with random numbers between 1 and 100. Then sort the array and count how many of these numbers are originally at sorted position. Display that original array, the sorted array, and the count (number of elements originally at sorted position).
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them array1 and array 2) Add the two 3Darrays together and then get the average. Save the average on a separate 3D array.(lets name it array3) Then add array1 and array3 then get the average Save the average on a separate 3Darray(lets name it array 4) Then add array2 and array3 then get the average Save the average on a separate 3Darray (lets name it...
********JAVA************ This program deals with graphs and path-finding within them. write a program which allows the...
********JAVA************ This program deals with graphs and path-finding within them. write a program which allows the user to enter multiple levels of a text, consisting of a 'start', an 'exit', 'walls', and 'floors'. The letter S represents the start. E is the exit. O (the letter 'oh') represents a 'floor' (meaning a path you can follow). X is a wall (meaning a block you cannot pass). The program will accept Standard Input (System.in), It is up to you whether you...
IN JAVA: Write a simple program that takes 5 inputs from the user and save them...
IN JAVA: Write a simple program that takes 5 inputs from the user and save them into a Text File. The inputs are Student Name, Student Address and student Date of Birth. Also write a simple program that reads and display the input from the first program text file.
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread...
DO THIS IN JAVA Write a complete Java program. the program has two threads. One thread prints all capital letters 'A' to'Z'. The other thread prints all odd numbers from 1 to 21.
Write a java program that lets the user to enter any two integers and weave them...
Write a java program that lets the user to enter any two integers and weave them digit by digit and print the result of weaving their digits together to form a single number. Two numbers x and y are weaved together as follows. The last pair of digits in the result should be the last digit of x followed by the last digit of y. The second-to-the-last pair of digits in the result should be the second-to- the-last digit of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT