Arrays and Functions You can pass individual array elements to a function just like other variables. If you have declared hours as: double hours[10], the function call sqrt(hours[0]) returns the square root of the first element of array hours. If you have declared a function with prototype void my_fun(int, double); and arrays int id[10]; double hours[10]; then you can write my_fun(id[3], hours[10]); Array element id[3] (type int variable) is the first actual argument and array element hours[10] (type double variable) is the second actual argument. You can also pass entire arrays to functions as arguments. For example, function print_int_array displays all elements of a type int array. void print_int_array(int list[], /* array to print */ int n) /* number of elements */ { int i; /* subscript and loop control variable */ for (i = 0; I < n; I++) printf("% %d", i, list[i_]); } There is no actual array with the name list. The array that is processed is determined by the first actual argument in a function call. The statement print_int_array(id, 10); displays all values in array id (array id corresponds to the first formal argument - int list[]). Each reference to list[i] processes the element in actual array id with subscript i. Notice there are no brackets after the array name in the function call but brackets are required to identify the formal parameter as an array (int list[]). The function prototype is: void print_int_array(int[], int); /*first arg - integer array*/ Function to store a particular value in all elements of a type double array. void fill_array(double items[], /* output argument */ int n, double in_value) { int i; for (i = 0; i < n; i++) items[I] = in_value ; } /* set all elements of array hours[hours_size] to 1.0 */ fill_array(hours, hours_size , 1.0); Notice that the assignment statement in the function changes the value stored in the element of array hours with subscript i. This is the first example of a function that changes an argument value. The array is considered a function output argument, not an input argument. Function to add 2 arrays and store the result in a third array. void add_arrays(double a[], /* input - first array */ double b[], /* input - second array */ double sum[], /* output - array of sums*/ int n) /* input- number of elements*/ { int I; for (I = 0; I < n; I++) sum[I] = a[I] + b[I]; } double reg_pay[10], overtime_pay[10], total_pay[10]; /* Write statement to store the sum of corresponding * elements in arrays reg_pay and overtime_pay in * total_pay. */ add_arrays(reg_pay, overtime_pay, total_pay, 10); Function prototype: void add_arrays(_double[]_,double[],double[], int); Function to find the smallest value in an array. Algorithm to find the smallest value in a collection of integers. The key is to allocate a storage location that saves the smallest value encountered so far. 1. Consider the first value to be the smallest value so far. 2. If the second value is smaller than the smallest value so far, save it as the smallest value so far. 3. If the third value is smaller than the smallest value so far, save it as the smallest value so far. 4. .... Steps 2 and on processes all elements of the array in the same way - we can do this by writing a loop. 1. Consider the first value to be the smallest value so far. 2. For each array element If that element is smaller than the smallest value so far Save the current element as the smallest value so far 3. Return the smallest value so far. int find_min (______list____, int n) { int I; int min_so_far; min_so_far = _______; for (I = ____; I < n; I++) if (_______________) ____________ = ____________; return _____; } int find_min (int list[], int n) { int I; int min_so_far; min_so_far = list[0];/*first element is smallest so far*/ for (I = 1; I < n; I++) if (list[I] < min_so_far) min_so_far = list[I]; /* save list[I]*/; return min_so_far; } Modification: write function find_max that returns the largest value in an array. int find_max (int list[], int n) { int I; int max_so_far; max_so_far = list[0]; for (I = 1; I < n; I++) if (list[I] > max_so_far) max_so_far = list[I]; return max_so_far; } Modify function find_min so it returns the location (subscript) of the smallest value, not the smallest value. Need to remember the location of the smallest value so far. int find_min (int list[], int n) { int I; int min_so_far; /*subscript of smallest value so far*/ min_so_far = _0;/*0 is location of smallest so far*/ for (I = 1; I < n; I++) if (list[I] < list[min_so_far]) min_so_far = ___i_; /* save I */ return min_so_far; } Modify so that it finds the subscript of the smallest value so far among elements with subscripts min through max of the array instead of the entire array. int find_min (int list[], int min, /* first subscript in range*/ int max) /* last subscript in range */ { int I; int min_so_far; /*subscript of smallest value so far*/ min_so_far = ___;/* location of smallest so far is __*/ for (I = _____; I ________; I++) if (list[I] < __________) min_so_far = I; /* save I */ return min_so_far; } Modify so that it finds the subscript of the smallest value so far among elements with subscripts min through max of the array instead of the entire array. int find_min (int list[], int min, /* first subscript in range*/ int max) /* last subscript in range */ { int I; int min_so_far; /*subscript of smallest value so far*/ min_so_far=min;/* location of smallest so far is min*/ for (I = min; I <= max; I++) if (list[I] < list[min_so_far] min_so_far = I; /* save I */ return min_so_far; }