This document provides instructions on using 1-D arrays with functions in C programming. It explains that arrays can be passed to functions in different ways: by passing individual elements, passing the address of the array, or passing the entire array. When an array is passed to a function, the function receives the address of the actual array and can directly access and modify the array elements. Examples of functions that use arrays for input, output, or both are provided to demonstrate how arrays behave like pointers in functions. The document also includes an example program that defines a function to calculate the sum of array elements and calls this function, demonstrating how an array can be passed and accessed within a function.
This document provides instructions on using 1-D arrays with functions in C programming. It explains that arrays can be passed to functions in different ways: by passing individual elements, passing the address of the array, or passing the entire array. When an array is passed to a function, the function receives the address of the actual array and can directly access and modify the array elements. Examples of functions that use arrays for input, output, or both are provided to demonstrate how arrays behave like pointers in functions. The document also includes an example program that defines a function to calculate the sum of array elements and calls this function, demonstrating how an array can be passed and accessed within a function.
This document provides instructions on using 1-D arrays with functions in C programming. It explains that arrays can be passed to functions in different ways: by passing individual elements, passing the address of the array, or passing the entire array. When an array is passed to a function, the function receives the address of the actual array and can directly access and modify the array elements. Examples of functions that use arrays for input, output, or both are provided to demonstrate how arrays behave like pointers in functions. The document also includes an example program that defines a function to calculate the sum of array elements and calls this function, demonstrating how an array can be passed and accessed within a function.
!b"eti#e: Practice how to use one-dimensional arrays with function. $a%groun&: You can pass individual array elements or their addresses to a function that has normal or pointer variables as arguments respectively. Array elements behave like normal variables. We can also have a function that takes the whole array as argument. Such a function does not create a local array when called; instead it will ust receive the address of the actual array !the one used in the call" and work directly on its elements. #hus array addresses are passed in a function call. An e$ample of a prototype of a function that has an array as argument is% void print_array (double a[], int n); As noticed above& we use s'uare brackets without specifying the si(e. )ut in addition to the array& we need the number of elements of the array and this is why we have the second argument n. When calling such a function& we use the name of the actual array without s'uare brackets for the first argument. *or the second argument& we use the si(e of the actual array if it is full& otherwise we use the actual number of the elements present in the array if it is partially filled. When array are used as arguments& they behave like pointers. Since the function is able to access all the elements of the actual array because it has received its address& it can use the values of the array elements. +n this case& the array is used as input. +t can also assign values to the elements of the array; +n such a case& the array is used as output. +t can also modify the values of the array elements; leading to the array being used for both input and output. ,ore clarification for this issue by looking at the functions e$amples covered in the lecture% void print_array (double a[], int n); -- array used for input. #he function accesses the --values and print them on the screen void get_average (double a[], int n ); -- array used for input. #he functions uses the array --values to find sum and average. void get_max_min (double a[], int n, double *max, double *min); -- array used for input. #he --function uses the array values to find ma$imum and minimum. void read_array (double a[], int n); -- array used for output. #he function reads values --from the user and assigns them to the elements of the array. void double_array (double a[], int n); -- .ere the array is used for both input and output. When --calling the function& the array has already values in its elements. #he function --modifies these values by --multiplying them by /. #hus the modified array will be output. void reverse_array (double a[], int n); -- .ere also the array is used for input and output. 0alues are --already present in the array when the function is called !input". #hese values are modified by reversing --their order !output". 1 The following example shows the use of a function with array as argument. #include <stdio.h> #define SIZE 10 int sumValues (int a[], int n ) !!function "#otot$"e int main( ) % int &alues[SIZE] int total'sum,i "#intf((Ente# )d inte*e# &alues >(,SIZE) fo#(i+0i<SIZEi,,) scanf(()d(,-&alues[i]) total'sum + sumValues (&alues, SIZE) !!function call !!.he function accesses di#ectl$ the elements of a##a$ &alues. It does !! not c#eate a local a##a$ a "#intf((.otal sum+)d(,total'sum) #etu#n 0 / !! function definition int sumValues (int a[], int n) % int i,sum + 0 fo#( i+0 i < n i,,) sum,+a[i] #etu#n sum / '(erises: '(erise ) 1: Write a program that reads from the user two arrays of the same si(e% Array *% contains double numbers representing the students2 lab marks Array $% contains integer numbers representing the total absences for each student. #he program then calls a function called !a+,ota+" that will take array * and $ as an input and returns an array C of type double that contains the result of subtracting each element in $ from the corresponding element in *. 3$ample% *rray * 45.6 76.6 88.8 58.4 77.6 *rray $ 8 9 / 6 9 *rray C 49.6 54.6 89.8 58.4 7:.6 / '(erise ) -: Write a program that reads a list of students2 marks and prints the following% 1- #he minimum grade. /- #he ma$imum grade. 9- #he corresponding letter grade for each student according to the following table% ,ark ;etter <= 76 A <=48 >> ?76 ) <=:8 >> ?48 @ <=86 >> ?:8 A ?86 * Bse the following functions to solve the problem% readArray A function that takes two parameters% 1- double array. /- +nteger n representing the si(e of the array. #he function should return an array containing !n" numbers read from the user. minMax A function that takes an array as an input and returns two values !the minimum and the ma$imum numbers found in the array". calcGrade A functions that takes a double array containing the students C marks and returns another array of type char containing the corresponding letter grade for each student. '(erise ) 3: Write a @ program that declares 9 integer 1-A arrays $& y& and ( of si(e S+D3 !constant to be defined e'ual to 16". #he program then reads values for $ and y arrays from the user!use some common values". *or the reading use the function readEarray. *inally the program finds the common values in $ and y. #o achieve this& you need to write a function intersection that receives / integer arrays as input arguments and a third array ( to be used as output to contain the common values. +n addition to putting the common values in array (& the function returns the number of common values. After calling the intersection function& your main function will display the contents of the three arrays on the screen. *or printing use the function print_array. 9