Solved–Arrays, Strings, and Pointers –Solution

$30.00 $19.00

​Pointers 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. 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…

You’ll get a: . zip file solution

 

 

Description

5/5 – (2 votes)

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.