Question: It is a C++ code programming problem. All of the functions you must write take at least two parameters: an array of strings, and the
It is a C++ code programming problem.
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", "howard", "cersei" }; int i = findLastOccurrence(folks, 5, "howard"); // 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 findLastOccurrence 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 = findLastOccurrence(people, 25, "cersei"); // Bad call of function, but // your 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.
Your task
Here are the functions you must implement:
int locateMinimum( const string array[ ],int n ); Return the index of the smallest item found in the array or -1 if n <= 0. For example, for the array people[ 5 ] shown above, locateMinimum( people, 5 ) should return the value 3, corresponding to the index of "daenerys". If there are multiple duplicate minimum values, please return the smallest index that has this smallest item.
int findLastOccurrence( const string array[ ], int n, string target ); Return the largest index that holds the target value in the array or -1 if it is not found at all or n <= 0. For example, for the array string data[ 4 ] = { "howard", "ucla", "howard", "ucla" }; findLastOccurrence( data, 4, "howard" ) should return the value 2 while findLastOccurrence( data, 4, "cs 31" ) should return -1. int flipAround( string array[ ],int n ); Flips the items found in the array so that the first item holds what was originally found in the last item, the last item holds what was originally found in the first item, and so on throughout the array. The function should keep track of all the flips it performs and return that count. For example, for the array people[ 5 ] shown above, flipAround( people, 5 ) should adjust the array so that it now holds { "tyrion", "daenerys", "margaery", "jon", "samwell" }; and should return 2 (because it performed the flip of samwell and tyrion and the flip of jon and daenerys).
bool hasNoDuplicates( const string array[ ], int n ); If every value in the array is unique and unduplicated, return true otherwise false or if n < 0 return false. For example, for the array people[ 5 ] shown above, hasNoDuplicates( people, 5 ) should return true. For example, for the array people[ 5 ] shown above, hasNoDuplicates( people, 0 ) should return true because the empty array indeed has no duplicates either.
void unionWithNoDuplicates( const string array1[ ], int n1, const string array2[ ], int n2, string resultingString[ ], int&resultingSize ); Create a new array by combining together all the items of array1 and array2, while ensuring that the new array has no duplicated values. If either n1 or n2 is less than or equal to zero, set the resultingSize to -1. For example, for the array string data[ 4 ] = { "howard", "ucla", "howard", "ucla" }; and the array people[ 5 ] shown above, then unionWithNoDuplicates( people, 5, data, 4, result, size ) should adjust the result array to hold the values { "samwell", "jon", "margaery", "daenerys", "tyrion", "howard", "ucla" }; and sizeshould have the value 7. For example, unionWithNoDuplicates( data, 4, people, 3, result, size ) should adjust the result array to hold the values { "howard", "ucla", "samwell", "jon", "margaery" }; and size should have the value 5. As the examples attempt to show, the elements coming from array1 should be listed before any elements coming from array2. (Note: Please be sure that your driver code has allocated enough array memory for the resultingString array to hold all the elements that this function plans to store into it)
int shiftRight( string array[ ], int n, int amount, string placeholderToFillEmpties ); Adjust the items found in the array, shifting each value to the right by amount parameter, filling the resulting first amount elements of the array with the placeholder parameter and returning the number of original array items still remaining in the array after all the shifting has been performed. For example, for the array people[ 5 ] shown above, shiftRight( people, 5, 3, "foo" ) should return 2 and adjust the array to have the value { "foo", "foo", "foo", "samwell", "jon" }; If the amount parameter is less than zero or exceeds n, return -1.
bool isInIncreasingOrder( const string array[ ], int n ); If every value in the array is larger than the one that precedes it, return true otherwise false or if n < 0 return false . For example, for the array people[ 5 ] shown above, hasNoDuplicates( people, 5 ) should return false. When passed an array of size 0 or 1, isInIncreasingOrder( ... ) will return true, since the function will not find a pair of values that fail to meet the ordering criteria.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
