PF Lab09
PF Lab09
Programming Fundamentals
Lab 09
Arrays – I
Version: 2.0.0
Lahore, Pakistan
Array:
An array is a group of elements of the same type stored contiguously in memory. The
following figure shows an integer array containing five elements:
To refer to a particular location or element in the array, we specify the array’s name, followed
by the element’s position number in square brackets ([]). The first element is located at position
number 0. The position number is called the element’s subscript (or index). A subscript must
be a non-negative integer or integer expression.
Defining Array:
When you define an array, you specify its element type and number of elements. The
following definition reserve five elements for integer array “A”, which has subscripts
(indexes) in the range 0-9.
int A[10];
Array Elements:
Like variables, uninitialized array elements contain garbage values. The following figure
uses for statements to set array elements to one.
Initializer List:
We can initialize an array’s elements when defining the array by providing a comma separated
list of array initializers in braces “{}”.
If there are fewer initializers than array elements, the remaining elements are initialized to 0.
Static Local Array:
A static local array is created and initialized once, not each time the function is called. This
reduces program execution time. If static array is not explicitly initialized, then that array’s
elements are initialized to zero by default.
Following code contains a static local array:
int Array[5];
The function call will be
function (Array, 5);
And function definition will be
void function (int Array[], int size);
Passing Individual Element:
To pass an individual array element to a function, the function call use the subscripted array
name in argument.
function (Array[2]);
and function definition will be
void function (int element);
Multidimensional Arrays:
Arrays can have multiple subscripts. A common use of multidimensional arrays is to represent
tables of values consisting of information arranged in rows and columns. To identify a particular
table element, we specify two subscripts:
• The first (by convention) identifies the element’s row.
• The second (by convention) identifies the element’s column.
Arrays that require two subscripts to identify a particular element commonly are called two-
dimensional arrays. Multidimensional arrays can have more than two subscripts.
Illustrating a Two-Dimensional Array:
The array contains three rows and four columns, so it’s said to be a 3 x 4 array. In general, an
array with m rows and n columns is called an m x n array. Every element in array a is identified
by a name of the form a[i][j], where a is the array name, and i and j are the subscripts that
uniquely identify each element. The element names in row 0 all have the first subscript 0.
Pictorial Representation:
Initializing a Double-Subscripted Array:
You can initialize a multidimensional array when you define it. For example, you can define
and initialize the two-dimensional array int b [2][2] with:
int b [2][2] = {{1, 2}, {3, 4}};
The values in the initializer list are grouped by row in braces. The values in the first set of
braces initialize row 0 and the values in the second set of braces initialize row 1. So, the values
1 and 2 initialize elements b [0][0] and b [0][1], respectively, and the values 3 and 4 initialize
elements b [1][0] and b [1][1], respectively. If there are not enough initializers for a given row,
that row’s remaining elements are initialized to 0. So, the definition:
int b [2][2] = {{1}, {3, 4}};
would initialize b [0][0] to 1, b [0][1] to 0, b [1][0] to 3 and b [1][1] to 4.
Example Code:
Output:
Explanation:
array1 Definition: The program defines three arrays of two rows and three columns. The
definition of array1 (line 6) provides six initializers in two sublists. The first sublist initializes
row 0 to the values 1, 2 and 3, and the second sublist initializes row 1 to the values 4, 5 and 6.
array2 Definition: The definition of array2 (line 10) provides five initializers in two sublists,
initializing row 0 to 1, 2 and 3, and row 1 to 4, 5 and 0. Any elements that do not have an
explicit initializer are initialized to zero automatically, so array2[1][2] is initialized to 0.
array3 Definition: The definition of array3 (line 14) provides three initializers in two sublists.
The first row’s sublist explicitly initializes the row’s first two elements to 1 and 2 and implicitly
initializes the third element to 0. The second row’s sublist explicitly initializes the first element
to 4 and implicitly initializes the last two elements to 0.
Example Code:
Output:
Explanation:
In the above program, we have declared and printed three variable length arrays. First, we
asked the user to enter the size for a one-dimensional array, two-dimensional and, multi-
dimensional array.
There are two user-defined functions oneDArray () and twoDArray () for printing arrays.
Function oneDArray () takes size as parameter whereas, twoDArray takes row and col as its
parameter. First, all the elements in the first row of the multi-dimensional array are filled, then
proceed for the second row and so on.
Lab Tasks
Input Output
23456 Unique Elements: 2, 3, 4, 5,
6
89 89 55 34 55 Unique Element: 34
33 33 33 33 33 No element is unique