Question
4 Exercise: Arrays and Functions Many of the tasks from the previous exercises can be generalized to functions, allowing easy reuse. Recall that arrays in
4 Exercise: Arrays and Functions
Many of the tasks from the previous exercises can be generalized to functions, allowing easy reuse. Recall that arrays in C are essentially represented by pointers, so when an array is passed into a function, the function is given access to the original array data (not a copy). This means that arrays are effectively passed by reference in C, and therefore that functions must be careful not to modify the contents of arrays they receive as arguments. Also recall that the size of an array cannot be obtained directly from the array; instead, a function which receives an array must take an extra parameter giving the size of the array.
Exercise 3: Complete the three functions in the code below. You will probably want to use your code from Exercise 2.
#include/* find_minimum_element( the_array, size ) Given an array of the specified size, find and return the minimum element of the array. */ int find_minimum_element(int the_array[], int size){ /* Your code here */ } /* search_for_element( the_array, size, search_value ) Given an array of the specified size, along with a value to search for, return 1 if the provided value is found somewhere in the array and 0 otherwise. */ int search_for_element(int the_array[], int size, int search_value){ /* Your code here */ } /* clamp( the_array, size, lower_bound, upper_bound ) Given an array of the specified size, along with two bounds lower_bound and upper_bound, do the following: - Set any element with a value less than lower_bound to equal lower_bound - Set any element with a value greater than upper_bound to equal upper_bound - Leave all other elements intact. Note that the input array is modified by this function (and therefore, no return value is necessary). */ void clamp(int the_array[], int size, int lower_bound, int upper_bound ){ /* Your code here */ } /* print_array( the_array, size ) Given an array of the specified size, print every element of the array, followed by a newline. */ void print_array( int the_array[], int size ){ int i; for (i = 0; i < size; i++){ printf("%d ", the_array[i]); } printf(" "); } /* Do not modify anything below here */ int main(){ int A1[5] = {31, -41, 59, 2, 65}; int A2[7] = {3, 58, 9, 79, 323, -8, 4}; printf("A1 is "); print_array(A1,5); printf("A2 is "); print_array(A2,7); printf("Minimum element of A1: %d ", find_minimum_element(A1, 5)); printf("Minimum element of A2 (first four elements): %d ", find_minimum_element(A2, 4)); printf("Minimum element of A2 (all elements): %d ", find_minimum_element(A2, 7)); printf("Does A1 contain the element 2? search_for_element returns %d ", search_for_element(A1, 5, 2)); printf("Does A1 contain the element 6? search_for_element returns %d ", search_for_element(A1, 5, 6)); printf("Does A2 contain the element 6? search_for_element returns %d ", search_for_element(A2, 7, 6)); printf("Does A2 contain the element 4? search_for_element returns %d ", search_for_element(A2, 7, 4)); printf("Clamping A1 to the range [0, 50] "); clamp(A1, 5, 0, 50); printf("A1 is "); print_array(A1,5); printf("Clamping A2 to the range [10, 100] "); clamp(A2, 7, 10, 100); printf("A2 is "); print_array(A2,7); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started