Answered You can hire a professional tutor to get the answer.

QUESTION

Please send the . For the OrdArray class in the orderedArray.java program (Listing 2.

Please send the .cpp attachment

-----------------------------

Here's the question: For the OrdArray class in the orderedArray.java program (Listing 2.4 on page 59), you are asked to add the following methods

·        A merge() method so you can merge two ordered source arrays into an ordered destination array. To simplify, for duplicate values (no mater in an array or two arrays), only count once. 

·        A common() method so you can get the common values owned by the two ordered source arrays into an ordered destination array. For duplicate values, only count once. 

·        Using the objected oriented analysis and design, using the available insert(), find(). delete() and display() methods.

·        You cannot use any available library. 

Write code in main() of class OrderedApp that inserts some random numbers into the two source arrays, invokes merge() and common() and displays the contents of the resulting arrays. The source arrays may hold different numbers of data items. In your algorithm you will need to compare the keys of the source arrays, picking the smallest one to copy to the destination. You'll also need to handle the situation when one source array exhausts its contents before the other.

Example:

Source array A = {3, 6, 8, 8, 11, 17, 25, 34, 38, 46, 48, 48, 48, 57, 62, 69, 72, 72, 77, 83};

Source array B = {5, 8, 14, 25, 31, 37, 41, 48, 48, 52, 77, 82, 94};

Then, Destination array C (after merge) = {3, 5, 6, 8, 11, 14, 17, 25, 31, 34, 37, 38, 41, 46, 48, 52, 57, 62, 69, 72, 77, 82, 83, 94};

Destination array D (after common) = {8, 25, 48, 77}; 

------------------------------------------

Here's the code so far, but I'm getting an Error: import does not name a type & expected unqualified-id before "public:

------------------------

//orderedArray.java

import java.util.Arrays;

public class orderedArray {

static int[] merge(int[] a, int[] b) {

int[] result = new int[a.length + b.length];

int i=0, j=0, count=0;

int last = -1;

while(i < a.length && j < b.length) {

if(a[i] < b[j]) {

if(a[i] != last) {

result[count++] = a[i];

last = a[i];

}

i++;

} else if(a[i] > b[j]){

if(b[j] != last) {

result[count++] = b[j];

last = b[j];

}

j++;

} else {

if(b[j] != last) {

result[count++] = b[j];

last = b[j];

}

j++;

i++;

}

}

if(i == a.length) {

while(j < b.length) {

if(b[j] != last) {

result[count++] = b[j];

last = b[j];

}

j++;

}

} else if(j == b.length){

while(i < a.length) {

if(a[i] != last) {

result[count++] = a[i];

last = a[i];

}

i++;

}

}

// there may be duplicate elements in result

// so create new result

int newResult[] = Arrays.copyOf(result, count);

return newResult;

}

static int[] common(int[] a, int[] b) {

// find max length

int l = (a.length > b.length ? a.length : b.length);

int[] result = new int[l];

int i=0, j=0, count=0;

int last = -1;

while(i < a.length && j < b.length) {

if(a[i] < b[j]) {

i++;

} else if(a[i] > b[j]){

j++;

} else {

if(b[j] != last) {

result[count++] = b[j];

last = b[j];

}

j++;

i++;

}

}

// there may be duplicate elements in result

// so create new result

int newResult[] = Arrays.copyOf(result, count);

return newResult;

}

public static void main(String args[]) {

int[] A = {3, 6, 8, 8, 11, 17, 25, 34, 38, 46, 48, 48, 48, 57, 62, 69, 72, 72, 77, 83};

int[] B = {5, 8, 14, 25, 31, 37, 41, 48, 48, 52, 77, 82, 94};

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

System.out.println(Arrays.toString(merge(A, B)));

System.out.println("nCommon Elements:");

System.out.println(Arrays.toString(common(A, B)));

}

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question