Question

CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...

CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER'

QUESTION

Using C++

11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume

First array

04

Second array

Result array

that one array doesn’t contain two copies of the same value. The result array should also contain no duplicate values.

(Hint: When one of the input arrays has been exhausted, don’t forget to copy the remaining data in the other array into the result array.) Test your function with cases in which (1) the first array is exhausted first, (2) the second array is exhausted first, and (3) the two arrays are exhausted at the same time (that is, they end with the same value). Remember that the arrays input to this function must already be sorted.

CODE THAT NEED TO BE WRITTEN IN A DIFFERENT WAY

#include <iostream>

using namespace std;

//Function to merge 2 sorted arrays
int * merge(int *a1, int n1, int *a2, int n2){
//Initialize array to store merged array
static int ans[100];
int i1=0,i2=0,ind=0;
//Loop through both arrays
while(i1<n1&&i2<n2){
//Add minimum element to the array
if(a1[i1]<a2[i2]){
ans[ind++] = a1[i1++];
}
else{
ans[ind++] = a2[i2++];
}
}
//Add remaining elements to the array
while(i1<n1){
ans[ind++] = a1[i1++];
}
while(i2<n2){
ans[ind++] = a2[i2++];
}
return ans;
}

int main()
{
int a1[] = {1,3,5,8};
int a2[] = {2,4,6,7,9,10};
int *ans = merge(a1,4,a2,6);
for(int i=0;i<10;i++){
cout<<ans[i]<<" ";
}
cout<<endl;
return 0;
}

Homework Answers

Answer #1
  • There is a function called std::sort() that can do the sorting job. We have to put a1 and a2 one after other in a new array and sort the new array using this function.

Modified program:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int * merge(int *a1, int n1, int *a2, int n2){
int* a3 = new int[n1+n2];
a3 = a1; // assign starting address to a1
a2 = a3+n1; // assign address of n1th element to staring address to a2
sort(a3, a3+n1+n2);
return a3;
}

int main()
{
int a1[] = {1,3,5,8};
int a2[] = {2,4,6,7,9,10};
int *ans = merge(a1,4,a2,6);
for(int i=0;i<10;i++){
cout<<ans[i]<<" ";
}
cout<<endl;
return 0;
}

output:

Know the answer?
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
Similar Questions
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Do a theta analysis and count the number of computations it performed in each function/method of...
Do a theta analysis and count the number of computations it performed in each function/method of the following code: import java.io.*; import java.util.Scanner; class sort { int a[]; int n; long endTime ; long totalTime; long startTime; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public sort(int nn) // Constructor { a = new int[nn]; n = nn; endTime= 0; totalTime =0; startTime =0; } public static void main(String args[]) throws IOException { System.out.print("\nEnter number of students: "); int nn =...
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...
Please Use C++ I tried to calculate complex number by using *= and operator /= but...
Please Use C++ I tried to calculate complex number by using *= and operator /= but I got an incorrect result compared with the result of complex number calculator For example, When I calculate ( (c5 *= c4) *= c4) by using my operator function, the result was 1.08288e+06+1.11262e+07i on output, However, when using a complex calculator, the result was = −253987.448 − 355181.112i, so I got the wrong answer There is my code below. It compiles well, but my...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
please can you make it simple. For example using scanner or hard coding when it is...
please can you make it simple. For example using scanner or hard coding when it is a good idea instead of arrays and that stuff.Please just make one program (or class) and explain step by step. Also it was given to me a txt.htm 1.- Write a client program and a server program to implement the following simplified HTTP protocol based on TCP service. Please make sure your program supports multiple clients. The webpage file CS3700.htm is provided. You may...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT