1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Stolb23 [73]
4 years ago
8

Merge together two sorted arrays of ints into a new array. * * Example1 merge: [-1 3 5 7 ] with [ 2 4 6 8] would yield [-1 2 3 4

5 6 7 8] * Example2 merge: [1 6 ] with [ 2 3 8 9] would yield [1 2 3 6 8 9]
* There is no guarantee about the size of either array. When/if you run out of elements in * either array, copy all the remaining elements from the non-empty array to the new array
* preconditions:
* both arrays, are sorted low to high
* there are no duplicate values among the two arrays
* either array may be empty
* postcondition: return an array with all elements from both arrays sorted from low to high
*
* You may not use any additional methods, sorting routines, etc
* For full credit, your solution may only go through each array one time ( so in particular - no nested loops)
*
* You will need to create a new array inside the function
* You do not have to write this recursively.
public static int[] mergeArrays( int[] a, int[] b) {

int[] answer = new int[0]; // an empty array to have something to return
return answer; // ToDo 3 . Fix this.
}


/*
* exercising functions and main.
* There are no Todo's for you in the code below.
*/
public static void mergeArrayTests() {

int a[] = new int[] {1,3,5,7,9,11};
int b[] = new int[] {2,4,6};
int[] combinedArray = mergeArrays( a,b);
StdOut.println("merging: "+ Arrays.toString(a) + " " + Arrays.toString(b));
StdOut.println(" --> " + Arrays.toString(combinedArray));

int c[] = new int[] {1,3,5,7,9,11};
int d[] = new int[] {2,4};
combinedArray = mergeArrays( c,d);
StdOut.println("merging: "+ Arrays.toString(c) + " " + Arrays.toString(d));
StdOut.println(" --> " + Arrays.toString(combinedArray));

int e[] = new int[] {1,3,5,7,9,11};
int f[] = new int[] {};
combinedArray = mergeArrays( e,f);
StdOut.println("merging: "+ Arrays.toString(e) + " " + Arrays.toString(f));
StdOut.println(" --> " + Arrays.toString(combinedArray));

int g[] = new int[] {3,11};
int h[] = new int[] {2,4,6,8,10};
combinedArray = mergeArrays( g,h);
StdOut.println("merging: "+ Arrays.toString(g) + " " + Arrays.toString(h));
StdOut.println(" --> " + Arrays.toString(combinedArray));
}

public static void main (String[] args) {

StdOut.println(" This main function calls your functions for a variety of inputs");
StdOut.println(" and simply prints the results");
StdOut.println(" It does NOT check to see if your functions return the correct values");
StdOut.println(" It is up to you to review the output and verify the results");
StdOut.println("***************************************************************");
int[] list0 = new int[] {};
int[] list1 = new int[] { 5 };
int[] list2 = new int[] { 3, 4 };
int[] list3 = new int[] { -2, 3, -4 };
int[] list4 = new int[] { -1, -2, -4, -5 };
int[] list5 = new int[] { 6, 1, 2, -3, 8 };

StdOut.format(" list: %s sum of positives: %d\n",Arrays.toString(list0), sumOfPositivesRecursive (list0));
StdOut.format(" list: %s sum of positives: %d\n",Arrays.toString(list1), sumOfPositivesRecursive (list1));
StdOut.format(" list: %s sum of positives: %d\n",Arrays.toString(list2), sumOfPositivesRecursive (list2));
StdOut.format(" list: %s sum of positives: %d\n",Arrays.toString(list3), sumOfPositivesRecursive (list3));
StdOut.format(" list: %s sum of positives: %d\n",Arrays.toString(list4), sumOfPositivesRecursive (list4));
StdOut.format(" list: %s sum of positives: %d\n",Arrays.toString(list5), sumOfPositivesRecursive (list5));
StdOut.println();

StdOut.println ("Reverse: Before: " + Arrays.toString(list1 ) );
reverseArray (list1);
StdOut.println (" After: " + Arrays.toString (list1) + "\n" );

StdOut.println ("Reverse: Before: " + Arrays.toString(list2 ) );
reverseArray (list2);
StdOut.println (" After: " + Arrays.toString (list2) + "\n");

StdOut.println ("Reverse: Before: " + Arrays.toString(list3 ) );
reverseArray (list3);
StdOut.println (" After: " + Arrays.toString (list3) + "\n");

StdOut.println ("Reverse: Before: " + Arrays.toString(list4 ) );
reverseArray (list4);
StdOut.println (" After: " + Arrays.toString (list4) + "\n");

StdOut.println ("Reverse: Before: " + Arrays.toString(list5 ) );
reverseArray (list5);
StdOut.println (" After: " + Arrays.toString (list5) + "\n");

mergeArrayTests();
StdOut.println("***************************************************************");
StdOut.println(" Output was not verified to be correct by this program. ");
StdOut.println(" It is up to you to review the output and verify the results");

}
}

Computers and Technology
1 answer:
Lelu [443]4 years ago
8 0

Answer:

Explanation:

def mergeArrays(arr1, arr2):

   s1 = len(arr1) # size of array 1

   s2 = len(arr2) # size of array 2

   new_size = s1 + s2

   new_arr=[]

   for i in range(new_size):

       if len(arr1)!= 0 and len(arr2) != 0:

           if arr1[0]<=arr2[0]:

               new_arr.append(arr1[0])

               del(arr1[0])

           else:

               new_arr.append(arr2[0])

               del(arr2[0])

       elif len(arr1)==0:

           for e in arr2:

               new_arr.append(e)

           del(arr2)

           break

       else:

           for e in arr1:

               new_arr.append(e)

           del(arr1)

           break

   return new_arr

arr = mergeArrays([-1,3,5,7],[2,4,6,8])

print(arr)

arr = mergeArrays([1,6],[2,3,8,9])

print(arr)

You might be interested in
When designing a laptop, which three things should designers think about?
FromTheMoon [43]

Answer:B,C,D

Explanation:

7 0
3 years ago
Explain at least 5 parts a a computer.
Stels [109]

Answer: There are many parts of a computer, but there are basic ones and major ones.

Explanation: Computers may look very different, but the components installed are standard. The major difference among most machines is the brand of hardware installed. The hardware components—video card, processor, memory, motherboard and hard drive—are the same for all computer systems. While, these are the major ones: Motherboard.

Processor/CPU.

Power Supply.

Hard Drive.

PCI-Express Cards.

Graphics Cards.

RAM/Memory.

6 0
3 years ago
In the SoundByte, the Magic Gradient Pen has an algorithm which listens for ________ and then changes the gradient coloring of t
kirill [66]

Answer:

It listens for sound intensity

Explanation:

It listens for the sound intensity and loudness. As the intensity varies the background color changes as well.

4 0
4 years ago
Read 2 more answers
Choose the correct option that completes the sentence.
vekshin1

What are the answer options?

5 0
3 years ago
Qbasic program to convert Nepali rupees into paisa​
Svetradugi [14.3K]

Answer:

INPUT "What is the amount of rupees you want converted into paisa"; rupees

paisa = rupees*100

PRINT paisa

Explanation:

done in QBASIC
the semicolon in the 1st line makes the question have a ? at the end. the rupees key word in the 1st line saves the input as a variable

then the second line multiplies by 100 since there are 100 paisa in 1 rupee

5 0
2 years ago
Other questions:
  • Describe a situation involving making a copy of a computer program or an entertainment file of some sort for which you think it
    7·1 answer
  • An example of a primary collision factor is
    6·2 answers
  • The spellchecker can be found in which two locations in most wordprocessing software?
    5·1 answer
  • Which event occurs when the user clicks on an html element?
    9·1 answer
  • SONET: is a standard for optical transmission that currently operates at Terabit per second speeds is almost identical to the IT
    7·1 answer
  • What do you understand by technological depenence?
    9·1 answer
  • Write only in C, not C++.
    14·1 answer
  • If I was to sort the months of the year in ASCENDING order alphabetically, which
    11·1 answer
  • Use the drop-down menus to match each description to the correct term.
    13·2 answers
  • which filename refers to a 16-bit real-mode program that queries the system for device and configuration data, and then passes i
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!