Question
c++ programming please. thx.

Define a function template named merge to merge two lists of items into one list including all the items of the first list
5) releases the memories allocated to the two input lists, and 6) writes all the integers included in the merged list separat
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <new>

#include<fstream>

using namespace std;

void merge(int p1[],int p2[],int n1,int n2)

{

int i,k;

int n3 = n1 + n2;

int * p3;

p3= new (nothrow) int[n3];

for(i=0; i<n1; i++)

  {

    p3[i]=p1[i];

  }

  for(i=0, k=n1; k<n3 && i<n2; i++, k++)

  {

    p3[k]=p2[i];

  }

ofstream ofs("merge.txt",ofstream::out);

for(int i =0;i<n3;i++)

{

ofs<<p3[i]<<" ";

}

ofs.close();

delete[] p3;

}

int main ()

{

int i,n1,n2,k;

int * p1;

int * p2;

cout << "Enter size of first list: ";

cin >> n1;

p1= new (nothrow) int[n1];

cout<<"Enter the integers included in the first list: ";

for (i=0; i<n1; i++)

{

cin >> p1[i];

}

cout << "Enter size of second list: ";

cin >> n2;

p2= new (nothrow) int[n2];

cout<<"Enter the integers included in the second list: ";

for (i=0; i<n2; i++)

{

cin >> p2[i];

}

merge(p1,p2,n1,n2);

delete[] p1;

delete[] p2;

return 0;

}

main.cpp 2 saving... 1 #include <iostream> #include <new> #include<fstream> using namespace std; void merge(int p1[], int p2[

int main() int i,n1, n2,k; int * p1; int * P2; cout << Enter size of first list: ; cin >> n1; p1= new (nothrow) int[n1]; co

merge (p1, p2, n1, n2); delete[] p1; delete[] p2; return 0; 61 }

clang version 7.0.0-3~ubuntu0.18.04.1 (tags/RELEASE 700/final) ? clang++-7 -pthread -std=c++17 -o main main.cpp ./main Enter

Files : merge.txt saved 1 1 2 3 4 5 6 7 8 9 main.cpp merge.txt

########################################################

Give a ThumbsUp if you found this answer Helpful :)
#######################################################

################################################################
............ Still having trouble understanding the answer ? Feel free to ask in the comment section.......

Add a comment
Know the answer?
Add Answer to:
c++ programming please. thx. Define a function template named "merge" to merge two lists of items...
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
  • a) 7% Define a function (the function definition only) named combine_lists that receives two lists of...

    a) 7% Define a function (the function definition only) named combine_lists that receives two lists of integers (the function has 2 parameters). You can assume that each list is the same length and each list is not empty. The function will create a new list which is composed of the addition of each element of the two lists. The function returns the new list. For example, if the function is sent this list: [2,4,6,8,10] and this list: [5,6,7,8,9] then the...

  • When we have two sorted lists of numbers in non-descending order, and we need to merge...

    When we have two sorted lists of numbers in non-descending order, and we need to merge them into one sorted list, we can simply compare the first two elements of the lists, extract the smaller one and attach it to the end of the new list, and repeat until one of the two original lists become empty, then we attach the remaining numbers to the end of the new list and it's done. This takes linear time. Now, try to...

  • Please solve using java. 21. Merge Two Sorted Lists Easy 2705 396 ♡ Favorite Sha *...

    Please solve using java. 21. Merge Two Sorted Lists Easy 2705 396 ♡ Favorite Sha * Definition for singly-linked list. * public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. class Solution { public ListNode merge TwoLists(ListNode li, ListNode 12) { 10 Example: Input: 1->2->4, 1-3-4...

  • C++ Programming question Problem: 5. Write a program that reads in a list of integers into...

    C++ Programming question Problem: 5. Write a program that reads in a list of integers into an array with base type int. Provide the facility to either read this array from the keyboard or from a file, at the user's option. If the user chooses file input, the program should request a file name. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is to be...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

  • Write a program in C++ that uses a class template to create a set of items....

    Write a program in C++ that uses a class template to create a set of items. . . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of...

  • Write you code in a class named HighScores, in file named HighScores.java. Write a program that...

    Write you code in a class named HighScores, in file named HighScores.java. Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look approximately like this: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score...

  • You will be writing some methods that will give you some practice working with Lists. Create...

    You will be writing some methods that will give you some practice working with Lists. Create a new project and create a class named List Practice in the project. Then paste in the following code: A program that prompts the user for the file names of two different lists, reads Strings from two files into Lists, and prints the contents of those lists to the console. * @author YOUR NAME HERE • @version DATE HERE import java.util.ArrayList; import java.util.List; import...

  • Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed...

    Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed as command-line arguments to MergeFiles as follows: java MergeFiles filel file2 mergedFile II. MergeFiles reads names stored in files file and file2 III. Merges the two list of names into a single list IV. Sorts that single list V. Ignores repetitions VI. Writes the sorted, free-of-repetitions list to a new file named mergedFile.txt VII. The names in all three files are stored as one...

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