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

6. Write a program in java which has two arays of size 4 and 5; merge...
6. Write a program in java which has two arays of size 4 and 5; merge them and sort.
This is for c++ Write a program that works with two arrays of the same size...
This is for c++ Write a program that works with two arrays of the same size that are related to each other in some way (or parallel arrays). Your two arrays must be of different data types. For example, one array can hold values that are used in a formula that produces the contents of the second array. Some examples might be:  from a previous program, populations and the associated flowrates for those populations (an int array of populations...
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
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points) #include using namespace std; void SortArrays (int a[], int b[], int c[], int size); void printArray(int m[], int length); const int NUM = 5; int main() { int arrA[NUM] = {-2, 31, 43, 55, 67}; int arrB[NUM] =...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points) #include <iostream> using namespace std; void SortArrays (int a[], int b[], int c[], int size); void printArray(int m[], int length); const int NUM = 5; int main() { int arrA[NUM] = {-2, 31, 43, 55, 67}; int arrB[NUM]...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points) #include <iostream> using namespace std; void SortArrays (int a[], int b[], int c[], int size); void printArray(int m[], int length); const int NUM = 5; int main() { int arrA[NUM] = {-2, 31, 43, 55, 67}; int arrB[NUM]...
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).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT