As you gain experience with arrays, youll 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). Youll find that its 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 each function you must write, this specification will tell you its interface (what parameters it takes, what it returns, and what it must do). Its 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. Youll 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 folks[8] = { samwell, jon, margaery, daenerys, tyrion, sansa, llewmas, noj }; bool b = hasReverse( folks, 5 ); // should return false and not inspect the last three elements. 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 any of the others.
The one error your function implementations dont have to handle (because they cannot) 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 cant possibly know the caller is lying about the number of items in the array: string people[5] = { jon, mamabbcc!, samwell,, daenerys., tyrion }; bool b = hasReverse(people, 25 ); // Bad driver code call // your implementation doesnt have to check for this, because it cant
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 dont be surprised by that result. 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 passed array or -1 if n <= 0. For example, for the array people[ 5 ] shown above,
locateMaximumlocateMinimum( people, 5 ) should return the value 3, corresponding to the index of daenerys.. If there are multiple duplicate minimum values, return the smallest index that has this minimum value. The minimum value is determined by its dictionary-sorted order which is what < and > use in C++ to determine true and false.int countPunctuation( const string array[ ], int n );Return the number of punctuation characters found inside each of the elements of the passed array or if n <= 0 return -1. Punctuation characters to be counted should include: , . ; ? ! : ( ) namely, comma, period, semicolon, dash, question mark, exclamation mark, colon, double quote and parentheses. For example, for the array people[ 5 ] shown above, countPunctuation( people, 5 ) should return 3. For the array people[ 5 ] shown above, countPunctuation( people, 1 ) should return 0 because the first element has no punctuation characters.
bool hasReverse( const string array[ ], int n );If there is an element in the passed array whose value is the reverse of an existing value found in the array, return true otherwise false or if n <= 0 return false . For example, for the array people[ 5 ] shown above, hasReverse( people, 5 ) should return false. For example, for the array folks[ 8 ] shown above, hasReverse( folks, 8 ) should return true because samwell and its reverse llewmas are in the array as well as jon and noj.
char highestOccurredCharacter( const string array[ ], int n, int index );For the element found at position index in the passed array, return the character that occurred more frequently than any other character in the identified string element. Unlike the other functions in this assignment, your code should only inspect a single string, not an entire array. If multiple characters occur an identical number of times, your function can decide which of these characters to return. For example, for the array people[ 5 ] shown above, highestOccurredCharacter( people, 5, 0 ) can return either j or o or n. For example, for the array people[ 5 ] shown above, highestOccurredCharacter( people, 5, 2 ) should return l. If n <= 0 or index >= n or index < 0, return which is the NULL character.
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, isInIncreasingOrder( people, 5 ) should return false. For example, for the array people[ 5 ] shown above, isInIncreasingOrder( people, 3 ) should return true.
-
char firstNonRepeatedCharacter( const string array[ ], int n, int index );For the element found at position index in the passed array, return the first non-repeated character found in the identified string element. Unlike the other functions in this assignment, your code should only inspect a single string, not an entire array. For example, for the array people[ 5 ] shown above, firstNonRepeatedCharacter( people, 5, 1 ) should return !. For example, for the array people[ 5 ] shown above, firstNonRepeatedCharacter( people, 5, 0 ) should return j. If n <= 0 or index >= n or index < 0, return which is the NULL character.
-
bool isOnlyDigits( const string array[ ], int n );If every element in the passed array is comprised of only digit characters 0-9, return true otherwise false or if n <= 0 return false. An example of using this function is shown below.
-
Additionally, I have created a testing tool called CodeBoard to help you check your code. CodeBoard enables you to be sure you are naming things correctly by running a small number of tests against your code. Passing CodeBoard tests is not sufficient testing so please do additional testing yourself. To access CodeBoard for Project 4, please click the link you see in this week named CodeBoard for Project 4. Inside the file named user_functions.cpp, copy and paste your implementation of the assigned functions. CodeBoard uses its own main( ) to run tests against your code. Click Compile and Run. However please be aware that no editing changes can be saved inside CodeBoard. In this anonymous user configuration, CodeBoard is read-only and does not allow for saving changes.
-
Programming Guidelines
Your program must not use any function templates from the algorithms portion of the Standard C++ library or use STL <list> or <vector>. If you dont know what the previous sentence is talking about, you have nothing to worry about. Additionally, your code must not use any global variables which are variables declared outside the scope of your individual functions.
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
What you will turn in for this assignment is a zip file containing these two files and nothing more:
- A text file named array.cpp that contains the source code for your C++ program. Your source code should have helpful comments that explain any non-obvious code.
- A file named report.doc or report.docx (in Microsoft Word format) or report.txt (an ordinary text file) that contains in addition your name and your UCLA Id Number:
- A brief description of notable obstacles you overcame.
- A list of the test data that could be used to thoroughly test your functions, along with the reason for each test. You must note which test cases your program does not handle correctly. (This could happen if you didnt have time to write a complete solution, or if you ran out of time while still debugging a supposedly complete solution.) Notice that most of this portion of your report can be written just after you read the requirements in this specification, before you even start designing your program.
How nice! Your report this time doesnt have to contain any design documentation.
As with Project 3, a nice way to test your functions is to use the assert
facility from the standard library. As an example, heres a very incomplete set of tests for Project 4:
#include <iostream> #include <string> #include <cassert> using namespace std; int main() {
string a[6] = { 123, 456, 789, gamma, beta, delta };
assert(isOnlyDigits(a, 6 ) == false); assert(isOnlyDigits(a, 3 ) == true);
cout << "All tests succeeded" << endl; return( 0 );
}
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.
Make sure that if you were to replace your main routine with the one above, your program would build successfully under both Visual C++ and either clang++ or g++. This means that even if you havent figured out how to implement some of the functions, you must still have some kind of implementations for them, even if those implementations do nothing more than immediately return.
By July 27, there will be links on the class webpage that will enable you to turn in your zip file electronically. Turn in the file by the due time above. Give yourself enough time to be sure you can turn something in, because we will not accept excuses like My network connection at home was down, and I didnt have a way to copy my files and bring them to a SEASnet machine. Theres a lot to be said for turning in a preliminary version of your program and report early (You can always overwrite it with a later submission). That way you have something submitted in case theres a problem later.
-
G31 Build Commands
g31 -c array.cpp g31 array.o -o runnable ./runnable
Reviews
There are no reviews yet.