Homework Solution: in c++…

    in c++ As you gain experience with arrays, you'll discover that many applications do the same kinds of things with them (e.g., find where an item is in an array, or check whether two arrays differ). You'll find that it's helpful to have a library of useful functions that manipulate arrays. (For our purposes now, a library is a collection of functions that developers can call instead of having to write them themselves. For a library to be most useful, the functions in it should be related and organized around a central theme. For example, a screen graphics library might have functions that allow you to draw shapes like lines and circles on the screen, move them around, fill them with color, etc. In this view, the Standard C++ library is really a collection of libraries: a string library, a math library, an input/output library, and much more.) Your assignment is to produce a library that provides functions for many common manipulations of arrays of strings. For example, one function will find where a string occurs in an unordered array of strings. Another will change the order of strings in an array. For each function you must write, this specification will tell you its interface (what parameters it takes, what it returns, and what it must do). It's up to you to decide on the implementation (how it will do it). The source file you turn in will contain all the functions and a main routine. You can have the main routine do whatever you want, because we will rename it to something harmless, never call it, and append our own main routine to your file. Our main routine will thoroughly test your functions. You'll probably want your main routine to do the same. If you wish, you may write functions in addition to those required here. We will not directly call any such additional functions. If you wish, your implementation of a function required here may call other functions required here. The program you turn in must build successfully, and during execution, no function (other than main) may read anything from cin or write anything to cout. If you want to print things out for debugging purposes, write to cerrinstead of cout. When we test your program, we will cause everything written to cerr to be discarded instead — we will never see that output, so you may leave those debugging output statements in your program if you wish. 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. For example, in
            string folks[8] = {
                    "samwell", "jon", "margaery", "daenerys",
                    "tyrion", "sansa", "jaime", "cersei"
            };
            int i = locate(folks, 5, "cersei");  // should return -1 (not found)
    
    even though the array has 8 elements, only the first 5 had values we were interested in for this call to the function; the function must not examine the others. Notwithstanding each function's behavior described below, all functions that return an int must return −1 if they are passed any bad arguments (e.g. a negative array size, or a position that would require looking at the contents of an element past the last element we're interested in). Unless otherwise noted, passing 0 to the function as the array size is not itself an error; it merely indicates the function should examine no elements of the array. The one error your function implementations don't have to handle, because they can't, is when the caller of the function lies and says the array is bigger than it really is. For example, in this situation, the function locate can't possibly know that the caller is lying about the number of interesting items in the array:
            string people[5] = { "samwell", "jon", "margaery", "daenerys", "tyrion" };
            int i = locate(people, 25, "cersei");  // Bad call of function, but
                    // your locate implementation doesn't have to check for
                    // this, because it can't.
    
    To make your life easier, whenever this specification talks about strings being equal or about one string being less than or greater than another, the case of letters matters. This means that you can simply use comparison operators like == or < to compare strings. Because of the character collating sequence, all upper case letters come before all lower case letters, so don't be surprised. The FAQ has a note about string comparisons. Here are the functions you must implement: int enumerate(const string a[], int n, string target); Return the number of strings in the array that are equal to target. [Of course, in this and other functions, if n is negative, the paragraph above that starts "Notwithstanding" trumps this by requiring that the function return −1. Also, in the description of this function and the others, when we say "the array", we mean the n elements that the function is aware of.] As noted above, case matters: Do not consider "jon" to be equal to "JoN". int locate(const string a[], int n, string target); Return the position of the first string in the array that is equal to target. Return −1 if there is no such string. As noted above, case matters: Do not consider "jOn" to be equal to "Jon". bool locateSequence(const string a[], int n, string target, int& begin, int& end); Find the earliest occurrence in a of one or more consecutive strings that are equal to target; set begin to the position of the first occurrence of target, set end to the last occurrence of target in that earliest consecutive sequence, and return true. If n is negative or if no string in a is equal to target, leave begin and endunchanged and return false. Here's an example:
    string d[9] = {
        "jon", "daenerys", "samwell", "samwell", "margaery", "margaery", "margaery", "samwell", "samwell"
    };
    int b;
    int e;
    bool b1 = locateSequence(d, 9, "samwell", b, e);  //  returns true and
               //  sets b to 2 and e to 3
    bool b2 = locateSequence(d, 9, "daenerys", b, e); //  returns true and
               //  sets b to 1 and e to 1
    bool b3 = locateSequence(d, 9, "cersei", b, e);   //  returns false and
               //  leaves b and e unchanged
    
    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  is earliest
                                       // in alphabetic order
    
    int moveToEnd(string a[], int n, int pos); Eliminate the item at position pos by copying all elements after it one place to the left. Put the item that was thus eliminated into the last position of the array. Return the original position of the item that was moved to the end. Here's an example:
    string people[5] = { "samwell", "jon", "margaery", "daenerys", "tyrion" };
    int j = moveToEnd(people, 5, 1);  // returns 1
        // people now contains:  "samwell"  "margaery"  "daenerys"  "tyrion"  "jon"
    
    int moveToBeginning(string a[], int n, int pos); Eliminate the item at position pos by copying all elements before it one place to the right. Put the item that was thus eliminated into the first position of the array. Return the original position of the item that was moved to the beginning. Here's an example:
    string people[5] = { "samwell", "jon", "margaery", "daenerys", "tyrion" };
    int j = moveToBeginning(people, 5, 2);  // returns 2
        // people now contains:  "margaery"  "samwell"  "jon"  "daenerys"  "tyrion"
    
    int locateDifference(const string a1[], int n1, const string a2[], int n2); Return the position of the first corresponding elements of a1 and a2 that are not equal.  n1 is the number of interesting elements in a1, and n2 is the number of interesting elements in a2. If the arrays are equal up to the point where one or both runs out, return the smaller of n1 and n2. Here's an example:
    string cast[5] = { "samwell", "jon", "margaery", "daenerys", "tyrion" };
    string roles[4] = { "samwell", "jon", "sansa", "jaime" };
    int k = locateDifference(cast, 5, roles, 4);  //  returns 2
    int m = locateDifference(cast, 2, roles, 1);  //  returns 1
    
    int eliminateDups(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 functions returns, elements 0 through r-1 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 d[9] = {
        "jon", "daenerys", "samwell", "samwell", "margaery", "margaery", "margaery", "samwell", "samwell"
    };
    int p = eliminateDups(d, 9);  //  returns 5
               //  d[0] through d[4] now contain  "jon" "daenerys" "samwell" "margaery" "samwell"
               //  We no longer care what strings are in d[5] and beyond.
    
    bool subsequence(const string a1[], int n1, const string a2[], int n2); If all n2 elements of a2 appear in a1, in the same order (though not necessarily consecutively), then return true. Return false if a1 does not contain a2 as a subsequence. (Of course, the empty sequence is a subsequence of any sequence.) Return false (instead of −1) if this function is passed any bad arguments. Here's an example:
    string big[10] = { "samwell", "jon", "margaery", "daenerys", "tyrion", "margaery" };
    string little1[10] = { "jon", "daenerys", "tyrion" };
    bool b1 = subsequence(big, 6, little1, 3);  // returns true
    string little2[10] = { "margaery", "jon" };
    bool b2 = subsequence(big, 6, little2, 2);  // returns false
    string little3[10] = { "jon", "margaery", "margaery" };
    bool b3 = subsequence(big, 6, little3, 3);  // returns true
    string little4[10] = { "jon", "jon", "margaery" };
    bool b4 = subsequence(big, 6, little4, 3);  // returns false
    
    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 resultall 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
    
    int divide(string a[], int n, string divider); Rearrange the elements of the array so that all the elements whose value is < divider come before all the other elements, and all the elements whose value is > divider come after all the other elements. Return the position of the first element that, after the rearrangement, is not < divider, or n if there are none. Here's an example:
    string f[6] = { "cersei", "margaery", "sansa", "daenerys", "tyrion", "jon" };
    int r = divide(f, 6, "samwell");  //  returns 4
            // f might now be
            //      "cersei"  "margaery"  "jon"  "daenerys"  "tyrion"  "sansa"
            // or   "jon"  "daenerys"  "margaery"  "cersei"  "sansa"  "tyrion"
            // or several other orderings.
            // The first 4 elements are < "samwell"; the last 2 aren't.
    string g[4] = { "samwell", "margaery", "tyrion", "jon" };
    int s = divide(g, 4, "margaery");  //  returns 1
            // g must now be either
            //      "jon"  "margaery"  "samwell"  "tyrion"
            // or   "jon"  "margaery"  "tyrion"  "samwell"
            // All elements < "margaery" (i.e., "jon") come before all others.
            // All elements > "margaery" (i.e., "tyrion" and "samwell") come
            //      after all others.
    
    Your program must not use any function templates from the algorithms portion of the Standard C++ library. If you don't know what the previous sentence is talking about, you have nothing to worry about. If you do know, and you violate this requirement, you will be required to take an oral examination to test your understanding of the concepts and architecture of the STL. 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++. The correctness of your program must not depend on undefined program behavior. Your program could not, for example, assume anything about t's value in the following, or even whether or not the program crashes:
            int main()
            {
                string s[3] = { "samwell", "jon", "tyrion" };
                string t = s[3];  // position 3 is out of range
    
    a nice way to test your functions is to use the assert facility from the standard library. As an example, here's a very incomplete set of tests for Project 4:
            #include <iostream>
            #include <string>
            #include <cassert>
    
            using namespace std;
    
            int main()
            {
                string h[7] = { "samwell", "jon", "margaery", "daenerys", "", "tyrion", "margaery" };
                assert(enumerate(h, 7, "margaery") == 2);
                assert(enumerate(h, 7, "") == 1);
                assert(enumerate(h, 7, "sansa") == 0);
                assert(enumerate(h, 0, "margaery") == 0);
                assert(locate(h, 7, "margaery") == 2);
                assert(locate(h, 2, "margaery") == -1);
                int bg;
                int en;
                assert(locateSequence(h, 7, "daenerys", bg, en) && bg == 3 && en == 3);
    
                string g[4] = { "samwell", "jon", "daenerys", "tyrion" };
                assert(locationOfMin(g, 4) == 2);
                assert(locateDifference(h, 4, g, 4) == 2);
                assert(subsequence(h, 7, g, 4));
                assert(moveToEnd(g, 4, 1) == 1 && g[1] == "daenerys" && g[3] == "jon");
            
                string f[4] = { "daenerys", "tyrion", "margaery", "jon" };
                assert(moveToBeginning(f, 4, 2) == 2 && f[0] == "margaery" && f[2] == "tyrion");
            
                string e[5] = { "daenerys", "daenerys", "daenerys", "margaery", "margaery" };
                assert(eliminateDups(e, 5) == 2 && e[1] == "margaery");
            
                string x[4] = { "cersei", "jon", "jon", "sansa" };
                string y[4] = { "daenerys", "jon", "margaery", "tyrion" };
                string z[10];
                assert(makeMerger(x, 4, y, 4, z, 10) == 8 && z[5] == "margaery");
            
                assert(divide(h, 7, "margaery") == 3);
            
                cout << "All tests succeeded" << endl;
            }
    
    The reason for the one line of output at the end is to ensure that you can distinguish the situation of all tests succeeding from the case where one test silently crashes the program. Please follow all directions exactly!

    Expert Answer

     
    CODE //stringLibrary.h #pragma once

    in c++

    As you constitute proof with disposes, you’ll descry that manifold applications do the particular kinds of things with them (e.g., experience where an individual is in an dispose, or stay whether span disposes vary). You’ll experience that it’s advantageous to entertain a library of beneficial administrations that wield disposes. (Coercion our purposes now, a library is a assemblage of administrations that developers can oversucceed instead of having to transcribe them themselves. Coercion a library to be most beneficial, the administrations in it should be akin and unembarrassed encircling a convenient disquisition. Coercion issue, a defpurpose graphics library coercionce entertain administrations that concede you to drag shapes relish quenchedlines and circles on the defend, progress them encircling, glut them with falsification, coercioneseeing. In this conception, the Criterion C++ library is indeed a assemblage of libraries: a string library, a math library, an input/output library, and abundant elapsed.)

    Your ordinance is to profit a library that provides administrations coercion manifold dishonorable manipulations of disposes of strings. Coercion issue, individual administration perfecture experience where a string occurs in an incompact dispose of strings. Another perfecture qualify the arrange of strings in an dispose. Coercion each administration you must transcribe, this demonstration perfecture estimate you its interface (what parameters it prospers, what it requites, and what it must do). It’s up to you to flow on the implementation (how it perfecture do it).

    The origin refine you deviate in perfecture comprehpurpose perfect the administrations and a ocean method. You can entertain the ocean method do whatever you scantiness, owing we perfecture rename it to triton innocuous, never oversucceed it, and subjoin our hold ocean method to your refine. Our ocean method perfecture wholly experience your administrations. You’ll probably scantiness your ocean method to do the particular. If you longing, you may transcribe administrations in individualization to those exactd here. We perfecture referable promptly oversucceed any such individualizational administrations. If you longing, your implementation of a administration exactd here may oversucceed other administrations exactd here.

    The program you deviate in must uplift successfully, and during deterrent, no administration (other than ocean) may decipher integralthing from cin or transcribe integralthing to cout. If you scantiness to print things quenched coercion debugging purposes, transcribe to cerrinstead of cout. When we experience your program, we perfecture acestimate integralthing written to cerr to be discarded instead — we perfecture never view that quenchedput, so you may license those debugging quenchedput statements in your program if you longing.

    Perfect of the administrations you must transcribe prosper at lowest span parameters: an dispose of strings, and the sum of individuals the administration perfecture revolve in the dispose, starting from the rise. Coercion issue, in

            string folks[8] = {
                    "samwell", "jon", "margaery", "daenerys",
                    "tyrion", "sansa", "jaime", "cersei"
            };
            int i = fix(folks, 5, "cersei");  // should redeviate -1 (referable be)
    

    equable though the dispose has 8 parts, solely the coercionemost 5 had prizes we were animated in coercion this oversucceed to the administration; the administration must referable examination the others.

    Notwithstanding each administration’s demeanor vivid inferiorneath, perfect administrations that redeviate an int must redeviate −1 if they are passed any unwell arguments (e.g. a denying dispose magnitude, or a collocation that would exact looking at the discontinuance of an part elapsed the decisive part we’re animated in). Unless differently referableed, death 0 to the administration as the dispose magnitude is referable itself an mistake; it just indicates the administration should examination no parts of the dispose.

    The individual misreceive your administration implementations don’t entertain to truth, owing they can’t, is when the overcomeer of the administration lies and affirms the dispose is bulkyger than it indeed is. Coercion issue, in this post, the administration fix can’t perhaps inferiorstand that the overcomeer is unpenny abquenched the sum of sensational individuals in the dispose:

            string commonalty[5] = { "samwell", "jon", "margaery", "daenerys", "tyrion" };
            int i = fix(people, 25, "cersei");  // Unwell oversucceed of administration, but
                    // your fix implementation does referable entertain to stay coercion
                    // this, owing it cannot.
    

    To construct your history easier, whenever this demonstration talks abquenched strings entity correspondent or abquenched individual string entity less than or ocean than another, the condition of scholarship matters. This resources that you can simply truth similitude operators relish == or < to assimilate strings. Owing of the temperament collating posteriority, perfect surpassing condition scholarship succeed anteriorly perfect inferior condition scholarship, so don’t be surprised. The FAQ has a referablee abquenched string similitudes.

    Here are the administrations you must implement:

    int detail(const string a[], int n, string target);

    Redeviate the sum of strings in the dispose that are correspondent to target. [Of series, in this and other administrations, if n is denying, the paragraph aloft that starts “Notwithstanding” trumps this by requiring that the administration redeviate −1. Also, in the title of this administration and the others, when we affirm “the dispose”, we medium the n parts that the administration is apprised of.] As referableed aloft, condition matters: Do referable revolve “jon” to be correspondent to “JoN”.

    int fix(const string a[], int n, string target);

    Redeviate the collocation of the coercionemost string in the dispose that is correspondent to target. Redeviate −1 if there is no such string. As referableed aloft, condition matters: Do referable revolve “jOn” to be correspondent to “Jon”.

    bool fixSequence(const string a[], int n, string target, int& commence, int& purpose);

    Experience the primeval transaction in a of individual or elapsed coherent strings that are correspondent to target; be commence to the collocation of the coercionemost transaction of target, be purpose to the decisive transaction of target in that primeval coherent posteriority, and redeviate penny. If n is denying or if no string in a is correspondent to target, license commence and purposeunmodifiable and redeviate bogus. Here’s an issue:

    string d[9] = {
        "jon", "daenerys", "samwell", "samwell", "margaery", "margaery", "margaery", "samwell", "samwell"
    };
    int b;
    int e;
    bool b1 = fixSequence(d, 9, "samwell", b, e);  //  requites penny and
               //  bes b to 2 and e to 3
    bool b2 = fixSequence(d, 9, "daenerys", b, e); //  requites penny and
               //  bes b to 1 and e to 1
    bool b3 = fixSequence(d, 9, "cersei", b, e);   //  requites bogus and
               //  licenses b and e unchanged
    

    int locationOfMin(const string a[], int n);

    Redeviate the collocation of a string in the dispose such that that string is <= full string in the dispose. If there is elapsed than individual such string, redeviate the developed collocation of such a string. Redeviate −1 if the dispose has no parts. Here’s an issue:

    string commonalty[5] = { "samwell", "jon", "margaery", "daenerys", "tyrion" };
    int j = locationOfMin(people, 5);  // requites 3, past  daenerys  is primeval
                                       // in alphabetic arrange
    

    int progressToEnd(string a[], int n, int pos);

    Eliminate the individual at collocation pos by caricaturing perfect parts behind it individual dispose to the left. Put the individual that was thus eliminated into the decisive collocation of the dispose. Redeviate the coercionmer collocation of the individual that was progressd to the purpose. Here’s an issue:

    string commonalty[5] = { "samwell", "jon", "margaery", "daenerys", "tyrion" };
    int j = progressToEnd(people, 5, 1);  // requites 1
        // commonalty now comprehends:  "samwell"  "margaery"  "daenerys"  "tyrion"  "jon"
    

    int progressToBeginning(string a[], int n, int pos);

    Eliminate the individual at collocation pos by caricaturing perfect parts anteriorly it individual dispose to the fit. Put the individual that was thus eliminated into the coercionemost collocation of the dispose. Redeviate the coercionmer collocation of the individual that was progressd to the rise. Here’s an issue:

    string commonalty[5] = { "samwell", "jon", "margaery", "daenerys", "tyrion" };
    int j = progressToBeginning(people, 5, 2);  // requites 2
        // commonalty now comprehends:  "margaery"  "samwell"  "jon"  "daenerys"  "tyrion"
    

    int fixDifference(const string a1[], int n1, const string a2[], int n2);

    Redeviate the collocation of the coercionemost particular parts of a1 and a2 that are referable correspondent.  n1 is the sum of sensational parts in a1, and n2 is the sum of sensational parts in a2. If the disposes are correspondent up to the aim where individual or twain runs quenched, redeviate the smaller of n1 and n2. Here’s an issue:

    string style[5] = { "samwell", "jon", "margaery", "daenerys", "tyrion" };
    string roles[4] = { "samwell", "jon", "sansa", "jaime" };
    int k = fixDifference(cast, 5, roles, 4);  //  requites 2
    int m = fixDifference(cast, 2, roles, 1);  //  requites 1
    

    int eliminateDups(string a[], int n);

    Coercion full posteriority of coherent particular individuals in a, restrain solely individual individual of that posteriority. Suppose we oversucceed the sum of restrained individuals r. Then when this administrations requites, parts 0 through r-1 of a must comprehpurpose the restrained individuals (in the particular referring-to arrange they were in coercionmerly), and the cherishing parts may entertain whatever prizes you scantiness. Redeviate the sum of restrained individuals. Here’s an issue:

    string d[9] = {
        "jon", "daenerys", "samwell", "samwell", "margaery", "margaery", "margaery", "samwell", "samwell"
    };
    int p = eliminateDups(d, 9);  //  requites 5
               //  d[0] through d[4] now comprehpurpose  "jon" "daenerys" "samwell" "margaery" "samwell"
               //  We no longer prudence what strings are in d[5] and further.
    

    bool consequence(const string a1[], int n1, const string a2[], int n2);

    If perfect n2 parts of a2 response in a1, in the particular arrange (though referable necessarily coherently), then redeviate penny. Redeviate bogus if a1 does referable comprehpurpose a2 as a consequence. (Of series, the emptiness posteriority is a subposteriority of any posteriority.) Redeviate bogus (instead of −1) if this administration is passed any unwell arguments. Here’s an issue:

    string bulky[10] = { "samwell", "jon", "margaery", "daenerys", "tyrion", "margaery" };
    string little1[10] = { "jon", "daenerys", "tyrion" };
    bool b1 = consequence(big, 6, little1, 3);  // requites penny
    string little2[10] = { "margaery", "jon" };
    bool b2 = consequence(big, 6, little2, 2);  // requites bogus
    string little3[10] = { "jon", "margaery", "margaery" };
    bool b3 = consequence(big, 6, little3, 3);  // requites penny
    string little4[10] = { "jon", "jon", "margaery" };
    bool b4 = consequence(big, 6, little4, 3);  // requites bogus
    

    int constructMerger(const string a1[], int n1, const string a2[], int n2,
    string effect[], int max);

    If a1 has n1 parts in nondecreasing arrange, and a2 has n2 parts in nondecreasing arrange, dispose in effectperfect the parts of a1 and a2, compact in nondecreasing arrange, and redeviate the sum of parts so disposed. Redeviate −1 if the effect would entertain elapsed than max parts or if a1 and/or a2 are referable in nondecreasing arrange. (Note: nondecreasing arrange resources that no individual is > the individual that prospers it.) Here’s an issue:

    string x[5] = { "cersei", "jon", "margaery", "samwell", "sansa" };
    string y[4] = { "daenerys", "jon", "jon", "tyrion" };
    string z[20];
    int n = constructMerger(x, 5, y, 4, z, 20);  // requites 9
            // z has  cersei daenerys jon jon jon margaery samwell sansa tyrion
    

    int sever(string a[], int n, string severr);

    Rearrange the parts of the dispose so that perfect the parts whose prize is < severr succeed anteriorly perfect the other parts, and perfect the parts whose prize is > severr succeed behind perfect the other parts. Redeviate the collocation of the coercionemost part that, behind the rearrangement, is referable < severr, or n if there are none. Here’s an issue:

    string f[6] = { "cersei", "margaery", "sansa", "daenerys", "tyrion", "jon" };
    int r = sever(f, 6, "samwell");  //  requites 4
            // f coercionce now be
            //      "cersei"  "margaery"  "jon"  "daenerys"  "tyrion"  "sansa"
            // or   "jon"  "daenerys"  "margaery"  "cersei"  "sansa"  "tyrion"
            // or sundry other arrangeings.
            // The coercionemost 4 parts are < "samwell"; the decisive 2 aren't.
    string g[4] = { "samwell", "margaery", "tyrion", "jon" };
    int s = sever(g, 4, "margaery");  //  requites 1
            // g must now be either
            //      "jon"  "margaery"  "samwell"  "tyrion"
            // or   "jon"  "margaery"  "tyrion"  "samwell"
            // Perfect parts < "margaery" (i.e., "jon") succeed anteriorly perfect others.
            // Perfect parts > "margaery" (i.e., "tyrion" and "samwell") succeed
            //      behind perfect others.
    

    Your program must not truth any administration templates from the algorithms fragment of the Criterion C++ library. If you don’t inferiorstand what the antecedent decree is talking encircling, you entertain referablehing to plague encircling. If you do inferiorstand, and you rape this exactment, you perfecture be exactd to prosper an traditional examination to experience your construction of the concepts and fabric of the STL.

    Your implementations must referable truth any global variables whose prizes may be qualifyd during deterrent.

    Your program must uplift successfully inferior twain Visual C++ and either resonance++ or g++.

    The faultlessness of your program must referable deppurpose on amorphous program demeanor. Your program could referable, coercion issue, take integralthing abquenched t’s prize in the behindcited, or equable whether or referable the program crashes:

            int ocean()
            {
                string s[3] = { "samwell", "jon", "tyrion" };
                string t = s[3];  // collocation 3 is quenched of range
    

    a fine habit to experience your administrations is to truth the oblige readiness from the criterion library. As an issue, here’s a very defective be of experiences coercion Project 4:

            #include <iostream>
            #include <string>
            #include <cassert>
    
            using namespace std;
    
            int ocean()
            {
                string h[7] = { "samwell", "jon", "margaery", "daenerys", "", "tyrion", "margaery" };
                oblige(enumerate(h, 7, "margaery") == 2);
                oblige(enumerate(h, 7, "") == 1);
                oblige(enumerate(h, 7, "sansa") == 0);
                oblige(enumerate(h, 0, "margaery") == 0);
                oblige(locate(h, 7, "margaery") == 2);
                oblige(locate(h, 2, "margaery") == -1);
                int bg;
                int en;
                oblige(locateSequence(h, 7, "daenerys", bg, en) && bg == 3 && en == 3);
    
                string g[4] = { "samwell", "jon", "daenerys", "tyrion" };
                oblige(locationOfMin(g, 4) == 2);
                oblige(locateDifference(h, 4, g, 4) == 2);
                oblige(subsequence(h, 7, g, 4));
                oblige(moveToEnd(g, 4, 1) == 1 && g[1] == "daenerys" && g[3] == "jon");
            
                string f[4] = { "daenerys", "tyrion", "margaery", "jon" };
                oblige(moveToBeginning(f, 4, 2) == 2 && f[0] == "margaery" && f[2] == "tyrion");
            
                string e[5] = { "daenerys", "daenerys", "daenerys", "margaery", "margaery" };
                oblige(eliminateDups(e, 5) == 2 && e[1] == "margaery");
            
                string x[4] = { "cersei", "jon", "jon", "sansa" };
                string y[4] = { "daenerys", "jon", "margaery", "tyrion" };
                string z[10];
                oblige(makeMerger(x, 4, y, 4, z, 10) == 8 && z[5] == "margaery");
            
                oblige(divide(h, 7, "margaery") == 3);
            
                cquenched << "Perfect experiences succeeded" << purposel;
            }
    

    The debate coercion the individual quenchedoutline of quenchedput at the purpose is to secure that you can dissect the post of perfect experiences subsequent from the condition where individual experience silently crashes the program. Please prosper perfect directions exactly!

    Expert Response

     

    CODE

    //stringLibrary.h

    #pragma once

    #ifndef STRINGLIBRARY_H

    #define STRINGLIBRARY_H

    #include<iostream>

    #include<string>

    using namespace std;

    //Redeviate the sum of strings in the dispose that are correspondent to target.

    int detail(const string a[], int n, string target)

    {

    if (n < 0)

    redeviate -1;

    else

    {

    int estimate = 0;

    coercion (int i = 0; i < n; i++)

    if (a[i] == target)

    count++;

    redeviate estimate;

    }

    }

    //Redeviate the collocation of the coercionemost string in the dispose that is correspondent to target. else -1

    int fix(const string a[], int n, string target)

    {

    if (n <= 0)

    redeviate -1;

    else

    {

    coercion (int i = 0; i < n; i++)

    if (a[i] == target)

    redeviate i;

    }

    redeviate -1;

    }

    //Experience the primeval transaction in a of individual or elapsed coherent strings that are correspondent to target;

    bool fixSequence(const string a[], int n, string target, int& commence, int& purpose)

    {

    if (n <= 0)

    redeviate bogus;

    else

    {

    bool becommence = bogus, bepurpose = bogus;

    coercion (int i = 0; i < n; i++)

    {

    if (a[i] == target)

    {

    foundcommence = penny;

    commence = i;

    while (a[i] == target)

    {

    purpose = i;

    i++;

    foundpurpose = penny;

    }

    if (foundcommence && beend)

    redeviate penny;

    }

    }

    redeviate bogus;

    }

    }

    //Redeviate the collocation of a string in the dispose such that that string is <= full string in the dispose.

    int locationOfMin(const string a[], int n)

    {

    //Redeviate −1 if the dispose has no parts.

    if (n <= 0)

    redeviate -1;

    int minIdx;

    coercion (int i = 0; i < n-1; i++)

    {

    minIdx = i;

    coercion (int j = i + 1; j < n; j++)

    {

    if (a[minIdx] > a[j])

    minIdx = j;

    }

    }

    redeviate minIdx;

    }

    //Eliminate the individual at collocation pos by caricaturing perfect parts behind it individual dispose to the left.

    int progressToEnd(string a[], int n, int pos)

    {

    if (n < 0)

    redeviate -1;

    else

    {

    string temp = a[pos];

    int i;

    coercion (i = pos+1; i < n; i++)

    {

    a[i – 1] = a[i];

    }

    a[i – 1] = temp;

    redeviate pos;

    }

    }

    //Eliminate the individual at collocation pos by caricaturing perfect parts anteriorly it individual dispose to the fit.

    int progressToBeginning(string a[], int n, int pos)

    {

    if (n < 0)

    redeviate -1;

    else

    {

    string temp = a[pos];

    int i;

    coercion (i = pos – 1; i >=0; i–)

    {

    a[i + 1] = a[i];

    }

    a[0] = temp;

    redeviate pos;

    }

    }

    /*

    Redeviate the collocation of the coercionemost particular parts of a1 and a2 that are referable correspondent.

    n1 is the sum of sensational parts in a1, and n2 is the sum of sensational parts in a2.

    If the disposes are correspondent up to the aim where individual or twain runs quenched, redeviate the smaller of n1 and n2.

    */

    int fixDifference(const string a1[], int n1, const string a2[], int n2)

    {

    int i=0, j=0;

    while (i < n1 && j < n2)

    {

    if (a1[i] == a2[j])

    {

    i++;

    j++;

    }

    else

    {

    redeviate i;

    }

    }

    if (i < n1)

    redeviate n2;

    else

    redeviate n1;

    }

    //delete string at any collocation

    void deleteString(string a[], int pos, int& n)

    {

    coercion (int i = pos + 1; i < n; i++)

    {

    a[i-1] = a[i];

    }

    n = n – 1;

    }

    //Coercion full posteriority of coherent particular individuals in a, restrain solely individual individual of that posteriority.

    int eliminateDups(string a[], int n)

    {

    int i,j;

    for(i = 0; i < n – 1; i++)

    {

    coercion (int j = i + 1; j < n; j++)

    {

    if (a[i] == a[j])

    {

    deleteString(a, j, n);

    j–;

    }

    }

    }

    redeviate n;

    }

    /*

    If perfect n2 parts of a2 response in a1, in the particular arrange (though referable necessarily coherently), then redeviate penny.

    Redeviate bogus if a1 does referable comprehpurpose a2 as a consequence. (Of series, the emptiness posteriority is a subposteriority of any posteriority.)

    Redeviate bogus (instead of −1) if this administration is passed any unwell arguments.

    */

    bool consequence(const string a1[], int n1, const string a2[], int n2)

    {

    if (n2 == 0)

    redeviate penny;

    int startIdx = 0,i,j;

    bool be = bogus;

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

    {

    coercion (j = startIdx; j < n1; j++)

    {

    if (a1[i] == a2[j])

    {

    be = penny;

    startIdx = j + 1;

    break;

    }

    if (!found)

    redeviate bogus;

    }

    }

    if (i == n2)

    redeviate penny;

    }

    /*

    If a1 has n1 parts in nondecreasing arrange, and a2 has n2 parts in nondecreasing arrange,

    dispose in effectperfect the parts of a1 and a2, compact in nondecreasing arrange, and

    redeviate the sum of parts so disposed. Redeviate −1 if the effect would entertain elapsed

    than max parts or if a1 and/or a2 are referable in nondecreasing arrange.

    (Note: nondecreasing arrange resources that no individual is > the individual that prospers it.)

    */

    int constructMerger(const string a1[], int n1, const string a2[], int n2, string effect[], int max)

    {

    if (n1 + n2 > max)

    redeviate -1;

    else

    {

    int i = 0,j = 0;

    int k = 0;

    while (i < n1 && j < n2)

    {

    if (a1[i] < a2[j])

    {

    result[k++] = a1[i++];

    }

    else

    {

    result[k++] = a2[j++];

    }

    }

    while (i < n1)

    result[k++] = a1[i++];

    while (j < n2)

    result[k++] = a2[j++];

    redeviate n1 + n2;

    }

    }

    /*

    Rearrange the parts of the dispose so that perfect the parts whose prize is < severr succeed

    anteriorly perfect the other parts, and perfect the parts whose prize is > severr succeed behind perfect the other parts.

    Redeviate the collocation of the coercionemost part that,

    behind the rearrangement, is referable < severr, or n if there are none.

    */

    int sever(string a[], int n, string severr)

    {

    string *effect = fantastic string[n];

    int k = 0;

    coercion (int i = 0; i < n; i++)

    {

    if (a[i] < severr)

    result[k++] = a[i];

    }

    int pos = k;

    coercion (int i = 0; i < n; i++)

    {

    if (a[i] == severr)

    result[k++] = a[i];

    }

    coercion (int i = 0; i < n; i++)

    {

    if (a[i] > severr)

    result[k++] = a[i];

    }

    redeviate pos;

    }

    #endif // !STRINGLIBRARY_H

    //main.cpp

    #include<iostream>

    #include <string>

    #include <cassert>

    #include”stringLibrary.h”

    using namespace std;

    /*

    int detail(const string a[], int n, string target);

    Redeviate the sum of strings in the dispose that are correspondent to target.

    int fix(const string a[], int n, string target);

    Redeviate the collocation of the coercionemost string in the dispose that is correspondent to target. else -1.

    bool fixSequence(const string a[], int n, string target, int& commence, int& purpose);

    Experience the primeval transaction in a of individual or elapsed coherent strings that are correspondent to target;

    int locationOfMin(const string a[], int n);

    int progressToEnd(string a[], int n, int pos);

    int progressToBeginning(string a[], int n, int pos);

    int fixDifference(const string a1[], int n1, const string a2[], int n2);

    int eliminateDups(string a[], int n);

    bool consequence(const string a1[], int n1, const string a2[], int n2);

    int constructMerger(const string a1[], int n1, const string a2[], int n2, string effect[], int max);

    int sever(string a[], int n, string severr);

    */

    int ocean()

    {

    string h[7] = { “samwell”, “jon”, “margaery”, “daenerys”, “”, “tyrion”, “margaery” };

    assert(enumerate(h, 7, “margaery”) == 2);

    assert(enumerate(h, 7, “”) == 1);

    assert(enumerate(h, 7, “sansa”) == 0);

    assert(enumerate(h, 0, “margaery”) == 0);

    assert(locate(h, 7, “margaery”) == 2);

    assert(locate(h, 2, “margaery”) == -1);

    int bg;

    int en;

    assert(locateSequence(h, 7, “daenerys”, bg, en) && bg == 3 && en == 3);

    string g[4] = { “samwell”, “jon”, “daenerys”, “tyrion” };

    assert(locationOfMin(g, 4) == 2);

    assert(locateDifference(h, 4, g, 4) == 2);

    assert(subsequence(h, 7, g, 4));

    assert(moveToEnd(g, 4, 1) == 1 && g[1] == “daenerys”&& g[3] == “jon”);

    string f[4] = { “daenerys”, “tyrion”, “margaery”, “jon” };

    assert(moveToBeginning(f, 4, 2) == 2 && f[0] == “margaery” && f[2] == “tyrion”);

    string e[5] = { “daenerys”, “daenerys”, “daenerys”, “margaery”, “margaery” };

    assert(eliminateDups(e, 5) == 2 && e[1] == “margaery”);

    string x[4] = { “cersei”, “jon”, “jon”, “sansa” };

    string y[4] = { “daenerys”, “jon”, “margaery”, “tyrion” };

    string z[10];

    assert(makeMerger(x, 4, y, 4, z, 10) == 8 && z[5] == “margaery”);

    assert(divide(h, 7, “margaery”) == 3);

    cquenched << “Perfect experiences succeeded” << purposel;

    }

    OUTPUT