lab10 1d arrays ii
TRANSCRIPT
-
8/10/2019 Lab10 1D Arrays II
1/3
ICS 103: Computer Programming in C
Lab 10: How to Use 1-D array with Funtions
!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 variablesas 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 needthe 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 elementspresent 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. +nthis 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 andprint them on the screen
void get_average (double a[], int n ); array used for input. #he functions uses the array values tofind sum and average.
void get_max_min (double a[], int n, double *max, double *min); array used for input. #hefunction 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 theuser 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. 0aluesare already present in the array when the function is called !input". #hese values are modified by
reversing their order !output".
1
-
8/10/2019 Lab10 1D Arrays II
2/3
The following example shows the use of a function with array as argument.#include #define SIZE 10int sumValues (int a[], int n ) !!function "otot$"eint main( ) %int &alues[SIZE]int total'sum,i
"intf(Ente d inte*e &alues >,SIZE) fo(i+0i
-
8/10/2019 Lab10 1D Arrays II
3/3
'(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 )
> ?48 @
> ?:8
?86 *
Bse the following functions to solve the problem%
readArrayA function that takes two parameters%1- double array.
/- +nteger nrepresenting the si(e of the array.
#he function should return an array containing !n" numbers read from the user.
minMaxA function that takes an array as an input and returns two values !the minimumand the ma$imum numbers found in the array".
calcGradeA 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- arrays $& y& and ( of si(e S+D3 !constant to be defined e'ual to16". #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
intersectionthat receives / integer arrays as input arguments and a third array ( to be used as outputto 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 functionprint_array.
9