Project 4 Ucla Cs 31
Project 4 Ucla Cs 31
Project 4 Ucla Cs 31
Programming Assignment 4
Ask Your Boss for Arrays
Time due: 11:00 PM Tuesday, November 6
Before you ask questions about this specification, see if your question has already been
addressed by the Project 4 FAQ. And read the FAQ before you turn in this project, to be
sure you didn't misinterpret anything.
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 cerr instead 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 people[5] = { "dianne", "fiona", "ed", "xavier",
"greg" };
int i = lookup(people, 3, "greg"); // should return -1
(not found)
even though the array has 5 elements, we're telling the function that only
the first 3 have values we're interested in for this call; 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 lookup can't possibly know that the caller is lying about the
number of interesting items in the array:
string people[5] = { "dianne", "fiona", "ed", "xavier",
"greg" };
int i = lookup(people, 25, "eleni"); // Bad call of
function, but
// your lookup 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 appendToAll(string a[], int n, string value);
Append value to the end of each of the n elements of the array and
return n. [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.] For example:
string people[5] = { "dianne", "fiona", "ed", "xavier",
"greg" };
int j = appendToAll(people, 5, "!!!"); // returns 5
// now people[0] is "dianne!!!", people[1] is
"fiona!!!", ...,
// and people[4] is "greg!!!"
int lookup(const string a[], int n, string target);
Return the position of a string in the array that is equal to target; if
there is more than one such string, return the smallest position number of
such a matching string. Return −1 if there is no such string. As noted
above, case matters: Do not consider "GReg" to be equal to "gReG".
int main()
{
string h[7] = { "greg", "gavin", "ed", "xavier",
"", "eleni", "fiona" };
assert(lookup(h, 7, "eleni") == 5);
assert(lookup(h, 7, "ed") == 2);
assert(lookup(h, 2, "ed") == -1);
assert(positionOfMax(h, 7) == 3);