Question

- LILOPOULOULU 5. You are given two arrays each of which is sorted. Write a method called mergelt that takes the two arrays a

can you do this question in C++.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <bits/stdc++.h>
using namespace std;
int merge(int a[],int n, int b[], int m)
{
int c[n+m];
int i=0,j=0,k=0;
while((i<n)&&(j<m))
{
if(a[i]<=b[j])
{
c[k++]=a[i++];
}
else if (a[i]>b[j])
{
c[k++]=b[j++];
}
}
while(i<n)
c[k++]=a[i++];
while(i<m)
c[k++]=b[j++];
  
for(int i=0;i<n+m;i++)
cout<<c[i] <<" ";
return 0;
}

int main()
{
int a[]={1,2,3,7,8};
int b[]= {4,6,7};
merge(a,5,b,3);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
can you do this question in C++. - LILOPOULOULU 5. You are given two arrays each...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program to merge two sorted arrays into a third array. Ask the user to...

    Write a program to merge two sorted arrays into a third array. Ask the user to enter the size of the two arrays. The length of array1 could be greater than, equal to or less than the length of array2. Fill both with random numbers 0-99. Sort both arrays as explained below. Write a method merge that takes the two arrays as arguments. The method returns a merged array with size equal to the size of both arrays combined. Note:...

  • Write a mips program that defines two integer array that are pre-sorted and the same size...

    Write a mips program that defines two integer array that are pre-sorted and the same size (e.g., [3, 7, 9, 11, 15, 21] and [1, 4, 6, 14, 18, 19]) and a function merge that takes the two arrays (and their size) as inputs and populates a single array of twice the size of either input array that contains the elements of both arrays in ascending order. In the example arrays given, then output would be [1, 3, 4, 6,...

  • HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge...

    HW58.1. Array Merge Sort You've done merge (on Lists), so now it's time to do merge sort (on arrays). Create a public non-final class named Mergesort that extends Merge. Implement a public static method int[] mergesort(int[] values) that returns the input array of ints sorted in ascending order. You will want this method to be recursive, with the base case being an array with zero or one value. If the passed array is null you should return null. If the...

  • void merge(Card[] cardArray, int first, int mid, int last) This is a helper method for the...

    void merge(Card[] cardArray, int first, int mid, int last) This is a helper method for the merge sort. It takes as parameters a card array, the first index, the middle index, and the end index. The method merges two adjacent sorted arrays into a single sorted one. The first array begins with the element at first and ends with the element at mid. The second array begins with the element at mid + 1 and ends with the element at...

  • *c language* Write a program that takes two sorted array A,B of size m,n respectively where...

    *c language* Write a program that takes two sorted array A,B of size m,n respectively where A is sorted in the ascending order and B is sorted in the descending order, and merge these two arrays into a sorted array C.   For example if A=[1, 4, 5, 7, 8] and B=[10,9,8,6,4,2]   then C=[1,2,4,4,5,6,7,8,8,9,10].

  • You are given an input of k arrays each of size n. Each one of the...

    You are given an input of k arrays each of size n. Each one of the k arrays is sorted. Give an algorithm that turns the k sorted arrays into one sorted array of k * n elements. Please explain the algorithm in words and analyze the run time.

  • write a c++ program that merges two arrays a and b. You may use the following...

    write a c++ program that merges two arrays a and b. You may use the following array contents A=5,7,8,6,3,3 and B=4,5,6,1,2,3.The merged array should exclude all repeating numbers

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • public static int[] interleaveArray(int[] a, int[] b) Given two arrays, return the array which interleaves the...

    public static int[] interleaveArray(int[] a, int[] b) Given two arrays, return the array which interleaves the elements of the two arrays. The two arrays do not have to be the same length. For example, given a = [1, 2, 3] and b = [4, 5, 6, 7, 8], the method returns c = [1, 4, 2, 5, 3, 6, 7, 8] Parameters: a - given array b - given array Returns: the array which interleaves the elements of the two...

  • Use C programming Swap two arrays using pointers Given two integer arrays, swap the two arrays...

    Use C programming Swap two arrays using pointers Given two integer arrays, swap the two arrays using pointers. Input:     4     1 2 3 4     5     4 5 6 7 8     where: First line represents the number of elements in the first array. Second line represents the elements in the first array. Third line represents the number of elements in the second array. Fourth line represents the elements in the second array. Output:     4 5 6 7...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT