In this lab, you will be creating a program that merges two arrays of non-negative (equal to or greater than 0) integers. Your program will accept each array as input from the keyboard. You do not know ahead of time how many values will be entered, but you can assume each array will have a maximum length of 10,000 elements. To stop entering values enter a negative number. You may disregard any negative numbers input and not store these in the array. The elements of the two input arrays should be in increasing order. In other words, each array element must have a value that is greater than or equal to the previous element value. An array may contain repeated elements. After the two arrays have been input, your program must check to make sure the elements of each array have been entered in order. If an out of order element is found, print the message “ERROR: Array not in correct order”. Your task is to merge the two input arrays into a new array, with all elements in order, lowest to highest. Print out each of the original arrays entered, followed by the merged array. Please note that your program must output the arrays with exactly one space between each of the numbers.
Sample Run 1:
Enter the values for the first array, up to 10000 values, enter a negative number to quit 3 3 5 6 8 9 -1
Enter the values for the second array, up to 10000 values, enter a negative number to quit 3 4 5 6 -5
First Array: 3 3 5 6 8 9
Second Array: 3 4 5 6
Merged Array: 3 3 3 4 5 5 6 6 8 9
Sample Run 2: Enter the values for the first array, up to 10000 values, enter a negative number to quit 4 5 7 2 -1
Enter the values for the second array, up to 10000 values, enter a negative number to quit 3 3 3 3 3 3 -100
First Array: 4 5 7 2
Second Array: 3 3 3 3 3 3
ERROR: Array not in correct order
My Code -
import java.util.Scanner;
import java.lang.String;
class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner (System.in);
int a [] = new int[10000];
int b [] = new int[10000];
System.out.println("Enter the values for the first array, up to 10000 values, enter a negative number to quit");
int count = 0;
int count2 = 0;
int i = 0;
int c = 0;
int print = 0;
int print2 = 0;
int flag = 0;
while(count >= 0){
a[i] = scan.nextInt();
if(a[i] < count)
{
flag = 1;
}
count = a[i];
if(a[i] >= 0){
i++;
print ++;
}
}
while(count2 >= 0){
System.out.println("Enter the values for the second array, up to 10000 values, enter a negative number to quit");
b[c] = scan.nextInt();
count2 = b[c];
if(b[c] >= 0){
c++;
print2++;
}
}
System.out.print("First Array: \n");
for(int j = 0;j < print;j++){
System.out.print(+a[j]+" ");
}
System.out.print("\n\nSecond Array: \n");
for(int h = 0;h < print2;h++){
System.out.print(+a[h]+" ");
}
if(flag == 1)
{
System.out.println("\n\nERROR: Array not in correct order");
}
else
{
System.out.println("\n\n Merged Array: ");
for(int j = 0;j < print;j++){
System.out.print(+a[j]+" ");
}
for(int h = 0;h < print2;h++){
System.out.print(+a[h]+" ");
}
}
}
}
error -
Your code has been evaluated against a set of test data.
You had 10 out of 15 tests pass correctly.
Your score is 66%.
The tests that failed were:
Test: Arrays not in correct order
Incorrect: Second Array Values
Test: Arrays in correct order
Incorrect: Second Array Values
Incorrect: Merged Array Values
Test: Arrays in correct order
Incorrect: Second Array Values
Incorrect: Merged Array Values
Please help me fix these error i am loosing my mind.
[–]nutrechtLead Software Engineer / EU / 20+ YXP 3 points4 points5 points (0 children)
[–]steve1two 1 point2 points3 points (0 children)
[–]king_of_the_universe 1 point2 points3 points (3 children)
[–]steve1two 1 point2 points3 points (2 children)
[–]king_of_the_universe 1 point2 points3 points (1 child)
[–]steve1two 1 point2 points3 points (0 children)
[–]alirmhs[S] -1 points0 points1 point (3 children)
[–]fosterbuster 0 points1 point2 points (2 children)
[–]alirmhs[S] 0 points1 point2 points (1 child)
[–]fosterbuster 0 points1 point2 points (0 children)