Your assignment is to produce a library that provides functions for many common
ID: 3878738 • Letter: Y
Question
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
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:
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 end unchanged and return false. Here's an example:
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:
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:
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:
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:
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:
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:
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:
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:
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:
As with Project 3, 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:
Explanation / Answer
int main() //main routine
{
//you can write here any instructions you want the main to do//
}
int enumerate(const string a[], int n, string target)
{
int j,k;
char str1[20];
if(n<0||n>sizeof(a))
return -1;
for(int i=0;i<n;i++)
{
str1=a[i];
while(str1[j]!='')
{
j++;
}
while(target[k]!='')
{
k++;
}
if(k!=j)
flag=0;
else
{
int c=0;
for(j=0,k=0;str1[j]!='',target[k]!='';j++,k++)
{
if(str1[j]==target[k])
{
flag=1;
}
c++; //counter variable initially set to zero
}
}
if(flag==0)
{ return c; }
else
{ return -1;}
}
}
}
bool locateSequence(const string a[], int n, string target, int& begin, int& end)
{
char str1[20];
if(n<0||n>sizeof(a))
return false;
int m=0,l=-1,x=0,begin=-1;
while(x<n)
{
str1=a[x];
returnvalue=enumerate(str1,sizeof(str1), target);
if(returnvalue!=0&&begin==-1)
begin=m;
if(returnvalue!=0)
l++;
}
end=l;
if(begin!=-1)
return true;
else
return false;
}
int locationOfMin(const string a[], int n)
{
int flag=0,I,j;
if(n<0||n>sizeof(a)) //negative size of array condition
return -1;
for(i=0;i<n;i++)
{
str1=a[i];
for(j=0;j<n;j++)
{
if(i==j)
continue; //we don't need to compare the string with itself
str2=a[j];
if(compare(str1,str2,sizeof(str1),sizeof(str2))!=-1)
{
break;
flag=1;
}
}
if(flag==0)
return i;
}
return -1;
}
int moveToEnd(string a[], int n, int pos)
{
int i=pos;
if(n<0||n>sizeof(a)) //negative size of array condition
return -1;
while(i!=n-1)
{
a[i]=a[i+1];
i++;
}
a[i]=a[pos];
return pos;
}
int moveToBeginning(string a[], int n, int pos)
{
int i=pos;
if(n<0||n>sizeof(a))
return -1;
while(i!=0)
{
a[i]=a[i-1];
i--;
}
a[i]=a[pos];
return pos;
}
int locateDifference(const string a1[], int n1, const string a2[], int n2)
{
if(n1<0||n2<0||n1>sizeof(a1)||n2>sizeof(a2)) //negative size of array condition
return -1;
for(i=0,j=0;i<n1,j<n2;i++,j++){
str1=a1[i];
str2=a2[j];
if(compare(str1,str2)!=0)
return i;
}
if(n1<=n2)
return n1;
else
return n2;
}
int eliminateDups(string a[], int n)
{
int i,j,k;
if(n<0||n>sizeof(a))
return -1;
for(i=0;i<n;i++)
{
str1=a[i];
for(j=0;j<i;j++)
{
str2=a[j];
if(compare(str1,str2)==0)
{
for(k=1;k<n;k++)
{
a[k]=a[k+1];
}
n=k;
j--;
}
}
}
return n;
}
bool subsequence(const string a1[], int n1, const string a2[], int n2)
{
int i=0,j,k=0;
if(n1<0||n2<0||n1>sizeof(a1)||n2>sizeof(a2))
return -1;
while(i<n2)
{
str2=a2[i];
for(j=k;j<n1;j++)
{
str1=a[j];
if(compare(str1,str2)==0)
{i++;k=j+1;}
}
}
if(i==n2 && j<=n1)
return true;
else
return false;
}
int makeMerger(const string a1[], int n1, const string a2[], int n2,
string result[], int max)
{
int i = 0, j = 0, k = 0;
if(n1<0||n2<0||n1>sizeof(a1)||n2>sizeof(a2))
return -1;
if(n1+n2>max)
return -1;
while (i<n1 && j <n2)
{
if (compare(a1[i],a2[j])==-1)
result[k++] = a1[i++];
else
result[k++] = a2[j++];
}
// Store remaining elements of first array
while (i < n1)
result[k++] = a1[i++];
// Store remaining elements of second array
while (j < n2)
result[k++] = a2[j++];
return n1+n2;
}
int divide(string a[], int n, string divider)
{
int c=0,i,k=n-1,t;
if(n<0||n>sizeof(a))
return -1;
for(i=0;i<n;i++)
{
if(compare(a[i],divider)==1)
{
t=a[k--];
a[k]=a[i];
a[i]=t;
}
}
for(i=0;i<n;i++)
{
if(compare(a[i],divider)!=-1)
c++;
}
if(c!=0)
return c;
else
return n;
}
//extra function written to compare two strings
int compare(string str1,int n1,string str2,int n2)
{
inti,j;
for(i=0,j=0;i<n1,j<n2;i++,j++)
{
if(str1[i]<str2[j])
return -1;
else if(str1[i]>str2[j])
return 1;
else{
i++;j++;
}
}
if(i==n1&&j<n2)
return -1;
else if(i<n1&&j==n2)
return 1;
else
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.