Assignment #3 Solution

$30.00 $24.90

Description

Include all the specified functions in a file “assignment-3.c”. Include a main program that calls the functions with test data and prints the results returned by the functions. Label the output so someone else can understand it. You must provide a documentation block for each function describing its purpose, parameters, and return values as appropriate. Check your file in to the CCIS GitHub repository 2017FACS5001SV/assignment-3-ccisID that was created by your instructor for your ccisID.

Problem 1

Write a function void initArray(unsigned int n, int array[], int value) that initializes all n elements of the input array to the specified value. Test the function in the main() function by calling it with arrays of size 0, 1, and one or two other small sizes, and printing the array on return. Be sure to label the output so someone reading it can verify the result.

Here are some examples for int arrays a0 of lenght 0, a1 of length 1, etc.

  • initArray(0, a0, 25) result: { }

  • initArray(1, a1, 42) result: { 42 }

  • initArray(3, a3, 88) result: { 88, 88, 88 }

  • initArray(6, a6, 14) result: { 14, 14, 14, 14, 14, 14 }

Problem 2

Write a function int wordCount(char str[]) that counts the number of words in the input string and return the count as the function result. A word is a sequence of characters that does not contain a space character (‘ ‘). The final word will end with the ‘\0’ end of string character. Use local counters, and a loop to implement this function. (Hint: what is the reliationship between the number of spaces and the number of words if just one space separates words?)

Test this function by passing it test strings and verifying that function returns the expected word count. Here are several examples for you to use in testing:

  • “” length: 0, word count: 0

  • “the” length: 3, word count: 1

  • “two words” length: 9, word count: 2

  • “the quick brown fox” length: 19, word count: 4

Although it is not required, consider how to make your function work corectly if there could multiple spaces before and afterwords.

Additional Exercises

Here are additional exercises that will give you more practice with the subject material in this module.

Pointers

  1. Write a function void minMax(unsigned n, int array[n], unsigned *minVal, unsigned *maxVal) that returns the minimum and maximum values in the array to the locations pointed to by minVal and maxVal.

  2. Write a function int *farthestFrom(unsigned n, int array[n], int val) that returns a pointer to the element of array whose value is farthest in absolute value from the value passed in. For example, given [3,8,7,14] and the value 5, the function returns a pointer to the array entry for n=14.

1D and 2D Arrays

  1. Write a function swapUpperLower(unsigned n, int array[n]) that swaps the upper and lower subarrays of an array. If n is even, the lower- and upper-subarrays are swapped. If n is odd, subarrays to the left and right of the center element are swapped and the center value stays fixed. For example, [1,3,5,7,2,4,6,8] becomes [2,4,6,8,1,3,5,7].

  2. Write a function double avg(unsigned n, double array[]) that computes the average of the values in the array. If the number of elements is 0, return the double constnat NaN.

  3. Write a function void rotateLeft(unsigned n, int array[n]) that shifts the elements in the n element array left by one position and moves the first element to the end position. For example, [10, 5, 2, 16] becomes [5, 2, 16, 10].

  4. Write a function void mask(unsigned n, int array[n], bool maskArray[n], int val) that sets all elements inarray to to val if the corresponding value in maskArray is true and leaves it unchanged if the corresponding value in maskArray is false.

  5. Write a function void addToAll(unsigned n, int array[n], int val) that given an int array of size n, adds val to every entry.

  6. Write a function transpose(unsigned nRows, unsigned nCols, int to[ncols][nrows], int from[nrows][ncols]);that given an nRows x nCols array from, transposes it into a nCols x nRows array to. The first row of frombecomes the first column of to, and so forth.

Strings

  1. Write a function void toUpper(char *str) that replaces any lower-case letters in the input string with their upper-case versions. Other characters are not changed.

  2. Write a function void toLower(char *str) that replaces any upper-case letters in the input string with their lower-case versions. Other characters are not changed.

  3. Implement a version of the C string function char *strcpy(char *s1, const char *s2) that copies string s2 into string s1, return s1 pointer

  4. Implement a version of the C string function char *stpcpy(char *s1, const char *s2) that copies string s2 into string s1, return a pointer to the ‘\0’ character in s1.

  5. Implement a version of the C string function char *strcat(char *s1, const char *s2) that concatenates string s2 onto the end of string s1, and return s1 pointer.

  6. Implement a version of the C string function int strcmp(const char *s1, const char *s2) that returns 0 if s1 and s2 are the same; less than 0 if s1 is lexically less than s2; greater than 0 if s1 is lexically greater than s2. A string s1 is lexically less than s2.

  7. Implement a version of the C string function char* strchr(const char*s1, char ch) that returns a pointer to the first occurrence of character ch in string s1.

  8. Implement a version of the C string function char* strstr(s1, s2) that returns a pointer to the first occurrence of string s2 in string s1.