Question

4 Exercise: Arrays and Functions Many of the tasks from the previous exercises can be generalized...

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 <stdio.h>

/* 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("\n");
}

/* 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\n", find_minimum_element(A1, 5));
    printf("Minimum element of A2 (first four elements): %d\n", find_minimum_element(A2, 4));
    printf("Minimum element of A2 (all elements): %d\n", find_minimum_element(A2, 7));

    printf("Does A1 contain the element 2? search_for_element returns %d\n", search_for_element(A1, 5, 2));
    printf("Does A1 contain the element 6? search_for_element returns %d\n", search_for_element(A1, 5, 6));
    printf("Does A2 contain the element 6? search_for_element returns %d\n", search_for_element(A2, 7, 6));
    printf("Does A2 contain the element 4? search_for_element returns %d\n", search_for_element(A2, 7, 4));

    printf("Clamping A1 to the range [0, 50]\n");
    clamp(A1, 5, 0, 50);
    printf("A1 is ");
    print_array(A1,5);

    printf("Clamping A2 to the range [10, 100]\n");
    clamp(A2, 7, 10, 100);
    printf("A2 is ");
    print_array(A2,7);


    return 0;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

/* 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 */
    int min = the_array[0];
    int i;
    for(i = 0; i < size; ++i) {
       if(the_array[i] < min) {
          min = the_array[i];
      }
   }
   return min;
}

/* 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){
    int i;
    for(i = 0; i < size; ++i) {
       if(the_array[i] == search_value) {
          return 1;
      }
   }
   return 0;
}

/* 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 ){
    int i;
    for(i = 0; i < size; ++i) {
       if(the_array[i] < lower_bound) {
          the_array[i] = lower_bound;
      }
      if(the_array[i] > upper_bound) {
          the_array[i] = upper_bound;
      }
   }
}

/* 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("\n");
}

/* 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\n", find_minimum_element(A1, 5));
    printf("Minimum element of A2 (first four elements): %d\n", find_minimum_element(A2, 4));
    printf("Minimum element of A2 (all elements): %d\n", find_minimum_element(A2, 7));

    printf("Does A1 contain the element 2? search_for_element returns %d\n", search_for_element(A1, 5, 2));
    printf("Does A1 contain the element 6? search_for_element returns %d\n", search_for_element(A1, 5, 6));
    printf("Does A2 contain the element 6? search_for_element returns %d\n", search_for_element(A2, 7, 6));
    printf("Does A2 contain the element 4? search_for_element returns %d\n", search_for_element(A2, 7, 4));

    printf("Clamping A1 to the range [0, 50]\n");
    clamp(A1, 5, 0, 50);
    printf("A1 is ");
    print_array(A1,5);

    printf("Clamping A2 to the range [10, 100]\n");
    clamp(A2, 7, 10, 100);
    printf("A2 is ");
    print_array(A2,7);


    return 0;
}

1 is 31-41 59 2 65 2 is 3 58 9 79 323 -8 4 Minimum element of A1 -41 Minimum element of A2 <first four elements 3 Minimum ele

Add a comment
Know the answer?
Add Answer to:
4 Exercise: Arrays and Functions Many of the tasks from the previous exercises can be generalized...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • From Algorithms 4th Ed by Sedgewick, Robert. Ex 1.4.10(modified) Exercise 1.4.10 I’m modifying the exercise from...

    From Algorithms 4th Ed by Sedgewick, Robert. Ex 1.4.10(modified) Exercise 1.4.10 I’m modifying the exercise from what’s in the book. I want two functions, lower_bound() and upper_bound(), defined as: int lower_bound(int[] a, int key) int upper_bound(int[] a, int key) lower_bound() should return the index of the first (lowest indexed) element of a that is greater than or equal to key, and upper_bound() returns the index of the first (lowest indexed) element of a that is greater than key. You may...

  • Consider the syntactically correct C code below, which is missing a function copy_positive. #include <stdio.h> /*...

    Consider the syntactically correct C code below, which is missing a function copy_positive. #include <stdio.h> /* copy_positive(A, Aout, size) * Given an array A, which will have the provided size, copy all positive * (non-negative/non-zero) elements of A into the provided output array Aout. * Return the size of the resulting array. */ /* (your code from below would be placed here) */ void print_array(int arr[], int n){ for( int i = 0; i < n; i++ ) printf("%d ",...

  • How do i Overload the & operator to concatenate two arrays?, so elements from both arrays...

    How do i Overload the & operator to concatenate two arrays?, so elements from both arrays will be seen Below is code i have so far. everything works except the operator overload of &. The & operator only works when both array are equal, first array smaller then second, fails when first array is the largest. class smartArray{ public: int *elements; // dynamic array, memory is allocated in the constructor. Use *this to access size member. int length(); // returns...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods...

    JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods (note that all data members and methods are for objects unless specified as being for the entire class) 1) Data members: none 2) Methods a. A method named displayContents that accepts an array of type int and prints their values to the b. A method named cemoveNegatives that accepts an array of type int, copies all its non-negative the new array should be the...

  • In C Write a couple of functions to process arrays. Note that from the description of...

    In C Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an int array and size, and...

  • How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++...

    How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...

  • c++. please show screenshots of output This project will continue to familiarize the student with using...

    c++. please show screenshots of output This project will continue to familiarize the student with using arrays. Store all the function proto-types in project11_funch and store all the functions in project11_func.cpp. Make sure you have proper header comments in each.h,.cpp file. When you run your program, your sample output should show your name. Your report should include the following: 1) project11_func.h 2) project11_func.cpp 3) project11.cpp 4) sample output Write the following functions: a) void fill_array_with_random_nums(int array(), int N): Fill array...

  • MIPS assembly language Implement the following code in MIPS int array [ ] {2, 3, 4,...

    MIPS assembly language Implement the following code in MIPS int array [ ] {2, 3, 4, 5, 6); int main) int num, position; scanf("%d",&num) ; position search(array, printf("The position is: num, 5); %d\n",positio int search(int array, int num, int size int position =-1; for(int i-0;i<size; i++) if(array [i]=num) { position-i; break; return position; Register map $s1: position $a0: array address $a1: num . $a2: size . $VO: return value

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT