Question

Please help with this function i'm having trouble with: must be written in c++ All of...

Please help with this function i'm having trouble with: must be written in c++ All of the functions you must write take at least two parameters: an array of strings, and the number of items the function will consider in the array, starting from the beginning. Your implementations must not use any global variables whose values may be changed during execution. Your program must build successfully under both Visual C++ and either clang++ or g++. Your program must not use any function templates from the algorithms portion of the Standard C++ library int makeMerger(const string a1[], int n1, const string a2[], int n2, string result[], int max); If a1 has n1 elements in nondecreasing order, and a2 has n2 elements in nondecreasing order, place in result all the elements of a1 and a2, arranged in nondecreasing order, and return the number of elements so placed. Return −1 if the result would have more than max elements or if a1 and/or a2 are not in nondecreasing order. (Note: nondecreasing order means that no item is > the one that follows it.) Here's an example: string x[5] = { "cersei", "jon", "margaery", "samwell", "sansa" }; string y[4] = { "daenerys", "jon", "jon", "tyrion" }; string z[20]; int n = makeMerger(x, 5, y, 4, z, 20); // returns 9 // z has cersei daenerys jon jon jon margaery samwell sansa tyrion

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

#include <iostream>
using namespace std;
#include<string>
int check_nonDec( string A[],int N)
{
for(int i=0;i<N-1;i++)
{
if(A[i]>A[i+1])
{
return 1;
}
}
return 0;
}

int makeMerger( string a1[], int n1, string a2[], int n2, string result[], int max)
{
if(check_nonDec(a1,n1) || check_nonDec(a2,n2) || max<(n1+n2))
{
return -1;
}
int i=0,j=0;
int k=0;
while(k<(n1+n2))
{
if(i==n1 && j<n2)
{
result[k]=a2[j];
j++;
k++;
continue;
}
if(i<n1 && j==n2)
{
result[k]=a1[i];
i++;
k++;
continue;
}
if(i<n1 && j<n2)
{
if(a1[i]<a2[j] )
{
result[k]=a1[i];
k++;i++;continue;
}
else
{
result[k]=a2[j];
k++;j++;continue;
}
}
}
return n1+n2;
}
int main()
{
cout<<"enter no of elements for a1"<<endl;
int N1;
cin>>N1;
string a1[N1];
cout<<"enter no of elements for a2"<<endl;
int N2;
cin>>N2;
string a2[N2];
cout<<"enter a1 and a2 resp."<<endl;
int non_decreasing=1;
for(int i=0;i<N1;i++)
{
cin>>a1[i];
}
for(int i=0;i<N2;i++)
{
cin>>a2[i];
}
int N3;
cout<<"enter no of elements for z"<<endl;
cin>>N3;
string z[N3];
N3=makeMerger(a1,N1,a2,N2,z,N3);
if(N3!=-1)
{
cout<<"z array contains"<<N3<<" elements"<< endl;
for(int i=0;i<N3;i++)
{
cout<<z[i]<<" ";
}
}
else
{
cout<<"z doesn't have anything.result is "<< N3<<endl;
}

}


Add a comment
Know the answer?
Add Answer to:
Please help with this function i'm having trouble with: must be written in c++ All of...
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
  • Please help with this function i'm having trouble with: must be written in c++ All of...

    Please help with this function i'm having trouble with: must be written in c++ All of the functions you must write take at least two parameters: an array of strings, and the number of items the function will consider in the array, starting from the beginning. Your implementations must not use any global variables whose values may be changed during execution. Your program must build successfully under both Visual C++ and either clang++ or g++. Your program must not use...

  • Can you make this a function? Thank you. int locationOfMin(const string a[], int n): Return the...

    Can you make this a function? Thank you. int locationOfMin(const string a[], int n): Return the position of a string in the array such that that string is < = every string in the array. If there is more than one such string, return the smallest position of such a string. Return -1 if the array has no elements. Here's an example: string people[5] = { "samwell", "jon", "margaery", "daenerys" "tyrion" }: int j = locationOfMin(people, 5);//returns 3, since daenerys...

  • Write function in C++: int removeDups (string a[], int n); For every sequence of consecutive identical...

    Write function in C++: int removeDups (string a[], int n); For every sequence of consecutive identical items in a, retain only one item of that sequence. Suppose we call the number of retained items r. Then when this function returns, elements through of a must contain the retained items (in the same relative order they were in originally), and the remaining elements may have whatever values you want. Return the number of retained items. Here's an example: string [9] =...

  • Hi this is C++, I'm really struggle with it please help me.... ************************ Here is the...

    Hi this is C++, I'm really struggle with it please help me.... ************************ Here is the original high score program,: 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 exactly like this: Enter the name for score #1: Suzy Enter the score for...

  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

  • C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble...

    C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble with is in bold. -------------------------------------------------------------------------------------------------driverProgram.cpp #include #include #include #include #include "quickSort.cpp" using namespace std; int main() { const int MIN_SIZE = 4; //Array size const int SIZE = 25; int theArray[SIZE] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 20, 21, 22, 23, 24, 25}; cout << "List of 25 items: ";...

  • hello, I'm having trouble understanding the problems below for my Computer science class, please include a...

    hello, I'm having trouble understanding the problems below for my Computer science class, please include a work-through and some explanations on how to do it so that I can understand remember how do problems like this on my own. Thank you very much WE ARE USING C++ The following project includes the frequency_start_letter function and a few other functions that work with a string array. Reminder: you could click "Open in Repl.it" on the top right corner to see the...

  • Data Structures and Algorithms C++: I'm having a hard time getting my main.cpp part of the...

    Data Structures and Algorithms C++: I'm having a hard time getting my main.cpp part of the source code (shown below) to output the following: Inserting elements to array list: The list contains 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Deleting elements: The list contains: 2 4 6 8 10 12 14 16 18 20 22 24 this is a programming problem in...

  • Write a complete C++ program that is made of functions main() and rShift(). The rShift() function...

    Write a complete C++ program that is made of functions main() and rShift(). The rShift() function must be a function without return with 6 parameters. rShift() should do following. Shift the first 4 formal parameters' value one place to right circularly and send the updated values out to the caller function, i.e. main. Furthermore, after calling this function with first 4 actual parameters, say a1, a2, a3, a4, and these actual parameters should shift their value one place to right...

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