ITP Exp11
ITP Exp11
OBJECTIVES
The main purpose of the experiment is to introduce you to C arrays. In this experiment, the
relationship between an array and a function is explained. Then, some array examples are
studied.
INFORMATION
An array is a data structure to store series of elements of the same type and the same name
placed in contiguous memory locations. Naturally, in order to pass an array to a function, we
must use the name of the array as an argument. Additionally, the size of the array is required to
process the array in the function.
A crucial property of the array name is to store the address of the first element of the array
(Fig.1). As seen from the figure, an array is defined and its name is A. The array name A is equal
to
&A[0];
statement which states that the address of the first element of the array. Therefore, when we pass
the name of the array to a function, the starting address of the array is passed to the function and
it knows the memory location of the array. As a result, the function reaches the original array
memory locations and modifies the value of the elements.
Now, we must remember how we use the user-defined C functions. There are three important
concepts to use user-defined C functions. One of them is function prototype that informs the
compiler about the function. An example function prototype that receives an array as follows:
E-mail : [email protected]
Introduction to Programming Laboratory, Fall 2022
statement indicates that passArray function expects to receive an integer array and its size. Also,
functions must be invoked by a function call which specify function name, information used in
the function (arguments) and results of the performed task. Assume that, we have an integer
array with 7 elements and its name is burak. In order to invoke passArray function with burak
array is:
passArray(burak,7);
header states that “a” points to the original array “burak”, size represents the number of elements
in the array. At that point, we are ready to analyze an example code for passing an array and an
element of an array to a function (Fig. 2).
Lines 12 and 13 show the function prototypes. The passArray function receives an integer
array and its size as in integer type. The passElement function simply receives an integer value.
In line 19, an integer array numbers is defined with 10 elements and it is initialized to zero by
using initializer list method. Then, line 24-27 prints the elements of the array. passArray function
is called by line 32. As seen from the figure, the integer array name numbers and its size 10 are
passed as argument. Line 36-39 prints the elements of the array numbers after modification of
the array. Line 44 prints the numbers[7]. Line 47 calls passElement function and passes
numbers[7] as argument. Then, line 50 prints the numbers[7] after passElement function called.
Lastly, definition of the passArray function begins with line 58. passArray function expects an
integer array and its size. The array “a” points to the original array “numbers”. size holds a copy
for the number of elements in the array. Line 63-65 modifies elements of the original array
numbers. The first element holds 0, second 1, and each element holds element-1 value. The
definition of the passElement function begins with line 72. The integer variable b stores only a
copy of the numbers[7] and; modifies and prints it locally at line 75.
QUESTIONS
1) Write a C program to find minimum of an array and print the result. Define an array that
stores 10 integer numbers (i.e. numbers[10]), then initialize the array by using loop initialization
method such that each element must contain 1-digit (0-9) number randomly. The program must
include the minimum function that receives the array and returns result. Print the result and the
array in main function.
2) Write a C program to count total number of negative elements in array. Define an array that
stores 20 integer numbers (i.e. numbers[20]), then initialize the array randomly in interval [-8,9].
The program must include the count function that receives the numbers array and returns total
number of negative elements in array. In main function, print the numbers array and result.
3) Write a C program to put all even and odd elements in two separate arrays. Define an array
that stores 20 integer numbers (i.e. numbers[20]), then initialize the array randomly in interval
[11,48]. Also, define two integer arrays for odd (i.e odd[20]) and even (i.e even[20]) numbers.
Then, initialize these arrays to zero. The program must include the separate function that
receives the numbers, odd, and even arrays. In main function, print the numbers, odd and even
arrays.
E-mail : [email protected]
Introduction to Programming Laboratory, Fall 2022
4) Write a C program to merge two sorted array into third array. Define the A array that stores 6
integer numbers (i.e. A[6]), then initialize the array by using initializer list method such that
A[6]={1,4,7,9,15,18}. Also, define the B array that stores 5 integer numbers (i.e. B[5]), then
initialize the array by using initializer list method such that B[5]={2,6,10,13,19}. Lastly, define
the C array that stores 11 integer numbers (i.e. C[11]), and initialize the array to zero. The
program must include the merge function that receives the A, B, and C arrays. In the function,
merge A and B into C in ascending order. In main function, print the A, B, and C arrays.
[1]http://en.wikibooks.org/wiki/Polymorphic_Data_Structures_in_C/Function_Invocation, 2018.
[2] P. J. Deitel and H. M. Deitel, “C How To Program Fifth Edition”, Prentice Hall, 2007.
E-mail : [email protected]
Introduction to Programming Laboratory, Fall 2022
E-mail : [email protected]