Arrays

In C, arrays are not considered "first-class objects" - that is, an array cannot be treated as a type. For instance, you cannot have an array returned from a function.

Sara the Cheesemaker has long been frustrated with the non-first-class status of arrays in C. With a maniacal laugh, she announces her intention to remedy this situation. Noting that "C", "cheese", and "CSE" all begin with the letter 'c', she shrewdly recruits the students of section AC to do her dirty work and implement 3* array-manipulation functions which she has already prototyped.


#define TRUE	1
#define FALSE	0
/* copy
 *   Copies all the valid elements from the source array into the
 *   destination array.  Returns TRUE if successful, FALSE
 *   otherwise.  Sets the value of numDestElts appropriately.
 * numXXXXElts - the number of valid elts in the XXXX array
 * XXXXCap - the capacity of the XXXX array 
 * Preconditions:
 *   XXXXCap > 0, numXXXElts <= XXXXCap, destCap == sourceCap, 
 *   numSrcElts >= 0 */
int copy(char dest[], int *numDestElts, int destCap, 
         char source[], int numSrcElts, int sourceCap);

#define LESS	-1
#define EQUAL	MAX_INT /* If EQUAL were 0, it'd be confused with FALSE */
#define GREATER 1
/* compare
 *   Compares successive elements from the two arrays until it finds
 *   elements that are not equal.  If the two arrays are identical,
 *   returns EQUAL.  Returns LESS if the first differing element is less
 *   than the second; otherwise, returns GREATER.  Do not worry about the
 *   case of the letter.
 * Preconditions:
 *   numXXXXElts <= XXXXCap
 * Examples:
 *   first		second	result
 *   "abcde"	"abczz"	LESS
 *   "abc"		"abcd"	LESS
 *   "xyz"		"abcd"	GREATER */
int compare(char first[], int numFirstElts, int firstCap,
            char second[], int numSecondElts, int secondCap);

/* concat
 *   Copies all the valid elements from first followed by all the valid
 *   elements from second into dest.  Returns TRUE if successful, FALSE
 *   otherwise.  Sets the value of numDestElts appropriately.
 * Preconditions:
 *   numXXXXElts <= XXXXCap, destCap >= numFirstElts + numSecondElts 
 * Examples:
 *   destCap	first		second	result
 *	10		"Han"		"nah"		dest[] is "Hannah"
 *	10		"Chubby"	"Bunny"	FALSE (dest[] is too short) */
int concat(char dest[], int *numDestElts, int destCap, 
	char first[], int numFirstElts, int firstCap,
	char second[], int numSecondElts, int secondCap);
* Note that her three functions involve array copying, comparison, and concatenation. What is her obsession with C?